对resultMap的感觉
1.
1.
1. resultMap用于绑定某种属性;
2.
2.
2. resultMap可以实现定制键值对来绑定实体类属性和数据库字段;
3.
3.
3. resultMap可以实现嵌套结果,从而嵌套绑定;通过关联(association )或集合(collection )实现跨表绑定。
当实体类属性名与数据库字段不对应时
映射xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.UserMapper">
<resultMap id="UserMap" type="com.tl.pojo.User">
<!--column数据库中的字段,property实体类中的属性-->
<result column="id" property="id" />
<result column="name" property="name" />
<result column="pwd" property="password" />
</resultMap>
<select id="getUserById" resultMap="UserMap">
select * from user where id=#{id}
</select>
</mapper>
实体类
public class User {
private int id;
private String name;
private String password;
}
测试
public class Test {
@org.junit.Test
public void Test(){
//通过工具类获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//通过sqlSession.getMapper获取接口对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//通过接口获取其实现的方法返回的user对象集
User user = userMapper.getUserById(5);
System.out.println(user);
}
}
多对一(按结果嵌套)
映射xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.StudentMapper">
<select id="getStuInfo" resultMap="studentTeacher">
select s.id sid,s.name sname,t.name tname,s.tid
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="studentTeacher" type="com.tl.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
<!--关联结果-->
<association property="teacher" javaType="com.tl.pojo.Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
实体类
public class Student {
private int id;
private int tid;
private String name;
private String pwd;
//组合另一张表对象
private Teacher teacher;
}
测试
@org.junit.Test
public void Test(){
//通过工具类获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//通过sqlSession.getMapper获取接口对象
StudentMapper userMapper = sqlSession.getMapper(StudentMapper.class);
//通过接口获取其实现的方法返回的user对象集
List<Student> list = userMapper.getStuInfo();
for(Student student:list){
System.out.println(student);
}
}
多对一结果
为null是因为SQL语句没有指定字段,mybatis补上了默认值;
一对多(按结果嵌套)
映射xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.TeacherMapper">
<select id="getTeaInfo" resultMap="teacherStudent">
select t.name tname,t.id tid,s.name sname
from teacher t,student s
where s.tid=t.id and t.id=#{tid};
</select>
<resultMap id="teacherStudent" type="com.tl.pojo.Teacher">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
<collection property="students" ofType="com.tl.pojo.Student">
<result property="name" column="sname"/>
</collection>
</resultMap>
</mapper>
实体类
public class Teacher {
private int id;
private String name;
private String pwd;
//集合
private List<Student> students;
}
测试
@org.junit.Test
public void test2(){
//通过工具类获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//通过sqlSession.getMapper获取接口对象
TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
//通过接口获取其实现的方法返回的user对象集
List<Teacher> list = teacherMapper.getTeaInfo(1);
for(Teacher teacher:list){
System.out.println(teacher);
}
// System.out.println(teacher);
}
一对多结果
为0为null是因为没有指定字段,mybatis补上了默认值;
/*
Teacher{id=1, name='唐三藏', pwd='null',
students=[Student{id=0, tid=0, name='至尊宝', pwd='null'},
Student{id=0, tid=0, name='紫霞仙子', pwd='null'},
Student{id=0, tid=0, name='猪八戒', pwd='null'},
Student{id=0, tid=0, name='沙僧', pwd='null'}]}
*/