在mybatis默认中采用 Auto Mapping特性进行对数据库查询结果与实体类的映射,这时要求实体类所有的属性必须与查询得到的列的名字相同,否则就会出现空值。
使用ResultMap标检可以进行自定义映射关系
1.单表映射
1.1 数据库设计--student表
1.2 实体类设计
1.3 采用 mybatis 默认设置查询所得结果
------- 可以看到当实体类的属性与查询所得列名不同时是无法接受到参数值得。
1.4 ResultMap 标签 进行自定义
<mapper namespace="com.yyl.Mapper.StudentMapper">
<resultMap type="student" id="stuMap"> // type 表示返回值类型 id 表示此resultmap名字
<id column="id" property="sid"/> // id 标签表示 主键 column 表示 列名
<result column="name" property="sname"/> //property 表示 实体类的属性名
<result column="age" property="age"/>
<result column="tid" property="tid"/>
</resultMap>
<select id="selAll" resultMap="stuMap"> // resultMap需要与上面的对应
select * from student
</select>
</mapper>
1.5 结果
2.使用ResultMap实现关联单个对象
即查询出一个表的信息,在根据这个表的信息查询出另一个表的信息。
2.1 创建另一个实体类 -- Teacher
学生与教师之间关系为多对多
在原来的student中增加Teacher
数据库设计如下:
2.2 自定义 ResultMap标签
// <association> property 返回值类型 column 表示 传给另一个查询参数
// select 第二个查询语句所在位置
<mapper namespace="com.yyl.Mapper.StudentMapper">
<resultMap type="student" id="stuMap">
<id column="id" property="sid"/>
<result column="name" property="sname"/>
<result column="age" property="age"/>
<result column="tid" property="tid"/>
<association property="teacher" column="tid"
select="com.yyl.Mapper.TeacherMapper.selById">
</association>
</resultMap>
<select id="selAll" resultMap="stuMap">
select * from student
</select>
</mapper>
2.3 结果
3.ResultMap 查询关联集合对象
3.1 在Teacher 表中添加 List<Student>
教师与学生之间的关系为 1 对 多
3.2 自定义映射
// <collectio> 集合 property 表示 属性 column 表示 传给select的参数 ofType 表示集合泛型
// select 同上
<mapper namespace="com.yyl.Mapper.TeacherMapper">
<resultMap type="teacher" id="teaMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="list" column="id" ofType="student"
select="com.yyl.Mapper.StudentMapper.selById"></collection>
</resultMap>
3.3 结果
4.ResultMap 加载集合数据
------------ 两表联合查询
4.1 自定义ResultMap
<resultMap type="teacher" id="teaMap2">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
<collection property="list" ofType="student">
<id column="sid" property="id"/> //返回结果为Student 确定student的属性之间映射关系
<result column="sname" property="name"/>
<result column="sage" property="age"/>
<result column="tid" property="tid"/>
</collection>
</resultMap>
4.2 SQL语句
<select id="selTea" resultMap="teaMap2">
select t.id tid,t.name tname,s.id sid,s.name sname,s.age sage, tid from teacher t left
join student s on t.id = s.tid
</select>
4.3 运行结果
5 使用Auto Mapping 和别名实现多表查询
5.1 sql 语句
<select id="selStu" resultType="student">
select t.id `teacher.id`,t.name `teacher.name`,s.id sid,s.name sname,s.age
sage,tid from student s left join teacher t on s.tid = t.id
</select>
5.2 结果
6. MyBatis 注解
6.1 建立Mapper 接口
@Select("select * from student")
List<Student> selAllStu();
@Insert("SQL 语句")
int insStu();
@Delete("SQL 语句")
int delStu();
@Update("SQL 语句")
int upStu(String name,int age,int tid);
----------------------------------------------------------
相当于 collection
@Results(value={
@Result(id=true,column="",property=""), // id 为true 表示 主键
@Result(column="",property=""), // column 与 property 与上面作用相同
@Result(column="",property="",many=@Many(select="")), // many 相当于 collection
})
@Select("SQL 语句")