MyBatis-12- dynamic SQL

12, dynamic SQL

What dynamic SQL: Dynamic SQL refers to different conditions according to different SQL statements generated

Dynamic SQL can use this feature to completely get rid of the pain

Dynamic SQL or similar elements and JSTL XML-like text-based processor. In previous versions of MyBatis, there are many elements need to take time to understand. MyBatis 3 greatly streamlined the element type, are only half of the original elements can learn. MyBatis employs powerful OGNL based expressions to eliminate most of the other elements.

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

Built environment

CREATE TABLE `bolg`(
    `id` VARCHAR(50) NOT NULL COMMENT '博客id',
    `title` VARCHAR(100) not null comment '博客标题',
    `author` VARCHAR(30) not null comment '博客作者',
    `creat_time` datetime not null comment '创建时间',
    `views` int(30) not null comment '浏览量'
)ENGINE=InnoDB DEFAULT CHARSET=utf8

Creating a basic project

  1. Guide package

  2. Write configuration file

  3. Write entity class

    @Data
    public class Blog {
        private int id;
        private String title;
        private String author;
        private Date creatTime;
        private int views;
    }
  4. Mapper write interfaces and corresponding entity class Mapper.xml

IF

<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
    select * from mybatis.bolg where 1=1
    <if test="title != null">
        and title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</select>
@Test
public void queryBlogIF(){
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    HashMap map = new HashMap();
    map.put("author","尹锐");
    List<Blog> blogs = mapper.queryBlogIF(map);
    for (Blog blog : blogs) {
        System.out.println(blog);
    }
    sqlSession.close();
}

choose (when, otherwise)

<select id="queryBlogChoose" parameterType="map" resultType="com.rui.pojo.Blog">
    select * from mybatis.bolg
    <where>
        <choose>
            <when test="title != null">
                title=#{title}
            </when>
            <when test="author!=null">
                and author = #{author}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

trim, (where, set)

select * from mybatis.bolg
<where>
<if test="title != null">
    title = #{title}
</if>
<if test="author != null">
    and author = #{author}
</if>
</where>
<update id="updateBlog" parameterType="map">
    update mybatis.bolg
    <set>
        <if test="title != null">
            title = #{title},
        </if>
        <if test="author != null">
            author = #{author},
        </if>
    </set>
    where id = #{id}
</update>

The so-called dynamic SQL, or the nature of the SQL statement, but we can at SQL level, to perform some logic code

if

Where,set,choose,when

SQL fragment

Sometimes, we may some public parts extracted for easy reuse!

  1. Use SQL to extract the public part of the label

    <sql id="if-title-author">
        <if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </sql>
  2. Include reference to the use of the label used where needed

    <select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
        select * from mybatis.bolg
        <where>
            <include refid="if-title-author"></include>
        </where>
    </select>

    Precautions:

    • Preferably fragments are defined based on a single SQL table!
    • Do not exist or where set label , try to fragment in only if enough

    Foreach

    select * from user where 1=1 and 
      <foreach item="id" index="index" collection="ids"
          open="(" separator="or" close=")">
            #{id}
      </foreach>
    
    (id=1 or id=2 or id=3)

  1. <!--
    select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)
    
    我们现在传递一个万能的map,这个map中可以存在一个map
    -->
    <select id="queryBlogForeach" parameterType="map" resultType="com.rui.pojo.Blog">
        select * from mybatis.bolg
    
        <where>
        <foreach collection="ids" item="id" open="(" close=")" separator="or">
            id = #{id}
        </foreach>
        </where>
    </select>

    Dynamic SQL is SQL statements in the mosaic, as long as we ensure the correctness of SQL, SQL according to the format, to the permutations and combinations on it

    Suggest:

    • In the first to write a complete SQL Mysql, in a corresponding called to modify our dynamic SQL

Guess you like

Origin www.cnblogs.com/MrKeen/p/12012111.html