记Mybatis动态sql

2022-10-22,,

目录
记MyBatis动态SQL
1.< SQL >标签
2.< if >标签
3.分支标签
1.第一种:用在查询条件上用choose-when;otherwise可不要
2. 第二种case-when:用在查询结果上
3.case-when 的特殊用法:行转列:
< foreach >标签
遍历list
遍历map
关系映射 resultMap
一般情况:
list嵌套等
嵌套查询
补充:collection 与assocation的异同
其他重要sql笔记
concat拼接:
时间比较:

记MyBatis动态SQL

写在前面:

在此记录下项目中能够经常遇到的MyBatis动态SQL,因为经常容易忘,所以索性就给记录下来,免得每次都要百度搜索好久,需要使用的朋友自取,但是别忘了点赞哦

在此仅作部分说明和XML文件演示:

1.< SQL >标签

又叫 共性抽取

	<!--<sql>一般放在上面或最底下-->
<sql id="person">
id
,name,sex
</sql>
<select id="queryPerson" resultType="Person">
SELECT
<!--在此调用sql的id-->
<include refid="person"></include>
FROM person
</select>

2.< if >标签

在test中写判断条件,< if >中只能用and不能用&&,并且,避免连接时and/or出错,上面最好用 < where >或者 where 1=1拼接

< where >可以代替where 1=1 并且能自动去除掉第一个and

<select id="queryPerson" resultType="Person"> 
SELECT * FROM Person WHERE 1=1
<if test="name!=null and param1!=''">
and name=#{name}
</if>
</select>

3.分支标签

即分支语法,与switch case ,if-else相近

1.第一种:用在查询条件上用choose-when;otherwise可不要

 <select id="queryPerson" resultType="Person">
SELECT * FROM person
<where>
<choose>
<when test="name !=null">
name = #{name}
</when>
<when test="age !=null">
age = #{age}
</when>
<otherwise>
sex = #{sex}
</otherwise>
</choose>
</where>

2. 第二种case-when:用在查询结果上

 <select id="queryPerson" resultType="Person">
SELECT id,name,
<!--sex可省-->
( case sex
when 1 then "男"
when 2 then "女"
ELSE "中性人" end) as sex
FROM person
</select>

3.case-when 的特殊用法:行转列:

原本:

id name subject score
1 张三 JAVA 90
2 张三 VUE 66
3 李四 VUE 80
4 王五 XML 70

转换后:

name JAVA VUE XML
张三 90 66 null
李四 null 80 null
张三 null null 70
SELECT
name,
SUM(CASE subject WHEN 'JAVA' THEN score end) as 'JAVA',
SUM(CASE subject WHEN 'VUE' THEN score end) as 'VUE',
SUM(CASE subject WHEN 'XML' THEN score end) as 'XML'
FROM prson
GROUP BY name

< foreach >标签

foreach标签一般用来遍历查询或者批量操作

注:

collection中写列表名,

open表示语句前面用( 包含,close表示后面用 )包含,也可自行添加括号

separator表示中间用 隔开,

item表示每个list中的字符或者对象名,如果是array数组,item.id可写为id

index 表示下标,可省略

遍历list

1.批量查询列表

<select id="queryList" resultType="Person">
SELECT *
FROM person
WHERE person.id
IN
<foreach collection="list" open="(" close=")"separator="," item="item">
#{item.id}
</foreach>
</select>
    批量插入列表:
<insert id="insertBatch">
INSERT INTO person (id,name)
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.id},
#{item.name}
)
</foreach>
</insert>

3.批量修改列表

<update id="updateBatch">
UPDATE person
SET age = '18'
WHERE
sex = '男'
AND name IN
<foreach collection="nameList" item="item" index="index" open="(" close=")" separator=",">
#{item.name}
</foreach>
</update>

4.批量删除列表

<delete id="deleteBatch">
DELETE FROM person
WHERE id IN
<foreach collection="list" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
</delete>

遍历map

1.map 的entrySet()

<insert id="insertPersonMap" parameterType="java.util.Map">
INSERT INTO person(id, name)
VALUES
<foreach collection="map.entrySet()" open="(" separator="," close=")" index="key" item="val">
#{key}, #{val}
</foreach>
</insert>

2.分别遍历

<insert id="insertPersonMap" parameterType="java.util.Map">
INSERT INTO person
<foreach collection="map.keys" item="key" open="(" separator="," close=")">
#{key}
</foreach>
VALUES
<foreach collection="param.value" item="val" open="(" separator="," close=")">
#{val}
</foreach>
</insert>

3.map中值为list[map]的遍历:

参数形式:

{"k1":"v1",
"k2":[
{"a1":"b1"},
{"a2":"b2"}
]
}

遍历方式(双重):

 <foreach collection="Map" item="item" separator="," >
<foreach collection="item.entrySet()" separator="," index="key" item="val">
#{key} #{val}
</foreach>
</foreach>

关系映射 resultMap

一般情况:

  <!--使用resultMap手动映射-->
<resultMap id="personMap" type="person">
<!--主键绑定-->
<id property="id" column="person.id"></id>
<!--非主键绑定-->
<result property="name" column="person.name"></result>
</resultMap>

list嵌套等

<resultMap id="ListMap" type="路径.person">
<result column='id' property='id' />
<result column='name' property='name' />
<collection property='实体中的list名' resultMap='listResultMap'/>
</resultMap> <resultMap id="listResultMap" type="list中装的数据类型路径">
<result column='id' property='id' />
<result column='name' property='name' />
</resultMap>

嵌套查询

需要返回list的同时还要返回list中对象包含的list

<!--设定映射关系-->
<resultMap id="personMap" type="com.personList">
<id column="ID" property="id"/>
<result column="NAME" property="name"/>
<result column="TYPE" property="type"/>
<collection property="scoreList" select="queryscoreList" column="{personId=id}"></collection>
</resultMap>
<resultMap id="scoreListMap" type="com.scoreList">
<id column="ID" property="id"/>
<result column="PERSON_ID" property="personId"/>
<result column="SCORE" property="score"/>
</resultMap>
<!--编写语句-->
<select id="selectPersonList" resultMap="personMap">
SELECT
ID,
NAME`,
FROM person
</select>
<!--因为关联了映射关系,下面语句会自动执行并添加进list中-->
<select id="queryScoreList" resultMap="scoreListMap">
SELECT score from score表
</select>

补充:collection 与assocation的异同

异:

关联-association

集合-collection

即assocation是一对一的关系,collection是一对多的关系

< collection >比< association >多了一个ofType属性,

即collection集合中单个元素的javaType属性

< collection >的javaType属性是继承了Collection接口的list或set等java集合属性

同:

< collection >和< association >标签都属于嵌套结果集,

处理逻辑基本相同

其他重要sql笔记

concat拼接:

两个字段拼接起来搭建新的一列

或合并字符(例如:id,%)或字符串

select
concat(id,name) as id_name
from person

时间比较:

符号转换:避免出现奇怪的bug

< <= > >= & ' "
&lt ; &lt ;= &gt ; &gt ;= &amp ; &apos ; &quot ;
(unix_timestamp(START_TIME) &lt;=unix_timestamp(NOW()))

记Mybatis动态sql的相关教程结束。

《记Mybatis动态sql.doc》

下载本文的Word格式文档,以方便收藏与打印。