码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis学习记录~

时间:2018-12-30 14:40:02      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:sql语句   5.0   null   otherwise   com   pac   dac   pre   情况   

mybatis版本

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>

 

动态SQL语句

 

IF标签

<select id="findSelective" parameterType="com.tsing.model.Blog"
        resultMap="BaseResultMap">
        SELECT * FROM blog 
            WHERE `status` = 1
            <if test="title != null and title != ‘‘ ">
                and title like #{title}
            </if>
            
            <if test="summary != null ">
                and summary like #{summary}
            </if>
</select>

 

@Test
    public void testFindSelective() {
        SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory();

        SqlSession session = factory.openSession(true);
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        
        Blog blog = new Blog();
        blog.setTitle("");
        blog.setSummary("");
        mapper.findSelective(blog) ;

    }

 

日志输出

==>  Preparing: SELECT * FROM blog WHERE `status` = 1 and summary like ? 

==> Parameters: (String)

 

CHOOSE标签

<select id="findSelectiveWithChoose" resultType="com.tsing.model.Blog">
        SELECT * FROM blog WHERE `status` = 1
        <choose>
            <when test="title != null">
                AND title like #{title}
            </when>
            <when test="summary != null">
                AND summary like #{summary}
            </when>
            <otherwise>
                AND content =  ‘tsing‘
            </otherwise>
        </choose>
    </select>

 

@Test
public void testFindSelectiveWithChoose() {
        SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory();

        SqlSession session = factory.openSession(true);
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        
        Blog blog = new Blog();
        
        mapper.findSelectiveWithChoose(blog);

    }

 

日志输出

==>  Preparing: SELECT * FROM blog WHERE `status` = 1 AND content = ‘tsing‘ 

==> Parameters: 

 

WHERE标签

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE 
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
</select>

上述的sql语句可能导致以下的问题出现: SELECT * FROM BLOG WHERE AND title like ?

 where元素只会在至少有一个子元素的条件返回SQL子句的情况下才会插入where子句。若子句的开头是and或者or,where元素也会将其移除。

<select id="findSelectiveWithWhere" resultType="com.tsing.model.Blog">
        SELECT * FROM blog
        <where>
            <if test="title != null ">
                and title like #{title}
            </if>
            <if test="summary != null ">
                or summary like #{summary}
            </if>
        </where>
</select> 

 

@Test
public void testFindSelectiveWithWhere() {
        SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory();

        SqlSession session = factory.openSession(true);
        BlogMapper mapper = session.getMapper(BlogMapper.class);

        Blog blog = new Blog();
        blog.setSummary("");
        mapper.findSelectiveWithWhere(blog);

    }

 

日志输出

==>  Preparing: SELECT * FROM blog WHERE summary like ? 

==> Parameters: (String)

 

 

TRIM标签

<select id="findSelectiveWithTrim" resultType="com.tsing.model.Blog">
        SELECT * FROM blog
        <trim prefix="WHERE" prefixOverrides="AND |OR ">
            <if test="title != null ">
                and title like #{title}
            </if>
            <if test="summary != null ">
                or summary like #{summary}
            </if>
        </trim>
</select>

 

@Test
public void testFindSelectiveWithTrim() {
        SqlSessionFactory factory = DataBaseTools.getSqlSessionFactory();

        SqlSession session = factory.openSession(true);
        BlogMapper mapper = session.getMapper(BlogMapper.class);

        Blog blog = new Blog();
        blog.setTitle("");
        //blog.setSummary("");
        mapper.findSelectiveWithTrim(blog);

    }

 

日志输出

==>  Preparing: SELECT * FROM blog WHERE title like ? 

==> Parameters: (String)

 

 

  

 

 

 

mybatis学习记录~

标签:sql语句   5.0   null   otherwise   com   pac   dac   pre   情况   

原文地址:https://www.cnblogs.com/tsing0520/p/10198880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!