1. 动态增加
<!-- sql片段对应字段名 -->
<sql id="key">
<!-- 去掉最后一个, -->
<trim suffixOverrides=",">
<if test="id!=null">
id,
</if><if test="roleName!=null">
role_name,
</if><if test="title!=null">
title,
</if><if test="description!=null">
description,
</if><if test="sort!=null">
sort,
</if><if test="createTime!=null">
create_time,
</if><if test="updateTime!=null">
update_time,
</if><if test="version!=null">
version,
</if>
</trim>
</sql>
<!-- sql片段对应? -->
<sql id="value">
<!-- 去掉最后一个, -->
<trim suffixOverrides=",">
<if test="id!=null">
#{id},
</if><if test="roleName!=null">
#{roleName},
</if><if test="title!=null">
#{title},
</if><if test="description!=null">
#{description},
</if><if test="sort!=null">
#{sort},
</if><if test="createTime!=null">
#{createTime},
</if><if test="updateTime!=null">
#{updateTime},
</if><if test="version!=null">
#{version},
</if>
</trim>
</sql>
<!-- 增加角色 -->
<insert id="insertSysRole" keyColumn="id" keyProperty="id" parameterType="com.yqb.core.sys.domain.SysRole">
INSERT INTO sys_role (<include refid="key"/>,create_time) VALUES (<include refid="value"/>,NOW())
</insert>
2. 动态删除(http://blog.youkuaiyun.com/jpzhu16/article/details/52810747)
<!-- 动态删除操作 delete from students where id in(?,?,?);-->
<delete id="deleteStudent">
delete from students where id in
<!-- foreach用于迭代数组元素
open表示开始符号
close表示结束符号
seprator表示元素间的分割符
items表示迭代的数组
-->
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete>
3.动态修改
<!-- 更新角色 -->
<update id="updateSysRole" parameterType="com.yqb.core.sys.domain.SysRole">
UPDATE sys_role
<set>
update_time=NOW(),
<if test="roleName!=null">
role_name=#{roleName},
</if>
<if test="title!=null">
title=#{title},
</if>
<if test="description!=null">
description=#{description},
</if>
<if test="sort!=null">
sort=#{sort},
</if>
<if test="createTime!=null">
create_time=#{createTtime},
</if>
<if test="version!=null">
version=#{version},
</if>
</set>
WHERE id=#{id}
</update>
4. 动态查询
<!-- 动态查询SQL语句 -->
<select id="findAll" parameterType="map" resultMap="studentMap">
select id , name , sal
from students
<where>
<if test="pid!=null" >
and id = #{pid} <!-- #{}和之前使用c3p0的时候写的?含义是相同的 -->
</if>
<if test="pname!=null" >
and name = #{pname}
</if>
<if test="psal!=null" >
and sal = #{psal}
</if>
</where>
</select>
5. 模糊查询(http://blog.youkuaiyun.com/luqin1988/article/details/7865643)
a. sql中字符串拼接(常用)
SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');
b. 使用 ${...} 代替 #{...}
SELECT * FROM tableName WHERE name LIKE '%${text}%';
c. 程序中拼接
Java
// or String searchText = "%" + text + "%";
String searchText = new StringBuilder("%").append(text).append("%").toString();
parameterMap.put("text", searchText);
SqlMap.xml
SELECT * FROM tableName WHERE name LIKE #{text};
d. 大小写匹配查询
--或者是
SELECT * FROM TABLENAME WHERE LOWER(SUBSYSTEM) LIKE '%' || LOWER('jz') || '%'
6. 同时执行多条SQL(https://www.cnblogs.com/yuananyun/p/5445181.html)
a、修改数据库连接参数加上allowMultiQueries=true,如:
hikariConfig.security.jdbcUrl=jdbc:mysql://xx.xx.xx:3306/xxxxx?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
b、直接写多条语句,用“;”隔开即可
<delete id="deleteUserById" parameterType="String">
delete from sec_user_role where userId=#{id};
delete from sec_user where id=#{id};
</delete>
7、if标签判断字符串(https://www.cnblogs.com/westward/p/6910856.html)
因为mybatis映射文件,是使用的ognl表达式,所以在判断字符串sex变量是否是字符串Y的时候,
<if test="sex == 'Y'.toString()">;
<if test='sex == "Y"'>
不能使用<if test="sex == 'Y'">,因为mybatis会把'Y'解析为字符,java是强类型语言,所以不能这样写。