美文网首页
mybatis-动态sql

mybatis-动态sql

作者: 李霖神谷 | 来源:发表于2019-12-04 21:27 被阅读0次

动态sql,减少了我们的sql标签的书写,简化我们业务层的开发
1.where if标签
当我们发送name时就会根据那么来查获数据,当传的是password的时候就根据密码来查询。

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shuai.mapper.bookDao">
    <resultMap id="booklist" type="book">
        <result property="my_password" column="password"></result>
    </resultMap>

    <select id="findAll" resultMap="booklist" parameterType="book">

        select * from p
        <where>
            <if test="name!=null">
                name=#{name}
            </if>
            <if test="password!=null">
                password=#{password}
            </if>
        </where>
    </select>

</mapper>

2.sql片段
当你的sql语句需要写很多的时候,可以设置sql片段,简化开发。

<sql id="sselect_user"> select * from p</sql>
    <select id="findAll" resultMap="booklist" parameterType="book">

        <include refid="sselect_user"></include>

3.foreach标签
可以进行批量删除的操作

 
    @Test
    public void test(){
       SqlSession sqlSession= sqlSessionFactory.openSession();
       bookDao bookDao= sqlSession.getMapper(bookDao.class);
      List<String> list=new ArrayList<String>();
      list.add("66");
      list.add("22");
      bookDao.deletBook(list);

    }
}

   <delete id="deletBook" parameterType="list">
        delete  from  p where
        <foreach collection="list" open="password in(" close=")" separator="," item="password">
            #{password}
        </foreach>
    </delete>

相关文章

  • Mybatis-动态SQL

    概述ifchoose when otherwisetrim where setforeachtest元素 欢迎访问...

  • mybatis-动态sql

    动态sql,减少了我们的sql标签的书写,简化我们业务层的开发1.where if标签当我们发送name时就会根据...

  • MyBatis-动态SQL

    MyBatis动态SQL元素 1. 满足条件就执行对应的sql语句 小提示: 问:为什么要用where...

  • Mybatis-传入动态sql

    1. mybatis 传入sql 语句执行 实现: application.yml pom

  • MyBatis-动态SQL常用标签

    功能标签名称定义sql语句insertdeleteupdateselect配置java对象属性与查询结果集中列名对...

  • 16、mybatis-动态sql-if

    一、说明 if元素 是简单的条件判断逻辑,满足指定条件时追加 if元素内的SQL,类似于Java 中的 if语句...

  • Web框架-Mybatis-动态SQL

    Java工程师知识树[https://www.jianshu.com/p/db77d19a25f6] / We...

  • Spring-整合MyBatis

    零、本文纲要 一、JDBC & MyBatis相关内容 JDBC-手动编写SQL MyBatis-手动编写SQL ...

  • 19、mybatis-动态sql-foreach

    一、概要 动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历foreach可以遍历三种类型,List,ar...

  • 15、mybatis-映射文件-动态sql

    一、概要 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都...

网友评论

      本文标题:mybatis-动态sql

      本文链接:https://www.haomeiwen.com/subject/cbcrgctx.html