16、mybatis学习——mybatis的动态sql之<if>、<where>和<trim>

摘要:
=''“˃andname=#{name}Test 1//测试动态SQLif@TestpublicvoidtestGetStuByIf()throwsIOException{Stringresource=“mybatis config.xml”;InputStreaminputStream=Resources.getResourceAsStream;SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build;SqlSessionsqlSession=sqlSessionFactory.openSession();StudentMappersudentMapper=sqlSession.getMapper;Studentstudent=studentMapper.getStuByIf;System.out.println;sqlSession.close();}测试结果语句:Test 2//测试动态sqlif@TestpublicvoidtestGetStuByIf()throwsIOException{Stringresource=“mybatis config.xml”;InputStreaminputStream=Resources.getResourceAsStream;SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build;SqlSessionsqlSession=sqlSessionFactory.openSession();StudentMappersudentMapper=sqlSession.getMapper;Studentstudent=studentMapper.getStuByIf;System.out.println;sqlSession.close();}测试结果语句为:测试方法中的参数ID为空时存在问题//测试动态SQLif@TestpublicvoidtestGetStuByIf()throwsIOException{Stringresource=“mybatis config.xml”;InputStreaminputStream=Resources.getResourceAsStream;SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder().build;SqlSessionsqlSession=sqlSessionFactory.openSession();StudentMappersudentMapper=sqlSession.getMapper;Studentstudent=studentMapper.getStuByIf;System.out.println;sqlSession.close();}测试结果显示SQL语句此时出错,因此我们需要解决这个问题:解决方案1:在其中添加1=1,即StudentMapper配置更改为select*from studenthere1andname=#{name}˂/select

Student.java:

16、mybatis学习——mybatis的动态sql之<if>、<where>和<trim>第1张

 StudentMapper接口定义方法:

16、mybatis学习——mybatis的动态sql之<if>、<where>和<trim>第2张

 StudentMapper配置文件进行配置

     <select id="getStuByIf" resultType="student">
         select * from student where
         <!-- test:判断表达式;里面使用的是OGNL表达式,可百度查询OGNL的使用
             OGNL会自动进行字符串与数字的转换判断;字符串"0"和数字0是一样的
             从参数中取值判断;遇见特殊符号应该写转义字符例如&(&amp;)符号 -->
         <if test="id!=null">
             id = #{id}
         </if>
         <if test="name!=null &amp;&amp; name.trim()!=''">
             and name = #{name}
         </if>
     </select>

测试1(id和name都有值)

    //测试动态sql的if
    @Test
    public void testGetStuByIf() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.getStuByIf(new Student(1, "小明"));
        System.out.println(student);
        sqlSession.close();
    }

测试结果的语句:

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第3张

 测试2(当id有值,name为空时)

    //测试动态sql的if
    @Test
    public void testGetStuByIf() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.getStuByIf(new Student(1, ""));
        System.out.println(student);
        sqlSession.close();
    }

测试结果语句为:

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第4张

此时有一个问题当测试方法中传参的id为空时

    //测试动态sql的if
    @Test
    public void testGetStuByIf() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.getStuByIf(new Student(null, "小明"));
        System.out.println(student);
        sqlSession.close();
    }

测试结果为

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第5张

 此时sql语句出错(通过查看StudentMapper的sql配置可找到原因)

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第6张

所以我们需要解决此问题:

方案一:在where后面加一个1=1

即StudentMapper配置改为

     <select id="getStuByIf" resultType="student">
         select * from student where 1=1
         <!-- test:判断表达式;里面使用的是OGNL表达式,可百度查询OGNL的使用
             OGNL会自动进行字符串与数字的转换判断;字符串"0"和数字0是一样的
             从参数中取值判断;遇见特殊符号应该写转义字符例如&符号和'符号 -->
         <if test="id!=null">
             id = #{id}
         </if>
         <if test="name!=null &amp;&amp; name.trim()!=''">
             and name = #{name}
         </if>
     </select>

此时测试结果为

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第7张

方案二:将条件包括在<where>标签中

mybatis中的<where>标签会把多出来的and或者or自动去掉

