我是靠谱客的博主 超级汽车,这篇文章主要介绍一篇文章搞懂Mybatis SQL动态处理Mybatis SQL动态处理,现在分享给大家,希望可以做个参考。

文章目录

  • Mybatis SQL动态处理
    • Where 、if
    • choose 、 when、otherwise
    • set
    • trim
    • foreach
    • bind
    • sql片段
    • 主键自增回填
    • 模糊查询
    • resultMap 映射处理
      • 单表映射关系处理
      • 一对一映射关系处理
      • 一对多映射关系处理

Mybatis SQL动态处理

Where 、if

  • where 标签:相当于sql语法中的where

  • if 判断标签

    • test 判断条件 ,满足条件,执行if标签中sql语句
复制代码
1
2
3
4
5
6
7
8
9
10
<select id="findEmpByCondition" resultType="emp"> select * from emp <where> <if test="empno != null"> and empno= #{empno} </if> </where> </select>

choose 、 when、otherwise

  • choose选择标签

  • when : 相当于 java 中case语法

  • otherwise :when标签都不满足,执行otherwise标签中内容

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<select id="findEmpByCondition2" resultType="emp"> select * from emp <where> <choose> <when test="empno != null"> and empno= #{empno} </when> <when test="ename != null and ename != ''"> and ename= #{ename} </when> <when test="job != null and job != ''"> and job= #{job} </when> <when test="mgr != null "> and mgr= #{mgr} </when> <when test="hiredate != null "> and hiredate= #{hiredate} </when> <when test="sal != null"> and sal= #{sal} </when> <when test="comm != null "> and comm =#{comm} </when> <when test="deptno != null "> and deptno= #{deptno} </when> <otherwise> AND featured = 1 </otherwise> </choose> </where> </select>

set

set 代替sql中set 语法

复制代码
1
2
3
4
5
6
7
8
9
10
11
<update id="updateAuthorIfNecessary"> update Author <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id} </update>

trim

trim标签 增加前缀 、后缀 , 删除前缀、后缀

trim标签中属性:

  • prefix 增加前缀

  • suffix 增加后缀

  • prefixOverrides 删除前缀

  • suffixOverrides 删除后缀

复制代码
1
2
3
4
5
6
7
8
9
10
11
<update id="updateAuthorIfNecessary"> update Author <trim prefix="SET" suffixOverrides=","> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio},</if> </trim> where id=#{id} </update>

foreach

  • coollection 要迭代的集合,如:list 、 set 、map 、array
  • item 迭代临时变量值
  • index 当前迭代序号
  • open 以什么开头后开始遍历
  • close 以什么后结束遍历
复制代码
1
2
3
4
5
6
7
8
9
<delete id="delectById"> delete from project where project_id in <foreach collection="array" item="project_id" index="index" open="(" separator="," close=")"> #{project_id} </foreach> </delete>

bind

bind表达式以外创建一个变量,并将其绑定到当前的上下文

复制代码
1
2
3
4
5
6
7
8
9
10
<select id="findAll2" resultMap="selectAll3"> SELECT * FROM orders <where> <if test="order_name !=null and order_name != ''"> <bind name="tn" value="'%'+order_name+'%'" /> order_name like #{tn} </if> </where> </select>

sql片段

将sql语句共同的部分抽出来形成一个独立的片段,哪里需要该片段就引用该片段

复制代码
1
2
3
4
5
6
7
8
9
<sql id="empColumns"> empno,ename,job,mgr,hiredate,sal,comm,deptno </sql> <sql id="baseEmpQuery"> select <include refid="empColumns"/> from emp </sql>

主键自增回填

方式1

  • useGeneratedKeys=“true” 表示要使用自增的主键

  • keyProperty=“deptno” 表示把自增的主键赋给JavaBean的哪个成员变量。

    使用对象get方法可以获取主键

