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; } }
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>
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; } }
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; }
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; } }
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, stdout
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()); } } }
对应StudentDAO.xml中常规配法的结果:
对应StudentDAO.xml中resultMap配法的结果:
这样就可以成功的拿到值了,这就是resultMap的作用。
是不是很简单,这里只是记录一下。。