MyBatis入门 动态SQL
动态SQL的基本元素:
if: 单条件分支判断
choose, when, otherwise:多条件分支判断
trim, set, where:用于处理SQL拼装问题
foreach:循环语句
bind:定义一个上下文变量
test:用于判断条件是否成立
if条件判断语句:
当角色名称不为空时,根据角色名称查找对象
<resultMap id="roleResultMap" type="com.ssm.chapter6.pojo.Role"> <id column="roleNo" property="role_no" /> <id column="role_name" property="roleName" /> <id column="note" property="note" /> </resultMap> <select id="findRole1" parameterType="string" resultMap="roleResultMap"> select role_no, role_name, note from t_role where 1=1 <if test="roleName != null and roleName !=''"> and role_name like concat('%', #{roleName}, '%') </if> </select>
choose,when,otherwise多条件判断语句:
相当于if-else-else if
<select id="findRole2" parameterType="role" resultMap="roleResultMap"> select role_no, role_name, note from t_role where 1=1 <choose> <when test="roleNo != null and roleNo !=''"> AND role_no = #{roleNo} </when> <when test="roleName != null and roleName !=''"> AND role_name like concat('%', #{roleName}, '%') </when> <otherwise> AND note is not null </otherwise> </choose> </select>
where语句和if语句拼接
<select id="findRole3" parameterType="role" resultMap="roleResultMap"> select role_no, role_name, note from t_role <where> <if test="roleName != null and roleName !=''"> and role_name like concat('%', #{roleName}, '%') </if> <if test="note != null and note !=''"> and note like concat('%', #{note}, '%') </if> </where> </select>
当where元素里面的条件成立时,才会加入where这个SQL关键字到组装的SQL里面。
使用trim删掉特殊的元素
比如and,or
<select id="findRole4" parameterType="string" resultMap="roleResultMap"> select role_no, role_name, note from t_role <trim prefix="where" prefixOverrides="and"> <if test="roleName != null and roleName !=''"> and role_name like concat('%', #{roleName}, '%') </if> </trim> </select>
trim元素意味着要去掉一些特殊的字符,prefix代表的是if语句的前缀,当if语句成立时,才会加到语句的前面,而 prefixOverrides 代表的是当 if 语句成立时,去掉 if 语句中的and字符,suffixOverrides是去掉后缀,比如逗号。
使用set更新角色的数据
这样我们就可以选择要更新的字段,而不用每个字段都更新,而且set语句在遇到逗号时,会自动把对应的逗号去掉(最后一个逗号去掉)。
<update id="updateRole" parameterType="role"> update t_role <set> <if test="roleName != null and roleName !=''"> role_name = #{roleName}, </if> <if test="note != null and note != ''"> note = #{note} </if> </set> where role_no = #{roleNo} </update>
使用foreach遍历集合
它能够很好的支持数组和list,set,它往往用于SQL中的in关键字。
<select id="findRoleByNums" resultMap="roleResultMap"> select role_no, role_name, note from t_role where role_no in <foreach item="roleNo" index="index" collection="roleNoList" open="(" separator="," close=")"> #{roleNo} </foreach> </select>
collection 配置的 roleNoList 是传递进来的参数名称,它可以是一个数组,List,Set,等集合。
item 配置的是循环中的当前的元素。
index 配置的是当前元素在集合的位置下标。
open 和 close 配置的是以什么符号将这些集合元素包装起来。
separator 是各个元素的间隔符。
使用bind元素自定义一个上下文变量
在进行模糊查询的时候,如果是MYSQL数据库,常常用到的是一个concat,它用 ’%' 和参数相连,而在Oracle数据库中用 || ,这样SQL就需要提供两种形式去实现,但是有了bind元素,就不必使用数据库的语言,而是使用MyBatis的动态SQL即可完成。
<select id="findRole5" parameterType="string" resultMap="roleResultMap"> <bind name="pattern" value="'%' + _parameter + '%'" /> SELECT role_no, role_name, note FROM t_role where role_name like #{pattern} </select>
这里的 _parameter 代表的是传递进来的参数,它和通配符一起组成了pattern,然后就可以在select语句中使用这个变量进行模糊查询了。无论是MYSQL还是Oracle都可以使用这样的语句,提高了代码的移植性。
使用bind传递多个参数
首先定义接口方法
public List<RoleBean> findRole(@Param("roleName") String roleName, @Param("note") String note);
定义映射文件
<select id="findRole6" resultMap="roleResultMap"> <bind name="pattern_roleName" value="'%' + roleName + '%'" /> <bind name="pattern_note" value="'%' + note + '%'" /> SELECT role_no, role_name, note FROM t_role where role_name like #{pattern_roleName} and note like #{pattern_note} </select>
使用多个条件进行模糊查询。
REF
https://blog.csdn.net/qq_37745636/article/details/99584850
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 分享4款.NET开源、免费、实用的商城系统
2021-02-07 Support Vector Regression(SVR) 资料
2015-02-07 mysql删除重复记录
2015-02-07 python去掉行尾的换行符
2011-02-07 理解商集
2010-02-07 安装 IE8 Internet Explorer 8.0
2010-02-07 关于Microsoft Virtual PC 2007安装使用