怎么在MyBatis中配置動(dòng)態(tài)SQL

本篇文章為大家展示了怎么在MyBatis中配置動(dòng)態(tài)SQL,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的莘縣網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

動(dòng)態(tài)SQL

什么是動(dòng)態(tài)SQL:

動(dòng)態(tài)SQL就是根據(jù)不同的條件生成不同的SQL語(yǔ)句

  • if

  • choose(when,otherwise)

  • trim(where,set)

  • foreach

1、搭建環(huán)境

建表

CREATE TABLE `bolg`(
  `id` VARCHAR(50) NOT NULL COMMENT '博客id',
  `title` VARCHAR(100) not null comment '博客標(biāo)題',
  `author` VARCHAR(30) not null comment '博客作者',
  `creat_time` datetime not null comment '創(chuàng)建時(shí)間',
  `views` int(30) not null comment '瀏覽量'
)ENGINE=InnoDB DEFAULT CHARSET=utf8

創(chuàng)建一個(gè)基礎(chǔ)工程

導(dǎo)包

編寫(xiě)配置文件

編寫(xiě)實(shí)體類(lèi)

@Data
public class Blog {
  private int id;
  private String title;
  private String author;
  private Date creatTime;
  private int views;
}

編寫(xiě)實(shí)體類(lèi)對(duì)應(yīng)的Mapper接口和Mapper.xm

2、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();
}

3、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>

4、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>

所謂的動(dòng)態(tài)SQL,本質(zhì)還是SQL語(yǔ)句,只是我們可以在SQL層面,去執(zhí)行一些邏輯代碼

5、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)

怎么在MyBatis中配置動(dòng)態(tài)SQL

<!--
select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)

我們現(xiàn)在傳遞一個(gè)萬(wàn)能的map,這個(gè)map中可以存在一個(gè)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>

SQL片段

有的時(shí)候,我們可能會(huì)將一些公共的部分抽取處理,方便復(fù)用

使用SQL標(biāo)簽抽取公共的部分

<sql id="if-title-author">
  <if test="title != null">
    title = #{title}
  </if>
  <if test="author != null">
    and author = #{author}
  </if>
</sql>

在需要使用的地方使用Include標(biāo)簽引用即可

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

上述內(nèi)容就是怎么在MyBatis中配置動(dòng)態(tài)SQL,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:怎么在MyBatis中配置動(dòng)態(tài)SQL
文章地址:http://bm7419.com/article48/igcsep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航Google、關(guān)鍵詞優(yōu)化搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司