Mybatis进阶
介绍:Mybatis是一个非常优秀的持久层的框架,他支持自定义SQL,存储过程,以及高级映射。
1. 基础标签
在一些复杂的业务中,数据库的操作不止有简单的增、删、改、查和条件判断,在一些需要通过函数的传入值来做判断、选择的时候就需要可以自定义的SQL来解决。
1.1 SQL标签
用于定义查询的一些字段,SQL常量,该标签的好处是sql抽取,可以灵活的控制需要查询的字段,方便之后的引用。
<!-- 该sql标签一般用于查询的字段 -->
<sql id="selectParam"> satellite_id, satellite_name, satellite_holder</sql>
1.2 include标签
用于引入已经抽取出来的sql标签。
<!-- 用于 -->
<select id="testIf" resultType="Satellite">
SELECT
<include refid="selectParam"></include>
FROM
sate_satellite
</select>
2. 语法标签
2.1 if标签
该标签用于对查询语句做筛选的条件判断。
这是在java代码中调用的方法,param注解是用于传入参数的别名,在sql语句中使用到参数的地方就可以使用这个名字做区别。
List<SateSatellite> testIf(@Param("suppplementNum") double param1, @Param("dayBegin") double param2);
test中的字段是Satellite对象中存在的字段,然后再具体的语句还是需要使用数据库中的字段名字,#{}中的则为传入的参数,传入的参数根据param中取得别名作为区别。
<select id="testIf" resultType="Satellite">
SELECT
<include refid="selectParam"></include>
FROM
sate_satellite
where 1=1
<if test="suppplementNum != null and suppplementNum != ''">
AND supplement_num > #{suppplementNum}
</if>
<if test="dayBegin != null and dayBegin != ''">
AND day_begin = #{dayBegin}
</if>
</select>
2.2 foreach标签
该标签的应用场景为批量对数据库的操作,比如查询的时候,给定一个id的列表,需要从数据库中批量的将这组数据查询出来。
param,ids为取得别名,在后续的配置文件中会用到。
List<SateSatellite> selectBatchByIds( @Param("ids") List<String> ids);
<!--
collection: 这个地方填入的是取别名的列表
item: 代表将列表中的每一个对象取名为对应的字段,后续会用到该列表中每个对象的时候,就用该字段定义的名字去使用
open: 接在where后面的sql语句
close: 该SQL语句以该字符串结束,该值和open用于字符串的拼接
separator: 代表在这个循环中以什么来结束
下述的语句相当于Where id in (?,?,?)
-->
<select id="selectBatchByIds" resultType="Satellite">
SELECT
<include refid="selectParam"></include>
FROM
sate_satellite
where
<foreach collection="ids" item="item" open="satellite_id in (" close=")" separator=",">
#{item}
</foreach>
</select>
2.3 where标签
where标签的使用情景是为了减少上述的where 1= 1and 这个条件拼接的过程,在wehre标签中可以嵌套if标签、foreach查询标签。
<!--
resultType: 返回值的类型
parameterType: 传入的参数类型
-->
<select id="findByUser" resultType="user" parameterType="user">
select * from user
<where>
<if test="username!=null and username != '' ">
and username like #{username}
</if>
<if test="address != null">
and address like #{address}
</if>
</where>
</select>
3. 复杂查询
对于一些复杂的查询条件,包含字段的别名,对象的映射等,可以使用mybatis去实现。assiociation一般用于单个对象的关联,collection用于一对多的关系。
3.1 一对一关系
java部分的代码
// 实体类Dept
public class Dept {
private String deptId;
private String deptName;
}
// 实体类User
public class User {
private String username;
private String password;
private String id;
private Dept dept;
}
xml中的代码
<!--
resutMap: 将数据库中的表与对象之间的关系映射
id: javabean属性中对应的字段,为主键为id
property: javabean属性中对应的字段,对象中的普通属性
result: 为一般的属性
column: 表中对应的字段
association: 为一对一的关联关系,是对象中对象关联的桥梁
-->
<resultMap id="userMap" type="org.jeecg.modules.sate.sate_test_mybatis.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="password" column="password"></result>
<association property="dept" javaType="org.jeecg.modules.sate.sate_test_mybatis.domain.Dept">
<id column="id" property="deptId"></id>
<result column="dept_name" property="deptName"></result>
</association>
</resultMap>
<!--
在查询语句中引入上述的返回类型,根据上述的关系,将java实体类与mysql中的字段进行映射。
-->
<select id="selectUser" resultMap="userMap">
select
stu.id, stu.username, stu.password, std.dept_name
from
sate_test_user stu
join
sate_test_dept std on stu.dept_id = std.id
</select>
3.2 一对多关系
在java对象中表示的关系如图:
public class Student {
private String id;
private String studentName;
private String studentAge;
}
public class Teacher {
private String id;
private String teacherName;
private String teacherAge;
private List<Student> studentList;
}
resultMap是mybatis中最强大的标签之一,使用这个标签可以把一些非常复杂的表的数据关系,映射到一个resultMap中。
<resultMap id="batchStudent" type="org.jeecg.modules.sate.sate_test_mybatis.domain.Teacher">
<id property="id" column="id"></id>
<result property="teacherName" column="name"></result>
<result property="teacherAge" column="age"></result>
<collection property="studentList" ofType="org.jeecg.modules.sate.sate_test_mybatis.domain.Student">
<id property="id" column="id"></id>
<result property="studentName" column="name"></result>
<result property="studentAge" column="age"></result>
</collection>
</resultMap>
<select id="selectBatchUser" resultMap="batchStudent">
select
*
from
sate_test_teacher stt
join sate_test_student sts
on stt.id = sts.teacher_id
</select>