即StudentMapper的配置文件改为:

     <select id="getStuByIf" resultType="student">
         select * from student
         <where>
             <!-- test:判断表达式;里面使用的是OGNL表达式,可百度查询OGNL的使用
                 OGNL会自动进行字符串与数字的转换判断;字符串"0"和数字0是一样的
                 从参数中取值判断;遇见特殊符号应该写转义字符例如&符号和'符号 -->
             <if test="id!=null">
                 id = #{id}
             </if>
             <if test="name!=null &amp;&amp; name.trim()!=''">
                 and name = #{name}
             </if>
         </where>
     </select>

测试结果

 16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第8张

方案三:通过<trim>标签去掉条件中前面或者后面多余的字符

  为了测试方便在StudentMapper接口中再定义一个方法

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第9张

   StudentMapper的配置文件改为:

      <select id="getStuByTrim" resultType="student">
         select * from student
         <!-- 后面多出来的and或者or <where>标签不能解决
             则此时使用<trim>标签,<trim>标签体中是整个字符串拼串后的结果
              prefix="":给拼串后的字符串加一个前缀
              prefixOverrides="":去掉整个字符串前面多余的字符
              同理也有
              suffix="":给拼串后的字符串加一个后缀
              suffixOverrides="" 去掉整个字符串后面多余的字符-->
         <trim prefix="where" prefixOverrides="and"  >
             <if test="id!=null">
                 id = #{id}
             </if>
             <if test="name!=null &amp;&amp; name.trim()!=''">
                 and name = #{name}
             </if>
         </trim>
     </select>

测试方法:

    //测试动态sql的<trim>
    @Test
    public void testGetStuByTrim() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = studentMapper.getStuByTrim(new Student(null, "小明"));
        System.out.println(student);
        sqlSession.close();
    }

测试结果

16、mybatis学习——mybatis的动态sql之&lt;if&gt;、&lt;where&gt;和&lt;trim&gt;第10张

免责声明:文章转载自《16、mybatis学习——mybatis的动态sql之&amp;lt;if&amp;gt;、&amp;lt;where&amp;gt;和&amp;lt;trim&amp;gt;》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JS实现控制HTML5背景音乐播放暂停centos7安装配置mysql8下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SqlServer动态执行SQL语句sp_executesql、Exec

sp_executesql语法 sp_executesql[@stmt=]stmt[{,[@params=]N'@parameter_name data_type[,...n]'}{,[@param1=]'value1'[,...n] }] 参数 [@stmt=]stmt 包含 Transact-SQL 语句或批处理的 Unicode 字符串,stmt必...

vue 单页应用 seo 优化之 预渲染(prerender-spa-plugin)

前言:当前 SPA 架构流行的趋势如日中天,前后端分离的业务模式已经成为互联网开发的主流方式,但是 单页面 应用始终存在一个痛点,那就是 SEO, 对于那些需要推广,希望能在百度搜索时排名靠前的网站而言,使用单页面应用的是无法被 百度的 蜘蛛 爬到的,为此,众多流行的 MVVM 框架都推出了 很多解决方案,有官方的也有三方的,VUE也不例外,本文章就来分享...

oracle Database Link

1 Database Link 的创建: 有两个数据库服务器A/B, 其中A的IP地址为172.20.36.245, 服务器B为本机。服务器B上的数据库实例名为ORCL,在本机上的服务监听配置上有服务器A上实例配置: BIWG_TEST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.20...

layui select下拉菜单联动

做的比较简单,先从后台直接把第一级菜单输出,然后点击二级菜单的时候再动态展示 <div class="layui-inline"> <label class="layui-form-label">商品类别</label> <div cl...

mybatis的xml中sql语句中in的写法(迭代遍历)

这里使用 foreach标签 <foreach  item="item" collection="listTag" index="index"  open="(" separator="," close=")"> #{item} </foreach> foreach元素的属性主要有 item,index,collection,ope...

JS设置、获取DOM自定义属性

jQuery方式 //获取 $('#test').attr('mydata'); //设置 $('#test').attr('mydata','data-content'); //移除 $('#test').removeAttr('mydata'); JS方式 //获取 document.getElementById('test').getAttribut...