第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

首頁 慕課教程 MyBatis 入門教程 MyBatis 入門教程 MyBatis if 和多數(shù)據(jù)庫支持

MyBatis if 和多數(shù)據(jù)庫支持

1. 前言

動(dòng)態(tài) SQL 是 MyBatis 最標(biāo)志性的特性之一。在其它框架中,你可能需要根據(jù)不同的條件來拼接 SQL,輾轉(zhuǎn)在符號(hào)與條件的判斷上,處理起來麻煩而且易錯(cuò),而 MyBatis 的動(dòng)態(tài) SQL 可以讓我們擺脫這種痛苦,簡單而又高效的書寫 SQL。

MyBatis 動(dòng)態(tài) SQL 由 OGNL 表達(dá)式和條件標(biāo)簽兩部 分組成,我們將會(huì)分為多個(gè)小節(jié)進(jìn)行介紹。

OGNL 表達(dá)式是動(dòng)態(tài) SQL 的基礎(chǔ),如果你還不了解,請(qǐng)務(wù)必點(diǎn)擊學(xué)習(xí)一下。條件標(biāo)簽部分我們將會(huì)在四個(gè)小節(jié)中分別介紹,它們分別是MyBatis if 和多數(shù)據(jù)庫支持(本小節(jié)),MyBatis choose和bind小節(jié),MyBatis where、set、trim小節(jié)MyBatis foreach小節(jié)。

本小節(jié),我們先來學(xué)習(xí)最基礎(chǔ)的條件標(biāo)簽 if,以及如何使用 if 來讓 MyBatis 來支持多數(shù)據(jù)庫。

2. 定義

慕課解釋:if 常用于 where 語句中,通過判斷參數(shù)來決定在 select 語句中是否使用某個(gè)查詢條件,或者在 update 語句中判斷是否更新一個(gè)字段,還可以在 insert 語句中決定是否插入一個(gè)滿足條件的值。

3. 實(shí)例

我們以一個(gè)實(shí)際的例子來看一下 if 是如何工作的。

<select id="selectUserByAgeAndScore" parameterType="com.imooc.mybatis.model.User" resultMap="userMap">
  SELECT * FROM imooc_user
  WHERE age = #{age}
  <if test="score != null">
    AND score = #{score}
  </if>
</select>

在 if 標(biāo)簽中,test 屬性是 OGNL 的判斷表達(dá)式。

selectUserByAgeAndScore 的作用是通過用戶年齡和積分來查詢用戶,當(dāng) score 值為 null 時(shí),if 判斷不成立,此時(shí)生成的 SQL 語句為:

SELECT * FROM imooc_user WHERE age = ?

而當(dāng) if 成立時(shí),生成的 SQL 語句為:

SELECT * FROM imooc_user WHERE age = ? AND score = ?

通過這樣條件判斷方式,MyBatis 能根據(jù)實(shí)際情況來動(dòng)態(tài)生成 SQL 語句。

4. 實(shí)踐

4.1 例1、動(dòng)態(tài)查詢用戶

請(qǐng)使用 MyBatis 完成對(duì) imooc_user 表動(dòng)態(tài)查詢用戶的功能, username為必須的查詢條件,年齡和積分若為 null 則不使用該字段進(jìn)行過濾。

分析:

按照 MyBatis 的開發(fā)模式,先在 UserMapper.xml 文件中添加動(dòng)態(tài)查詢用戶的 select 標(biāo)簽,然后在 UserMapper.java 中增加上對(duì)應(yīng)的方法。

步驟:

首先,在 UserMapper.xml 中添加 select 標(biāo)簽,并在標(biāo)簽中寫入 SQL,使用 if 來判斷屬性值是否為 null,若為 null,則不使用該字段進(jìn)行查詢。如下:

<select id="selectUserByNameCondition" parameterType="com.imooc.mybatis.model.User"
        resultType="com.imooc.mybatis.model.User">
  SELECT * FROM imooc_user
  WHERE username = #{username}
  <if test="age != null">
    AND age = #{age}
  </if>
  <if test="score != null">
    AND score = #{score}
  </if>
