标签:02 resultMap 基础 pojo 查询 private MyBatis te id
MyBatis 基础使用02
1.使用resultMap 处理查询一对多,多对一的关系:
1.1关于xml 中 返回类型解析:
resultType:
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中
resultMap
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
resultType:常用限定到pojo就行
1.2当出现一对多且实体类不对应数据库
一对多还是多对一核心就是:在实体类中和数据库不能匹配,把不能匹配的属性单独用resultMap 处理
<resultMap id="studentbyteacher" type="Teacher">
<result property="当前实体类中属性" column="数据库中对应的列"/>
<!--实体类中复杂类型单独处理
1.对象类型用: association
2.集合类型用: collection: javaType:指定属性类型,集合中的泛型用ofType -->
<collection property="目标实体类中包含的其他属性的名字" javaType="java.util.List" ofType="泛型" >
<result property="包含的其他实体类中属性" column="s_id"/>
</collection>
</resultMap>
pojo 一定要实现序列化
Teacher.java
public class Teacher implements Serializable {
private int tId;
private String tName;
private int tAge;
//一对多的关系
private List<Student> students;
}
public class Student implements Serializable {
private int sId;
private String sName;
private int sAge;
private int tTeacher;
}
Mapper.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">
<!--namespace绑定到mapper接口-->
<mapper namespace="com.hzh.mapper.TeacherMapper">
<!--开启二级缓存,当然需要序列化实体类-->
<cache/>
<!-- 一对多关系处理 -->
<!--方法一 按结果集查询-->
<resultMap id="studentbyteacher" type="Teacher">
<result property="tId" column="t_id"/>
<result property="tName" column="t_name"/>
<result property="tAge" column="t_age"/>
<!--实体类中复杂类型单独处理
1.对象类型用: association
2.集合类型用: collection: javaType:指定属性类型,集合中的用ofType -->
<collection property="students" javaType="java.util.List" ofType="student" >
<result property="sId" column="s_id"/>
<result property="sName" column="s_name"/>
</collection>
</resultMap>
<select id="selectByTeacherId" resultMap="studentbyteacher" >
select te.t_id,te.t_name,st.s_id,st.s_name
from student st,teacher te
where
st.t_teacher = te.t_id and te.t_id = #{te.t_id}
</select>
<!--方法二 子查询的方式-->
<select id="selectByTeacherId2" resultMap="studentbyteacher2">
select * from teacher
where t_id = #{t_id}
</select>
<resultMap id="studentbyteacher2" type="Teacher">
<collection property="students" javaType="java.util.List" ofType="Student"
select="getStudent" column="t_id"/>
</resultMap>
<select id="getStudent" resultType="Student">
select * from student where t_teacher = #{t_id}
</select>
</mapper>
2.动态sql模板
<select id="list" resultMap="BaseResultmap" >
SELECT * FROM `order`
<where>
<trim prefixOverrides="and">
<if test="orderNum != null and orderNum != ''">
and orderNum=#{orderNum}
</if>
<if test="payStatus != null and payStatus >= 0">
and payStatus=#{payStatus}
</if>
<if test="payMethod != null and payMethod >= 0">
and payMethod=#{payMethod}
</if>
<if test="userId != null and userId >= 0">
and userId=#{userId}
</if>
</trim>
</where>
ORDER BY orderId DESC
</select>
3.缓存
默认情况下,只启用了本地的会话缓存,它仅仅对一个会话中的数据进行缓存。 要启用全局的二级缓存,只需要在你的 SQL 映射文件中添加一行:
<cache/>
标签:02,resultMap,基础,pojo,查询,private,MyBatis,te,id 来源: https://www.cnblogs.com/syfw/p/14806260.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。