Java笔记--Mybatis的动态sql(二)--2021-05-31

13 篇文章 0 订阅

Mybatis的动态sql(二)


动态sql: sql的内容是变化的,可以根据条件获取到不同的sql语句。主要是where部分发生变化。

一、foreach

    <foreach> 循环java中的数组, list集合的。主要用在sql的in语句中。
    collection="集合类型(list、array)"
    item="集合中的成员"
    open="开始字符"
    close="结束字符"
    separator="集合成员之间的分隔符"

1.1 例子

接口:

    /* 使用foreach动态sql */
    List<Student> selectStudentForeach(List<Integer> list);

xml:

    <!-- foreach
        <foreach> 循环java中的数组, list集合的。主要用在sql的in语句中。
        collection="集合类型(list、array)"
        item="集合中的成员"
        open="开始字符"
        close="结束字符"
        separator="集合成员之间的分隔符"
    -->
    <select id="selectStudentForeach" resultType="Student">
        /* 引入sql */
        <include refid="studentSql" /> where id in
        <foreach collection="list" item="myId" open="(" close=")" separator=",">
            #{myId}
        </foreach>
    </select>

测试类:

    @Test
    public void testSelectStudentForeach() {
        /*
         * 使用mybatis的动态代理机制,使用sqlSession.getMapper(dao接口)
         * getMapper()获取dao接口对于的实现类对象
         */
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获得实现类
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        //参数
        List<Integer> list = new ArrayList<>();
        list.add(1001);
        list.add(1002);
        //使用方法
        List<Student> students = studentDao.selectStudentForeach(list);
        //打印
        students.forEach(stu1 -> System.out.println(stu1));
        //关闭
        sqlSession.close();
    }

1.2 例子

接口:

    /* 使用foreach动态sql 灵活性一*/
    List<Student> selectStudentForeach2Student(List<Student> list);

xml:

  <!--  &lt;!&ndash; foreach 灵活性一 &ndash;&gt;
    <select id="selectStudentForeach2Student" resultType="Student">
        /* 引入sql */
        <include refid="studentSql" /> where id in
        <foreach collection="list" item="myStu" open="(" close=")" separator=",">
            #{myStu.id}
        </foreach>
    </select>-->

    <!-- foreach 灵活性二 -->
    <select id="selectStudentForeach2Student" resultType="Student">
        /* 引入sql */
        <include refid="studentSql" /> where id in (
            <foreach collection="list" item="myStu">
                #{myStu.id} ,
            </foreach>
        -1 )
    </select>

测试类:

    @Test
    public void testSelectStudentForeach2Student() {
        /*
         * 使用mybatis的动态代理机制,使用sqlSession.getMapper(dao接口)
         * getMapper()获取dao接口对于的实现类对象
         */
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获得实现类
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        //参数
        List<Student> list = new ArrayList<>();
        Student s1 = new Student();
        s1.setId(1001);
        Student s2 = new Student();
        s2.setId(1002);
        list.add(s1);
        list.add(s2);
        //使用方法
        List<Student> students = studentDao.selectStudentForeach2Student(list);
        //打印
        students.forEach(stu1 -> System.out.println(stu1));
        //关闭
        sqlSession.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

张德帅-001

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

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

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

打赏作者

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

抵扣说明:

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

余额充值