</select>

通過 if 對(duì)屬性的判斷,SQL 的過濾條件就會(huì)發(fā)生相應(yīng)的變化。

然后在 UserMapper.java 中添加上對(duì)應(yīng)的接口方法,方法接受 User對(duì)象作為參數(shù)。

package com.imooc.mybatis.mapper;

import org.apache.ibatis.annotations.Mapper;
import com.imooc.mybatis.model.User;

@Mapper
public interface UserMapper {
   User selectUserByNameCondition(User user);
}

結(jié)果:

通過如下代碼,我們運(yùn)行 selectUserByNameCondition 這個(gè)方法。

UserMapper userMapper = session.getMapper(UserMapper.class);
User condition = new User();
condition.setUsername("pedro");
condition.setScore(200);
User pedro = userMapper.selectUserByNameCondition(condition);
System.out.println(pedro);

condition 對(duì)象 username 和 score 屬性均不為空,而 age 屬性為空,因此最后所執(zhí)行的 SQL 為:

SELECT * FROM imooc_user WHERE username = ? AND score = ? 

成功后,結(jié)果為:

User{id=2, username='pedro', age=24, score=200}

5. 多數(shù)據(jù)庫支持

MyBatis 可以根據(jù)不同的數(shù)據(jù)庫廠商執(zhí)行不同的語句,這種多廠商的支持是基于配置文件中的 databaseId。

5.1 配置

首先在 MyBatis 的全局配置文件中添加如下配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <databaseIdProvider type="DB_VENDOR" />
</configuration>

在 configuration 中加入 databaseIdProvider 后,還需要在 databaseIdProvider 標(biāo)簽中添加上需要使用到的數(shù)據(jù)庫名稱,如:SQL Server。每一個(gè) property 屬性都代表了一個(gè)數(shù)據(jù)庫,name 表示數(shù)據(jù)庫廠商名稱,value 用來設(shè)置別名。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <databaseIdProvider type="DB_VENDOR">
    <property name="SQL Server" value="sqlserver"/>
    <property name="MySQL" value="mysql"/>
    <property name="PostgreSQL" value="postgre"/>
  </databaseIdProvider>
</configuration>

5.2 使用

配置完畢后,我們就可以在 mapper xml 文件中,通過 if 來判斷當(dāng)前的數(shù)據(jù)庫廠商,從而動(dòng)態(tài)生成不同數(shù)據(jù)庫的 SQL。

例如,PostgreSQL 支持使用 ||來拼接字符串,而 MySQL 需要使用 concat函數(shù)來拼接字符串。

因此,如果在模糊查詢的時(shí)候,不同的數(shù)據(jù)庫廠商需要不同的 SQL 語句,通過 if 來判斷數(shù)據(jù)庫廠商來生成對(duì)于的 SQL 語句。

如下:

<select id="selectUserByLikeName" resultType="com.imooc.mybatis.model.User">
  SELECT * FROM imooc_user
  WHERE username LIKE
  <if test="_databaseId == 'mysql'">
    CONCAT('%',#{username},'%')
  </if>
  <if test="_databaseId == 'postgre'">
    '%' || #{username} || '%'
  </if>
</select>

注意,這里 _databaseId 參數(shù)是由 MyBatis 提供的內(nèi)置參數(shù),對(duì)應(yīng)于 databaseIdProvider 中配置的數(shù)據(jù)庫名稱。

通過 databaseIdProvider 配置,即時(shí)數(shù)據(jù)庫廠商之間存在差異,但仍然可以通過動(dòng)態(tài) SQL 的形式來支持多數(shù)據(jù)庫。

6. 小結(jié)

  • if 簡單且好用,是動(dòng)態(tài) SQL 中的最基礎(chǔ)的標(biāo)簽,一看便會(huì)。
  • 多數(shù)據(jù)庫支持的應(yīng)用場(chǎng)景其實(shí)較少,但如果真的需要,也可通過 MyBatis 來方便的實(shí)現(xiàn)。