Mybatis进阶-动态sql

Mybatis进阶-动态sql

1.动态 SQL 之标签for

根据实体类的不同取值,使用不同的 SQL 语句来进行查询。比如在 id 如果不为空时可以根据 id 查询, 如果 username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。

持久层 Dao 接口

/**
 * 根据传入参数条件查询
 * @param user 查询条件:有可能有用户名,也有没用户名
 * @return
 */
List<User> findUserByCondition(User user);

持久层 Dao 映射配置

<!-- 多条件组合查询-->
    <select id="findUserByCondition" parameterType="string" resultMap="userMap">
        select * from user where 1=1
        <if test="userName != null">
            and username = #{userName}
        </if>
        <if test="userSex != null">
            and sex = #{userSex}
        </if>
    </select>

测试

    @Test
    public void testFindUserByCondition(){
        User u = new User();
        u.setUserName("老王");
        u.setUserSex("女");
        List<User> users = userDao.findUserByCondition(u);
        for (User user : users) {
            System.out.println(user);
        }
    }

2.动态 SQL 之标签where

为了简化上面 where 1=1 的条件拼装,我们可以采用标签来简化开发。

持久层 Dao 映射配置

<select id="findUserByCondition" parameterType="string" resultMap="userMap">
    select * from user
    <where>
        <if test="userName != null">
            and username = #{userName}
        </if>
        <if test="userSex != null">
            and sex = #{userSex}
        </if>
    </where>
</select>

3.动态标签之标签foreach

传入多个 id 查询用户信息,用下边两个 sql 实现:
SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USERS WHERE username LIKE '%张%' AND id IN (10,89,16)
这样我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。

在 QueryVo 中加入一个 List 集合用于封装参数

public class QueryVo {

    private User user;

    private List<Integer> ids;

    public User getUser(){
        return user;
    }

    public void setUser(User user){
        this.user = user;
    }

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
}

持久层 Dao 接口

/**
 * 根据queryvo中提供的id集合,查询用户信息
 * @param vo
 * @return
 */
List<User> findUserByIds(QueryVo vo);

持久层 Dao 映射配置

SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USERS WHERE username LIKE '%张%' AND id IN (10,89,16)
    <!-- 根据queryvo中的Id集合实现查询用户列表 -->
    <select id="findUserByIds" parameterType="Integer" resultMap="userMap">
        select * from user
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in(" close=")" item="uid" separator=",">
                    #{uid}
                </foreach>
            </if>
        </where>
    </select>

SQL 语句: select 字段 from user where id in (?)

  • foreach标签用于遍历集合
  • collection:代表要遍历的集合元素,注意编写时不要写#{}
  • open:代表语句的开始部分
  • close:代表结束部分
  • item:代表遍历集合的每个元素生成的变量名
  • sperator:代表分隔符

测试方法

@Test
public void testFindUserByIds(){
    QueryVo queryVo = new QueryVo();
    List<Integer> Ids = new ArrayList<>();
    Ids.add(41);
    Ids.add(42);
    Ids.add(43);
    Ids.add(44);
    Ids.add(46);
    queryVo.setIds(Ids);
    List<User> users = userDao.findUserByIds(queryVo);
    for (User user : users) {
        System.out.println(user);
    }
}

4.简化编写的 SQL 片段

Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。

定义代码片段,注意不要;号

<!-- 了解的内容:抽取重复的sql语句-->
<sql id="defaultFindAllUser">
    select * from user
</sql>

<!--映射配置文件的操作配置select,id属性必须dao接口的方法名,resultType是返回结果集类型-实体类的全限定类名-->
<select id="findAll" resultMap="userMap">
    <include refid="defaultFindAllUser"/>
</select>

引用代码片段

<!-- 根据queryvo中的Id集合实现查询用户列表 -->
<select id="findUserByIds" parameterType="Integer" resultMap="userMap">
    <include refid="defaultFindAllUser"/>
    <where>
        <if test="ids != null and ids.size()>0">
            <foreach collection="ids" open="and id in(" close=")" item="uid" separator=",">
                #{uid}
            </foreach>
        </if>
    </where>
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木子津

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值