复制代码
1
2
3
4
5
6
<insert id="addDept" parameterType="dept" useGeneratedKeys="true"keyProperty="deptno" > insert into dept values(default,#{dname},#{loc}) </insert>

方式2

  • order:取值AFTER|BEFORE,表示在新增之后|之前执行selectKey标签中的SQL命令
  • keyProperty: 执行select @@identity后结果填充到哪个属性中
  • resultType: 结果类型
复制代码
1
2
3
4
5
6
7
8
<insert id="addDept2" parameterType="dept"> <selectKey order="AFTER" keyProperty="deptno" resultType="int"> select @@identity </selectKey> insert into dept values(null,#{dname},#{loc}) </insert>

模糊查询

方式1: 使用concat() 函数,对模糊条件进行拼接

复制代码
1
2
3
4
<select id="getByName" parameterType="string" resultType="emp"> select * from emp where ename like concat( '%',#{ename},'%') </select>

方式2:使用bind标签

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="selectMore" resultMap="BaseResultMap2"> select * from user <where> <if test="name!=null and name!=''"> <bind name="li" value="'%'+project.projectName+'%'"/> name like #{li} </if> <if test="age!=null and age!=''"> <bind name="age" value="'%'+age+'%'"/> and age=#{age} </if> </where> </select>

resultMap 映射处理

单表映射关系处理

  • id 表示表主键字段
  • result 表示表普通字段

属性

  • type 返回类型

  • property 对应javaBean 属性

  • column 对应表字段 或 对映射表

  • javaType 表映射对象类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
<resultMap id="empMap" type="emp"> <id property="empno" column="empno"></id> <result property="ename" column="ename"></result> <result property="job" column="job"></result> <result property="sal" column="sal"></result> <result property="hiredate" column="hiredate"></result> <result property="mgr" column="mgr"></result> <result property="comm" column="comm"></result> <result property="deptno" column="deptno"></result> </resultMap>

一对一映射关系处理

标签

  • id 表示表主键字段
  • result 表示表普通字段
  • association 表连接一对一关系映射

属性

  • type 返回类型

  • property 对应javaBean 属性

  • column 对应表字段 或 对映射表

  • javaType 表映射对象类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!--使用resultMap定义一对一映射关系--> <resultMap id="empJoinDept" type="emp"> <!--处理emp原有8个属性 不能省略--> <id property="empno" column="empno"></id> <result property="ename" column="ename"></result> <result property="job" column="job"></result> <result property="sal" column="sal"></result> <result property="hiredate" column="hiredate"></result> <result property="mgr" column="mgr"></result> <result property="comm" column="comm"></result> <result property="deptno" column="deptno"></result> <!--将deptno dname loc 映射景dept属性 association 处理一对一映射关系 property 给实体类属性赋值 javaType 要映射的属性是哪个类 --> <association property="dept" javaType="dept" > <!--处理一对一属性映射--> <id property="deptno" column="deptno"></id> <result property="dname" column="dname"></result> <result property="loc" column="loc"></result> </association> </resultMap
复制代码
1
2
3
4
5
6
<select id="findByJoinDept" resultMap="empJoinDept"> select * from emp e left outer join dept d on e.deptno=d.deptno where empno=#{empno} </select>

一对多映射关系处理

标签

  • id 表示表主键字段
  • result 表示表普通字段
  • collection 表连接多对多关系映射

属性

  • type 返回类型

  • property 对应javaBean 属性

  • column 对应表字段 或 对映射表

  • javaType 表映射对象类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<resultMap id="empJoinPs" type="emp" > <id property="empno" column="empno"></id> <result property="ename" column="ename"></result> <result property="job" column="job"></result> <result property="sal" column="sal"></result> <result property="hiredate" column="hiredate"></result> <result property="mgr" column="mgr"></result> <result property="comm" column="comm"></result> <result property="deptno" column="deptno"></result> <collection property="projectrecords" ofType="projectrecords"> <id property="empno" column="empno"></id> <id property="pid" column="pid"></id> </collection> </resultMap>
复制代码
1
2
3
4
5
6
7
<select id="getEmpPs" resultMap="empJoinPs"> select * from emp e join projectrecords s on e.empno=s.empno where e.empno=#{empno} </select>

最后

以上就是超级汽车最近收集整理的关于一篇文章搞懂Mybatis SQL动态处理Mybatis SQL动态处理的全部内容,更多相关一篇文章搞懂Mybatis内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部