MyBatis之动态sql,插件

动态SQL

  1. 基于OGNL表达式

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

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

    • if

    • trim

    • where

    • set

    • choose(when、otherwise)

    • foreach

if

  • 改造查询用户信息列表的演示示例,增加查询条件 用户角色(根据角色id查询) 用户名称(模糊查询)
    <!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
    <resultMap type="User" id="userList">
        <result property="id" column="id"/>
        <result property="userCode" column="userCode"/>
        <result property="userName" column="userName"/>
        <result property="phone" column="phone"/>
        <result property="birthday" column="birthday"/>
        <result property="gender" column="gender"/>
        <result property="userRole" column="userRole"/>
        <result property="userRoleName" column="roleName"/>
    </resultMap>
    
    <!-- 查询用户列表 -->
    <select id="getUserList" resultMap="userList">
        select u.*,r.roleName from smbms_user u,smbms_role r where u.userRole = r.id
            <if test="userRole != null">
                and u.userRole = #{userRole}
            </if>
            <if test="userName != null and userName != ''">
                and u.userName like CONCAT ('%',#{userName},'%') 
            </if>
    </select>

where

  1. 简化SQL语句中where条件判断

  2. 智能处理and和or

<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 = #
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值