最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Fluent MyBatis實現(xiàn)動態(tài)SQL

 更新時間:2021年08月04日 16:07:09   作者:稻草江南  
MyBatis 令人喜歡的一大特性就是動態(tài) SQL。本文主要介紹了Fluent MyBatis實現(xiàn)動態(tài)SQL,具有一定的參考價值,感興趣的小伙伴們可以參考一下

MyBatis 令人喜歡的一大特性就是動態(tài) SQL。在使用 JDBC 的過程中, 根據(jù)條件進行 SQL 的拼接是很麻煩且很容易出錯的,
MyBatis雖然提供了動態(tài)拼裝的能力,但這些寫xml文件,也確實折磨開發(fā)。Fluent MyBatis提供了更貼合Java語言特質(zhì)的,對程序員友好的Fluent拼裝能力。

Fluent MyBatis動態(tài)SQL,寫SQL更爽

數(shù)據(jù)準(zhǔn)備

為了后面的演示, 創(chuàng)建了一個 Maven 項目 fluent-mybatis-dynamic, 創(chuàng)建了對應(yīng)的數(shù)據(jù)庫和表

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student`
(
    `id`           bigint(21) unsigned NOT NULL AUTO_INCREMENT COMMENT '編號',
    `name`         varchar(20) DEFAULT NULL COMMENT '姓名',
    `phone`        varchar(20) DEFAULT NULL COMMENT '電話',
    `email`        varchar(50) DEFAULT NULL COMMENT '郵箱',
    `gender`       tinyint(2)  DEFAULT NULL COMMENT '性別',
    `locked`       tinyint(2)  DEFAULT NULL COMMENT '狀態(tài)(0:正常,1:鎖定)',
    `gmt_created`  datetime    DEFAULT CURRENT_TIMESTAMP COMMENT '存入數(shù)據(jù)庫的時間',
    `gmt_modified` datetime    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的時間',
    `is_deleted`   tinyint(2)  DEFAULT 0,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_0900_ai_ci COMMENT ='學(xué)生表';

代碼生成

使用Fluent Mybatis代碼生成器,生成對應(yīng)的Entity文件

public class Generator {
    static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8";
    /**
     * 生成代碼的package路徑
     */
    static final String basePackage = "cn.org.fluent.mybatis.dynamic";

    /**
     * 使用 test/resource/init.sql文件自動初始化測試數(shù)據(jù)庫
     */
    @BeforeAll
    static void runDbScript() {
        DataSourceCreatorFactory.create("dataSource");
    }

    @Test
    void test() {
        FileGenerator.build(Nothing.class);
    }

    @Tables(
        /** 數(shù)據(jù)庫連接信息 **/
        url = url, username = "root", password = "password",
        /** Entity類parent package路徑 **/
        basePack = basePackage,
        /** Entity代碼源目錄 **/
        srcDir = "src/main/java",
        /** 如果表定義記錄創(chuàng)建,記錄修改,邏輯刪除字段 **/
        gmtCreated = "gmt_created", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
        /** 需要生成文件的表 ( 表名稱:對應(yīng)的Entity名稱 ) **/
        tables = @Table(value = {"student"})
    )

    public static class Nothing {
    }
}

編譯項目,ok,下面我們開始動態(tài)SQL構(gòu)造旅程

在 WHERE 條件中使用動態(tài)條件

在mybatis中,if 標(biāo)簽是大家最常使用的。在查詢、刪除、更新的時候結(jié)合 test 屬性聯(lián)合使用。

示例:根據(jù)輸入的學(xué)生信息進行條件檢索

  • 當(dāng)只輸入用戶名時, 使用用戶名進行模糊檢索;
  • 當(dāng)只輸入性別時, 使用性別進行完全匹配
  • 當(dāng)用戶名和性別都存在時, 用這兩個條件進行查詢匹配查詢

mybatis動態(tài) SQL寫法

<select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">
    select
    <include refid="Base_Column_List" />
    from student
    <where>
        <if test="name != null and name !=''">
          and name like concat('%', #{name}, '%')
        </if>
        <if test="sex != null">
          and sex=#{sex}
        </if>
    </where>
</select>

fluent mybatis動態(tài)寫法

@Repository
public class StudentDaoImpl extends StudentBaseDao implements StudentDao {
   /**
    * 根據(jù)輸入的學(xué)生信息進行條件檢索
    * 1. 當(dāng)只輸入用戶名時, 使用用戶名進行模糊檢索;
    * 2. 當(dāng)只輸入性別時, 使用性別進行完全匹配
    * 3. 當(dāng)用戶名和性別都存在時, 用這兩個條件進行查詢匹配的用
    *
    * @param name   姓名,模糊匹配
    * @param isMale 性別
    * @return
    */
    @Override
    public List<StudentEntity> selectByNameOrEmail(String name, Boolean isMale) {
        return super.defaultQuery()
            .where.name().like(name, If::notBlank)
            .and.gender().eq(isMale, If::notNull).end()
            .execute(super::listEntity);
    }
}

FluentMyBatis的實現(xiàn)方式至少有下面的好處

  • 邏輯就在方法實現(xiàn)上,不需要額外維護xml,割裂開來
  • 所有的編碼通過IDE智能提示,沒有字符串魔法值編碼
  • 編譯檢查,拼寫錯誤能立即發(fā)現(xiàn)

測試

@SpringBootTest(classes = AppMain.class)
public class StudentDaoImplTest extends Test4J {
    @Autowired
    StudentDao studentDao;

    @DisplayName("只有名字時的查詢")
    @Test
    void selectByNameOrEmail_onlyName() {
        studentDao.selectByNameOrEmail("明", null);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE name LIKE ?",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"%明%"});
    }

    @DisplayName("只有性別時的查詢")
    @Test
    void selectByNameOrEmail_onlyGender() {
        studentDao.selectByNameOrEmail(null, false);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE gender = ?",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{false});
    }

    @DisplayName("姓名和性別同時存在的查詢")
    @Test
    void selectByNameOrEmail_both() {
        studentDao.selectByNameOrEmail("明", false);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE name LIKE ? " +
                "AND gender = ?",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"%明%", false});
    }
}

在 UPDATE 使用動態(tài)更新

只更新有變化的字段, 空值不更新

mybatis xml寫法

<update id="updateByPrimaryKeySelective" parameterType="...">
    update student
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="gender != null">
        gender = #{gender,jdbcType=TINYINT},
      </if>
      <if test="gmtModified != null">
        gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
</update>

fluent mybatis實現(xiàn)

@Repository
public class StudentDaoImpl extends StudentBaseDao implements StudentDao {
    /**
     * 根據(jù)主鍵更新非空屬性
     *
     * @param student
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(StudentEntity student) {
        return super.defaultUpdater()
            .update.name().is(student.getName(), If::notBlank)
            .set.phone().is(student.getPhone(), If::notBlank)
            .set.email().is(student.getEmail(), If::notBlank)
            .set.gender().is(student.getGender(), If::notNull)
            .end()
            .where.id().eq(student.getId()).end()
            .execute(super::updateBy);
    }    
}

測試

@SpringBootTest(classes = AppMain.class)
public class StudentDaoImplTest extends Test4J {
    @Autowired
    StudentDao studentDao;

    @Test
    void updateByPrimaryKeySelective() {
        StudentEntity student = new StudentEntity()
            .setId(1L)
            .setName("test")
            .setPhone("13866668888");
        studentDao.updateByPrimaryKeySelective(student);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "UPDATE student " +
                "SET gmt_modified = now(), " +
                "name = ?, " +
                "phone = ? " +
                "WHERE id = ?",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"test", "13866668888", 1L});
    }
}

choose 標(biāo)簽

在mybatis中choose when otherwise 標(biāo)簽可以幫我們實現(xiàn) if else 的邏輯。

查詢條件,假設(shè) name 具有唯一性, 查詢一個學(xué)生

  • 當(dāng) id 有值時, 使用 id 進行查詢;
  • 當(dāng) id 沒有值時, 使用 name 進行查詢;
  • 否則返回空

mybatis xml實現(xiàn)

<select id="selectByIdOrName" resultMap="BaseResultMap" parameterType="...">
    select
    <include refid="Base_Column_List" />
    from student
    <where>
        <choose>
          <when test="id != null">
            and id=#{id}
          </when>
          <when test="name != null and name != ''">
            and name=#{name}
          </when>
          <otherwise>
            and 1=2
          </otherwise>
        </choose>
    </where>
</select>

fluent mybatis實現(xiàn)方式

@Repository
public class StudentDaoImpl extends StudentBaseDao implements StudentDao {

    /**
     * 1. 當(dāng) id 有值時, 使用 id 進行查詢;
     * 2. 當(dāng) id 沒有值時, 使用 name 進行查詢;
     * 3. 否則返回空
     */ 
    @Override
    public StudentEntity selectByIdOrName(StudentEntity student) {
       return super.defaultQuery()
           .where.id().eq(student.getId(), If::notNull)
           .and.name().eq(student.getName(), name -> isNull(student.getId()) && notBlank(name))
           .and.apply("1=2", () -> isNull(student.getId()) && isBlank(student.getName()))
           .end()
           .execute(super::findOne).orElse(null);
    }
}

測試

@SpringBootTest(classes = AppMain.class)
public class StudentDaoImplTest extends Test4J {
    @Autowired
    StudentDao studentDao;

    @DisplayName("有 ID 則根據(jù) ID 獲取")
    @Test
    void selectByIdOrName_byId() {
        StudentEntity student = new StudentEntity();
        student.setName("小飛機");
        student.setId(1L);

        StudentEntity result = studentDao.selectByIdOrName(student);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE id = ?",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{1L});
    }

    @DisplayName("沒有 ID 則根據(jù) name 獲取")
    @Test
    void selectByIdOrName_byName() {
        StudentEntity student = new StudentEntity();
        student.setName("小飛機");
        student.setId(null);

        StudentEntity result = studentDao.selectByIdOrName(student);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE name = ?",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{"小飛機"});
    }

    @DisplayName("沒有 ID 和 name, 返回 null")
    @Test
    void selectByIdOrName_null() {
        StudentEntity student = new StudentEntity();
        StudentEntity result = studentDao.selectByIdOrName(student);
        // 驗證執(zhí)行的sql語句
        db.sqlList().wantFirstSql().eq("" +
                "SELECT id, gmt_created, gmt_modified, is_deleted, email, gender, locked, name, phone " +
                "FROM student " +
                "WHERE 1=2",
            StringMode.SameAsSpace);
        // 驗證sql參數(shù)
        db.sqlList().wantFirstPara().eqReflect(new Object[]{});
    }
}

參考

示例代碼地址
Fluent MyBatis地址
Fluent MyBatis文檔
Test4J框架

到此這篇關(guān)于Fluent MyBatis實現(xiàn)動態(tài)SQL的文章就介紹到這了,更多相關(guān)Fluent MyBatis 動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring里的Async注解實現(xiàn)異步操作的方法步驟

    Spring里的Async注解實現(xiàn)異步操作的方法步驟

    這篇文章主要介紹了Spring里的Async注解實現(xiàn)異步操作的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java圖像處理教程之正片疊底效果的實現(xiàn)

    Java圖像處理教程之正片疊底效果的實現(xiàn)

    正片疊底效果是我們平時在Photoshop中會見到的一種效果,下面這篇文章主要給大家介紹了關(guān)于利用Java如何實現(xiàn)正片疊底的效果,分享出來供大家參考學(xué)習(xí),文中給出了詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友可以參考借鑒,下面來一起看看詳細的介紹吧。
    2017-09-09
  • 區(qū)塊鏈常用數(shù)據(jù)庫leveldb用java來實現(xiàn)常規(guī)操作的方法

    區(qū)塊鏈常用數(shù)據(jù)庫leveldb用java來實現(xiàn)常規(guī)操作的方法

    這篇文章主要介紹了區(qū)塊鏈常用數(shù)據(jù)庫leveldb用java來實現(xiàn)常規(guī)操作,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Mysql字段和java實體類屬性類型匹配方式

    Mysql字段和java實體類屬性類型匹配方式

    這篇文章主要介紹了Mysql字段和java實體類屬性類型匹配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java去除中文括號小括號,或者英文括號的實例代碼

    java去除中文括號小括號,或者英文括號的實例代碼

    這篇文章主要介紹了java去除中文括號小括號,或者英文括號的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Spring JPA聯(lián)表查詢之OneToMany源碼解析

    Spring JPA聯(lián)表查詢之OneToMany源碼解析

    這篇文章主要為大家介紹了Spring JPA聯(lián)表查詢之OneToMany源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • application.yml和bootstrap.yml不生效的3種解決方案

    application.yml和bootstrap.yml不生效的3種解決方案

    SpringBoot默認支持?properties(.properties) 和 YAML(.yml .yaml ) 配置文件,本文主要介紹了application.yml和bootstrap.yml不生效的3種解決方案,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Mybatisplus自動填充實現(xiàn)方式及代碼示例

    Mybatisplus自動填充實現(xiàn)方式及代碼示例

    這篇文章主要介紹了Mybatisplus自動填充實現(xiàn)方式及代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)詳解

    Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)詳解

    所有負載均衡的場景幾乎都會用到這個平滑加權(quán)輪詢算法,下面這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • 從零開始:快速入門SpringBoot注解的精髓

    從零開始:快速入門SpringBoot注解的精髓

    Spring?Boot是一個用于快速構(gòu)建基于Spring框架的應(yīng)用程序的開源框架,它通過使用注解來簡化配置和開發(fā)過程,使開發(fā)人員能夠更加專注于業(yè)務(wù)邏輯的實現(xiàn),Spring?Boot提供了許多注解,用于定義和配置應(yīng)用程序的各個方面,需要的朋友可以參考下
    2023-10-10

最新評論

巴南区| 田阳县| 射阳县| 渭南市| 淳安县| 岳阳县| 金堂县| 台北县| 都兰县| 什邡市| 历史| 岳阳县| 平湖市| 新闻| 波密县| 盱眙县| 大埔县| 滦平县| 绥化市| 临桂县| 璧山县| 大悟县| 株洲县| 平乐县| 会同县| 平南县| 兰西县| 道孚县| 营山县| 华蓥市| 上犹县| 莫力| 玉环县| 康平县| 都安| 仁寿县| 琼中| 宝丰县| 宣恩县| 浦城县| 清原|