前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在mybatis xml文件中定义局部变量?

如何在mybatis xml文件中定义局部变量?

作者头像
索码理
发布2022-12-28 14:59:42
2.6K0
发布2022-12-28 14:59:42
举报
文章被收录于专栏:索码理索码理

mybatis定义全局变量只需要配置一下即可,那如何在mybatis xml文件中定义局部变量呢?这就需要使用<bind>标签了。

「bind」标签允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。通俗来讲它就是声明了一个局部变量,它的优先级是高于其他语句的。

格式:

代码语言:javascript
复制
<bind name="name" value="value"/>
  • name:定义的变量名称
  • value:value是一个具体的值,它可以是入参,也可以是一个表达式,比如:判断条件

举例

UserInfo实体类

代码语言:javascript
复制
public class UserInfo {
    private Long id;
    private String gender;
    private String name;
    private Date createDate;
}

不使用「bind」标签的SQL语句

代码语言:javascript
复制
<select id="bindTest" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from users
    <where>
        <choose>
            <when test="minId != null and minId > 0 and maxId != null and maxId > 0">
                and id between #{minId} and #{maxId}
            </when>
            <otherwise>
                <if test="minId != null and minId > 0">
                    and id &gt;= #{minId}
                </if>
            </otherwise>
        </choose>
        <if test="username != null and username.trim() != ''">
            and name like concat('%' , #{username} , '%')
        </if>
    </where>
</select>

测试类

代码语言:javascript
复制
@Test
public void testBind(){
    SqlSession sqlSession = sqlSessionFactory.openSession();
    UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
    List<UserInfo> userInfos = userInfoMapper.bindTest(1L , 10L, "0");
    System.out.println(userInfos);
}

运行测试类,打印的SQL语句:

代码语言:javascript
复制
sql=select Id, gender , name , create_date from users WHERE id between 1 and 10 and name like concat('%' , '0' , '%')

修改成使用「bind」标签的SQL语句:

代码语言:javascript
复制
<select id="bindTest" resultMap="BaseResultMap">
   <bind name="minIdFlag" value="minId != null and minId > 0"/>
    <bind name="maxIdFlag" value="maxId != null and maxId > 0"/>
    <bind name="usernameLike" value="'%' + username + '%'"/>
    
    select
    <include refid="Base_Column_List" />
    from users
    <where>
        <choose>
            <when test="minIdFlag and maxIdFlag">
                and id between #{minId} and #{maxId}
            </when>
            <otherwise>
                <if test="minIdFlag">
                    and id &gt;= #{minId}
                </if>
            </otherwise>
        </choose>
        <if test="username != null and username.trim() != ''">
            and name like #{usernameLike}
        </if>
    </where>
</select>

运行测试类,打印SQL如下:

代码语言:javascript
复制
sql=select Id, gender , name , create_date from users WHERE id between 1 and 10 and name like '%0%'

对比一下可以发现使用<bind>标签前后的sql语句都是一样的,也说明<bind>标签替换成功。

注意事项

  • 「bind」标签的value不能为null
  • 「bind」标签name最好不要使用点·进行赋值操作,如果使用的话直接取值是没问题的, 但是如果在<if><when>等标签中使用时会报no getter xx异常。

本篇简单介绍了一下<bind>标签的使用,希望对你有用。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 索码理 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 举例
  • 注意事项
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档