1. 自动映射【数据库字段名与实体类的属性名一致】
<select id="selectList" resultType="cn.bjsxt.pojo.User"> select id,name,pwd,age from t_user </select> |
2. 使用resultMap设置映射结果集【数据库字段名与实体类的属性名不一致】
<!-- resultMap:定义结果集映射 id:代表结果集的唯一标记 type:结果集的类型,类的全路径名,或者别名 --> <resultMap type="cn.bjsxt.pojo.User" id="userone"> <!-- id:用于设置主键字段于实体类属性的映射关系 --> <id property="id" column="id" /> <!-- result:用于设置普通字段与实体类属性的映射关系--> <result property="uname" column="name" /> <result property="pwd" column="pwd" /> <result property="age" column="age" /> </resultMap> <select id="selectList" resultMap="userone"> select id,name, pwd,age from t_user </select> |
3. Mybatis查询方式
a) 一对一关系, 查询学生所在的班级。
第一种方式
<?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"> <!-- namespace:SQL标签命名空间 --> <mapper namespace="cn.bjsxt.mapper.StudentMapper"> <select id="selectList" resultType="cn.bjsxt.pojo.Student"> SELECT s.id,s.name ,s.gender ,s.age ,c.id as 'classes.cid',c.name as 'classes.cname',c.beginTime as 'classes.beginTime' from t_student s LEFT JOIN t_classes c ON c.id = s.cid </select> </mapper> |
第二种方式
<resultMap type="cn.bjsxt.pojo.Student" id="stuMap"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="age" column="age"/> <!-- 映射的实体类 property:关系数据属性名 javaType:关系数据属性类型 --> <association property="classes" javaType="cn.bjsxt.pojo.Classes"> <id property="cid" column="id"/> <result property="cname" column="name"/> <result property="beginTime" column="beginTime"/> </association> </resultMap> <select id="selectList" resultMap="stuMap"> SELECT s.id,s.name ,s.gender ,s.age, c.id,c.name,c.beginTime from t_student s LEFT JOIN t_classes c ON c.id = s.cid </select> |
b) 一对多关系:查询所有班级,并查询班级中所有学生集合
i. 一次访问数据库的方式
<!-- 1对多关系 --> <resultMap type="cn.bjsxt.pojo.Classes" id="clsMap"> <id property="cid" column="cid"/> <result property="cname" column="cname"/> <result property="beginTime" column="beginTime"/> <!-- 定义集合关系 property:关系属性名 javaType:关系对象属性:list:ArrayList ofType:集合的泛型 --> <collection property="students" javaType="java.util.ArrayList" ofType="cn.bjsxt.pojo.Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="gender" column="gender"/> <result property="age" column="age"/> </collection> </resultMap>
<select id="selectList" resultMap="clsMap"> SELECT s.id,s.name ,s.gender ,s.age, c.id as cid ,c.name as cname,c.beginTime from t_student s right JOIN t_classes c ON c.id = s.cid </select> 注意:id冲突问题 |
c) N+1次访问数据方式
<!-- 多次访问数据库 --> <resultMap type="cn.bjsxt.pojo.Classes" id="clsMap1"> <id property="cid" column="id"/> <result property="cname" column="name"/> <result property="beginTime" column="beginTime"/> <!-- 定义集合关系 如何查询班级中的学生 添加查询id:select 添加id值:column --> <collection property="students" javaType="java.util.ArrayList" ofType="cn.bjsxt.pojo.Student" select="selStu" column="id"> </collection> </resultMap> <!-- 查询班级信息 --> <select id="selectList1" resultMap="clsMap1"> select id , name , begintime from t_classes </select> <!-- 查询学生信息 --> <select id="selStu" resultType="cn.bjsxt.pojo.Student"> select * from t_student where cid = #{id} </select> |