mybatis--动态SQL

 

一、动态SQL基础介绍

基于OGNL表达式

完成多条件查询等逻辑实现

用于实现动态SQL的元素主要有

  • if
  • trim
  • where
  • set
  • choose(when、otherwise)
  • foreach

 


二、if

问题:当传入用户角色参数为空的时候,检索结果为空?

正确结果 所有用户角色下的用户数据

原因

select * from smbms_user u,smbms_role r 
where u.userRole = r.id and u.userName like  CONCAT (‘%’, ‘’, ‘%’) 
and  u.userRole = null;

如何处理

<select id=“getUserList” resultMap=“User”>
	select * from smbms_user where
         <if test=“userName!=null”>
		userName like CONCAT (‘%’,#{userName},’%’)
         </if>
	   <if test=“userRole!=null”>
		and userRole = #{userRole}
         </if>
</select>

 

 

三、where

问题:当只传入参数:用户角色,而不传入参数:用户名称的时候,控制台报SQL异常错误?

正确结果 所有用户角色下的用户数据

原因

select * from smbms_user  where and  userRole = ?

如何处理

<select id="getUserList" resultType="User">
		select * from smbms_user 
			<where>
				<if test="userName != null and userName != ''">
					and userName like CONCAT ('%',#{userName},'%') 
				</if>
				<if test="userRole != null">
					and userRole = #{userRole}
				</if>
			</where>
	</select>
  • 简化SQL语句中where条件判断
  • 智能处理and和or

 

四、set

问题:更新用户表数据时,若某个参数为null时,会导致更新错误

正确结果 若某个参数为null,则不需要更新,保持数据库原值

原因

update  smbms_user set username=?

如何处理

  • if
  • set
<!-- 修改用户信息 -->
	<update id="modify" parameterType="User">
		 update smbms_user 
			 <set>
		 		<if test="userCode != null">userCode=#{userCode},</if>
				<if test="userName != null">userName=#{userName},</if>
				<if test="userPassword != null">userPassword=#{userPassword},</if>
				<if test="gender != null">gender=#{gender},</if>
				<if test="birthday != null">birthday=#{birthday},</if>
				<if test="phone != null">phone=#{phone},</if>
				<if test="address != null">address=#{address},</if>
				<if test="userRole != null">userRole=#{userRole},</if>
				<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
				<if test="modifyDate != null">modifyDate=#{modifyDate}</if>
			 </set>
			 where id = #{id}
	</update>

 

五、trim

if+trim

  • 使用if+trim替代if+set进行更新用户表数据,效果一样
<!-- 修改用户信息 -->
	<update id="modify" parameterType="User">
		 update smbms_user 
			 <trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
		 		<if test="userCode != null">userCode=#{userCode},</if>
				<if test="userName != null">userName=#{userName},</if>
				<if test="userPassword != null">userPassword=#{userPassword},</if>
				<if test="gender != null">gender=#{gender},</if>
				<if test="birthday != null">birthday=#{birthday},</if>
				<if test="phone != null">phone=#{phone},</if>
				<if test="address != null">address=#{address},</if>
				<if test="userRole != null">userRole=#{userRole},</if>
				<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
				<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
			 </trim>
	</update>

 

<select id="getUserList" resultType="User">
		select * from smbms_user 
			<trim prefix="where" prefixOverrides="and | or">
				<if test="userName != null and userName != ''">
					and userName like CONCAT ('%',#{userName},'%') 
				</if>
				<if test="userRole != null">
					and userRole = #{userRole}
				</if>
			</trim>
	</select>

 

六、foreach

 

 

    <!-- 根据用户角色列表,获取该角色列表下用户列表信息-foreach_array -->
	<select id="getUserByRoleId_foreach_array" resultMap="userMapByRole">
		select * from smbms_user where userRole in 
			<foreach collection="array" item="roleIds" open="(" separator="," close=")">
				#{roleIds}
			</foreach>
	</select>
    <!-- 根据用户角色列表,获取该角色列表下用户列表信息-foreach_list -->
	<select id="getUserByRoleId_foreach_list" resultMap="userMapByRole">
		select * from smbms_user where userRole in 
			<foreach collection="list" item="roleList" open="(" separator="," close=")">
				#{roleList}
			</foreach>
	</select>

 

    <!-- 根据用户角色列表(单参数),获取该角色列表下用户列表信息-foreach_map -->
	<select id="getUserByRoleId_foreach_map" resultMap="userMapByRole">
		select * from smbms_user where userRole in 
			<foreach collection="rKey" item="roleMap" open="(" separator="," close=")">
				#{roleMap}
			</foreach>
	</select>

 

七、choose(when、otherwise)

	<!-- 查询用户列表(choose) -->
	<select id="getUserList_choose" resultType="User">
		select * from smbms_user where 1=1
			<choose>
				<when test="userName != null and userName != ''">
					and userName like CONCAT ('%',#{userName},'%')
				</when>
				<when test="userCode != null and userCode != ''">
					and userCode like CONCAT ('%',#{userCode},'%')
				</when>
				<when test="userRole != null">
					and userRole=#{userRole}
				</when>
				<otherwise>
					<!-- and YEAR(creationDate) = YEAR(NOW()) -->
					and YEAR(creationDate) = YEAR(#{creationDate})
				</otherwise>
			</choose>
	</select>

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-plus是在Mybatis基础上进行封装的一个框架,它简化了平时开发过程中对常用接口的调用,可以省去一些繁琐的操作。然而,对于一些更为复杂的查询,Mybatis-plus可能无法满足需求,此时就需要我们自定义SQL语句来实现。通过在入口类的MybatisSqlSessionFactoryBuilder#build方法中注入mybatis-plus自定义的动态配置xml文件,可以实现自定义SQL语句和动态SQL的功能。具体的实现步骤如下: 1. 在应用启动时,在入口类的MybatisSqlSessionFactoryBuilder#build方法中将mybatis-plus的自定义动态配置xml文件注入到Mybatis中。 2. 在自定义的动态配置xml文件中,可以使用各种Mybatis-plus提供的方法来实现动态SQL的功能,比如IF标签、CHOOSE标签、FOREACH标签等。 3. 在自定义SQL语句中,可以结合Mybatis-plus的Wrapper类来实现条件查询,例如使用LambdaQueryWrapper来构建查询条件。 总结起来,Mybatis-plus提供了简化开发的接口,但对于一些更为复杂的查询,仍然需要我们自定义SQL语句和动态SQL来实现。通过注入自定义的动态配置xml文件,并结合Mybatis-plus提供的方法和Wrapper类,可以实现更加灵活和高效的数据查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis-plus/mybatis 自定义 sql 语句、动态 sql](https://blog.csdn.net/CREATE_17/article/details/109117091)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Mybatis Plus实现动态SQL语句的原理,你知道吗?](https://blog.csdn.net/weixin_38405253/article/details/119880820)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值