mybatis04_动态sql

本文详细介绍了MyBatis中动态SQL的实现,包括if标签用于条件判断,where标签用于智能添加where关键字并优化多余的逻辑运算符,以及foreach标签用于处理数组和集合的in语句。通过实例代码展示了如何在映射文件中使用这些标签进行动态SQL拼接,以实现灵活的数据查询操作。
摘要由CSDN通过智能技术生成

动态sql

4.2、动态sql的实现

1.动态sql的实现,sql的内容是变化的,可以根据条件获取不同的sql语句,主要是where部分的变化。

2.动态sql的实现,使用的是mybatis提供的标签, if ,where,foreach.

  1. if是判断条件的,语法<if test="判断java对象的属性值” >
//动态sql,使用java对象做为参数    
List<Student> selectStudentIf(Student student);
  1. if test="使用参数java对象的属性值作为判断条件,语法:属性=xxx值“这里要注意sql语句的书写,很容易出现sql语句出错
  <select id="selectStudentIf" resultType="com.xw.domain.Student">
        select id,name,email,age from student where
        <if test="name!= null and name!=''">
             name=#{name}
        </if>
        <if test="age>0">
           and age>#{age}
        </if>
    </select>

测试代码

@Test
    public void testSelectStudentIf(){
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);
        Student stu=new Student();
//        stu.setName("小王");
        stu.setAge(30);
        List<Student> students = dao.selectStudentIf(stu);
        for (Student stu1:students) {
            System.out.println("查询的学生:"+stu1);
        }
  1. where 用来包含多个if的,当多个if有一个成立的,where会自动增加一个where关键字。并且会去掉if中多余的and,or等。
    StudentDao中的代码
//where的使用
    List<Student> selectStudentWhere(Student student);

StudentDao.xml中的代码

<!--where的语法:<where><if></if> <if></if>...</where>-->
    <select id="selectStudentWhere" resultType="com.xw.domain.Student">
        select id,name,email,age from student
        <where>
            <if test="name!=null and name!=''">
                name=#{name}
            </if>
            <if test="age>0">
                or age>#{age}
            </if>
        </where>
    </select>

测试代码

@Test
public void testSelectStudentWhere(){
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);
        Student student=new Student();
//        student.setName("小王");
        student.setAge(22);
        List<Student> students = dao.selectStudentWhere(student);
        for (Student stu:students){
            System.out.println("查询的学生:"+stu);
        }
    }

注意:使用where的好处:即使第一个name属性没有,下一个age前面的or也会被去掉

  1. foreach循环java中的数组,List集合的,主要用在sql的in语句中,
    StudentDao中的代码
//foreach的用法 1
    List<Student> selectStudentForeachOne(List<Integer>idList);

StudentDao.xml中的代码

<!--foreach使用1,List<Integer>
        collection:表示的接口中的方法参数的类型,如果数组使用array,如果是List集合使用List
        item:自定义的,表示数组和集合成员的变量。
        open:循环开始的的字符
        close:循环结束时的字符
        separator:集合成员之间的分割符
        open和close和separator也可以自己在外拼接,
    -->
    <select id="selectStudentForeachOne" resultType="com.xw.domain.Student">
          select id,name,email,age from student where id in
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
     </select>

测试代码

@Test
    public void testSelectStudentForeachOne(){
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        StudentDao dao =sqlSession.getMapper(StudentDao.class);
        List<Integer>list=new ArrayList<>();
        list.add(1001);
        list.add(1002);
        list.add(1003);
        List<Student> students = dao.selectStudentForeachOne(list);
        for(Student student:students){
            System.out.println(student);
        }
    }

  1. foreach的用法2
    StudentDao中的代码:
List<Student> selectStudentForeachTwo(List<Student>stuList);

StudentDao.xml中的代码

<!--foreach的第二种使用方法-->
    <select id="selectStudentForeachTwo" resultType="com.xw.domain.Student">
        select id,name,email,age from student where id in
        <foreach collection="list" item="stu" open="(" close=")" separator=",">
            #{stu.id}
        </foreach>
    </select>

测试代码

@Test
    public void testSelectStudentForeachTwo(){
        SqlSession sqlSession=MybatisUtils.getSqlSession();
        StudentDao dao =sqlSession.getMapper(StudentDao.class);
        List<Student>list=new ArrayList<>();
        Student stu=new Student();
        stu.setId(1001);
        list.add(stu);
        stu=new Student();
        stu.setId(1002);
        list.add(stu);
        List<Student> students = dao.selectStudentForeachTwo(list);
        for(Student student:students){
            System.out.println(student);
        }
    }

  1. 动态sql之代码片段,就是复用一些语法。
    1. 先定义<sql id="自定义名唯一的">sql语句,表名,字段等</sql>
    2. 然后在需要的地方使用<include refid="id的值"/>来引用。(使用非常灵活)

代码实现:

<sql id="select">
        select id,name,email,age from student
    </sql>
    <select id="selectStudentForeachOne" resultType="com.xw.domain.Student">
          <include refid="select"/> where id in
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
     </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis批量更新动态sql可以使用foreach标签来实现。在动态sql中,可以使用foreach标签来遍历一个集合,并对集合中的每个元素执行相同的sql语句。具体操作步骤如下: 1. 定义一个集合对象,用于存储要批量更新的数据。 2. 在mapper.xml文件中,使用<foreach>标签包裹要执行的sql语句。例如,对于Oracle 12C环境下的批量入库方式,可以按照以下格式编写sql语句: <insert id="insertSysUserLoanGroups" useGeneratedKeys="false"> insert into SYS_USER_LOAN_GROUP (USER_ID,GROUP_ID) ( <foreach item="item" index="index" collection="list" separator="UNION ALL"> select #{item.userId}, #{item.groupId} from dual </foreach> ) </insert> 在上述示例中,<foreach>标签中的collection属性指定了要遍历的集合对象,item属性指定了集合中的每个元素的别名,index属性指定了循环的索引变量名,separator属性指定了每个循环体之间的分隔符。 这样,通过foreach标签,可以将集合中的每个元素转换为一条insert语句的一部分,从而实现批量插入的功能。 3. 在Java代码中,使用mybatisSqlSession对象执行上述定义的批量更新操作。 通过调用SqlSession对象的insert方法,并指定要执行的sql语句的id(即insertSysUserLoanGroups),以及要传递给sql语句的参数,即定义的集合对象。 例如,可以按照以下方式执行批量更新操作: ``` List<UserLoanGroup> dataList = new ArrayList<>(); // 向dataList中添加要批量插入的数据 sqlSession.insert("insertSysUserLoanGroups", dataList); ``` 在上述示例中,insertSysUserLoanGroups是mapper.xml文件中定义的要执行的sql语句的id,dataList是要传递给sql语句的参数。 这样,就可以通过mybatisforeach标签和SqlSession对象来实现mybatis批量更新动态sql的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值