1.大家學習MyBatis時,可能會碰到實體類屬性跟數據庫字段不同的情況
如:數據庫 ------ 實體類
stuname ----> name
即: 數據庫中的stuname字段對應的事實體類裡的name屬性
如果這時,我們要用常規的查詢方法時是不能正確查詢到stuname的值的,它會顯示為null
這時,我們可以使用我們的resultMap來解決這一問題。。。
源碼介紹與對比:
1.Student.java (實體類)
package cn.zhang.entity; import java.util.Date; /** * 學生實體類 * */ public class Student { private Integer stuno; private String name; private Integer stuage; private Date studate; @Override public String toString() { return "Student [stuno=" + stuno + ", name=" + name + ", stuage=" + stuage + ", studate=" + studate + "]"; } public Integer getStuno() { return stuno; } public void setStuno(Integer stuno) { this.stuno = stuno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getStuage() { return stuage; } public void setStuage(Integer stuage) { this.stuage = stuage; } public Date getStudate() { return studate; } public void setStudate(Date studate) { this.studate = studate; } }View Code
2.mybatis-config.xml (MyBatis的配置文件)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置別名 --> <typeAliases> <!--方式一: 按類型名定制別名 --> <typeAlias type="cn.zhang.entity.Student" alias="Student" /> <!--方式二: 拿當前指定包下的簡單類名作為別名 --> <!-- <package name="cn.zhang.entity"/> --> </typeAliases> <environments default="mysql"> <environment id="mysql"> <!-- 使用jdbc的事務 --> <transactionManager type="JDBC" /> <!-- 使用自帶的連接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/y2161" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/zhang/dao/StudentDAO.xml" /> </mappers> </configuration>View Code
3.MybatisUtil.java (獲得session的工具類)
package cn.zhang.util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * 獲得session的工具類 * */ public class MybatisUtil { private static String config = "mybatis-config.xml"; static Reader reader; static { try { reader = Resources.getResourceAsReader(config); } catch (IOException e) { e.printStackTrace(); } } private static SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); // 提供一個可以獲取到session的方法 public static SqlSession getSession() throws IOException { SqlSession session = factory.openSession(); return session; } }View Code
4.StudentDao.java (定義方法的接口)
package cn.zhang.dao; import java.io.IOException; import java.util.List; import cn.zhang.entity.Student; public interface StudentDao { /** * 查詢所有記錄 * @return * @throws IOException */ public List<Student> findAll() throws IOException; }View Code
5.StudentDaoImpl.java (實現接口方法的實現類)
package cn.zhang.dao.impl; import java.io.IOException; import java.util.List; import org.apache.ibatis.session.SqlSession; import cn.zhang.dao.StudentDao; import cn.zhang.entity.Student; import cn.zhang.util.MybatisUtil; public class StudentDaoImpl implements StudentDao { SqlSession session; public StudentDaoImpl() throws IOException { session = MybatisUtil.getSession(); } /** * 查詢所有 */ public java.util.List<Student> findAll() throws IOException { List<Student> list = session.selectList("findAll"); session.close(); return list; } }View Code
6.StudentDAO.xml (對應的映射文件)
最常規的配法:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.zhang.dao"> <!-- 查詢所有 --> <select id="findAll" resultType="Student"> select * from student </select> </mapper>
用resultMap的配法:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.zhang.dao"> <!-- 結果映射,指定了數據庫和實體類中的對應值 --> <resultMap type="Student" id="findstudent"> <result property="name" column="stuname" /> </resultMap> <!-- 查詢所有 --> <select id="findAll" resultMap="findstudent"> select * from student </select> </mapper>
7.log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=debug, stdoutView Code
8..MyTest.java (測試類)
package cn.zhang.test; import java.io.IOException; import java.util.List; import org.junit.Before; import org.junit.Test; import cn.zhang.dao.StudentDao; import cn.zhang.dao.impl.StudentDaoImpl; import cn.zhang.entity.Student; public class MyTest { StudentDao dao; @Before public void initData() throws IOException{ dao=new StudentDaoImpl(); } /** * 查詢所有學生 * @throws IOException */ @Test public void findAll() throws IOException{ List<Student> list = dao.findAll(); for (Student student : list) { System.out.println("編號: "+student.getStuno()+"姓名:"+student.getName()); } } }View Code
對應StudentDAO.xml中常規配法的結果:
對應StudentDAO.xml中resultMap配法的結果:
這樣就可以成功的拿到值了,這就是resultMap的作用。
是不是很簡單,這裡只是記錄一下。。