欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Fluent Mybatis讓你擺脫Xml文件的技巧

 更新時間:2021年08月04日 15:47:39   作者:櫻花、散落的季節(jié)╮  
Fluent-Mybatis類似于Mybatis-Plus是對Mybatis進一步的封裝,可以只用一個實體類對象,通過代碼生成器,在編譯的過程中生成所需要的各類文件,簡化了項目的基礎構建,提高開發(fā)效率,本文重點給大家介紹Fluent Mybaits讓你擺脫Xml文件的技巧,一起看看吧

一、啥是Fluent-Mybatis

與Mybatis-Plus類似,是對Mybaits進一步的封裝,使之語法簡潔明了,更重要的是不需要在自主創(chuàng)建Xml文件,可以只用一個實體類對象,通過代碼生成器,在編譯的過程中生成所需要的各類文件,簡化了項目的基礎構建,提高開發(fā)效率。

在這里插入圖片描述

二、SpringBoot + Fluent-Mybatis

1、創(chuàng)建數據庫測試表

DROP TABLE IF EXISTS `t_user`;
create table `t_user`
(
    id bigint auto_increment comment '主鍵ID' primary key,
    name varchar(30) charset utf8 null comment '姓名',
    age int null comment '年齡',
    email varchar(50) charset utf8 null comment '郵箱',
    gmt_create datetime null comment '記錄創(chuàng)建時間',
    gmt_modified datetime null comment '記錄最后修改時間',
    is_deleted tinyint(2) default 0 null comment '邏輯刪除標識'
);

2、創(chuàng)建一個Springboot項目,pom文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mybatis.fluent</groupId>
    <artifactId>fluent_mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>fluent_mybatis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <fluent.mybatis.version>1.5.6</fluent.mybatis.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--JDBC驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- fluent mybatis依賴-->
        <dependency>
            <groupId>com.github.atool</groupId>
            <artifactId>fluent-mybatis</artifactId>
            <version>${fluent.mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.atool</groupId>
            <artifactId>fluent-mybatis-processor</artifactId>
            <version>${fluent.mybatis.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3、代碼生成器

import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;

public class AppEntityGenerator {

    public static void main(String[] args) {
        FileGenerator.build(Abc.class);
    }

    @Tables(
            /** 數據庫連接信息 **/
            url = "jdbc:mysql://IP:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai", 
            username = "XXXXXX",
            password = "XXXXXX",
            /** Entity類parent package路徑 **/
            basePack = "com.mybatis.fluent.fluent_mybatis",
            /** Entity代碼源目錄 **/
            srcDir = "/src/main/java",
            /** Dao代碼源目錄 **/
            daoDir = "/src/main/java",
            /** 如果表定義記錄創(chuàng)建,記錄修改,邏輯刪除字段 **/
            gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted",
            /** 需要生成文件的表 **/
            tables = @Table(value = {"t_user"})
    )
    static class Abc {

    }
}

需要注意,默認的是MySQL數據庫,如果需要其他數據庫,需要手動配置dbType和driver,其他選項按照需要修改。例如oralce

import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import cn.org.atool.generator.database.DbType;

public class AppEntityGenerator {

    public static void main(String[] args) {
        FileGenerator.build(Abc.class);
    }

    @Tables(
            /** 數據庫連接信息 **/
            dbType= DbType.ORACLE,
            driver= "oracle.jdbc.OracleDriver",
            url = "jdbc:oracle:thin:@IP:1521:orcl",
            username = "XXXXXX",
            password = "XXXXXX",
            /** Entity類parent package路徑 **/
            basePack = "com.mybatis.fluent.fluent_mybatis",
            /** Entity代碼源目錄 **/
            srcDir = "/src/main/java",
            /** Dao代碼源目錄 **/
            daoDir = "/src/main/java",
            /** 如果表定義記錄創(chuàng)建,記錄修改,邏輯刪除字段 **/
            gmtCreated = "INSERT_DATE_TIME",
            /** 需要生成文件的表 **/
            tables = @Table(value = {"table_name"})
    )
    static class Abc {

    }
}

main方法啟動執(zhí)行,生成的項目結構,如果在執(zhí)行是出現異常

在這里插入圖片描述

需要暫時注釋

在這里插入圖片描述

當生成好對應文件,項目目錄如下

在這里插入圖片描述

此時TUserDaoImpl會有錯誤

在這里插入圖片描述

此時將需要將之前注釋的provided打開,在執(zhí)行

在這里插入圖片描述

執(zhí)行過后,異常消除。此時你就擁有的對數據庫此表的強大操作能力。

4、測試CURD

import cn.org.atool.fluent.mybatis.model.IPagedList;
import cn.org.atool.fluent.mybatis.model.StdPagedList;
import com.mybatis.fluent.fluent_mybatis.dao.impl.HospitalinfoDaoImpl;
import com.mybatis.fluent.fluent_mybatis.dao.impl.TUserDaoImpl;
import com.mybatis.fluent.fluent_mybatis.entity.HospitalinfoEntity;
import com.mybatis.fluent.fluent_mybatis.entity.TUserEntity;
import com.mybatis.fluent.fluent_mybatis.helper.TUserWrapperHelper;
import com.mybatis.fluent.fluent_mybatis.wrapper.HospitalinfoQuery;
import com.mybatis.fluent.fluent_mybatis.wrapper.TUserQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@SpringBootTest
class FluentMybatisApplicationTests {

    @Resource
    private TUserDaoImpl dao;

    @Test
    void add() {
        TUserEntity entity = new TUserEntity().setAge(20).setEmail("122@163.com").setName("lisi").setIsDeleted(false);
        Serializable id = dao.save(entity);
        System.out.println("插入后返回的主鍵ID為:" + id);
    }

    @Test
    void select(){
        /**
         * 分頁查詢構造條件
         */
        TUserQuery query = TUserQuery.query().limit(0,1);
        List<TUserEntity> list = dao.listEntity(query);
        System.out.println(list);
    }

    @Test
    void upData(){
        TUserEntity entity = dao.selectById(1L);
        System.out.println(entity);
        entity.setAge(2000).setEmail("120098922@qq.com").setName("lisi111").setIsDeleted(true);
        System.out.println(entity);
        boolean b = dao.updateById(entity);
        System.out.println(b);
    }

    @Test
    void delete(){
        boolean b = dao.deleteById(1L);
        System.out.println(b);
        TUserQuery query = TUserQuery.query();
        List<TUserEntity> list = dao.listEntity(query);
        System.out.println(list);
    }

}

三、官方鏈接

官方文檔

官方網站提供了完整切詳細的構建和使用的文檔,本文的內容僅為學習練習的Demo

到此這篇關于Fluent Mybatis讓你擺脫Xml文件的技巧的文章就介紹到這了,更多相關Fluent Mybatis Xml文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java如何使用interrupt()終止線程

    Java如何使用interrupt()終止線程

    這篇文章主要介紹了Java如何使用interrupt()終止線程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Java表單重復提交的避免方法

    Java表單重復提交的避免方法

    如何避免表單重復提交,這篇文章就為大家詳細介紹了Java表單重復提交的避免方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Java實現自動生成縮略圖片

    Java實現自動生成縮略圖片

    這篇文章主要為大家詳細介紹了Java實現自動生成縮略圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解Spring-Boot集成Spring session并存入redis

    詳解Spring-Boot集成Spring session并存入redis

    這篇文章主要介紹了詳解Spring-Boot集成Spring session并存入redis,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Java阻塞隊列BlockingQueue詳解

    Java阻塞隊列BlockingQueue詳解

    這篇文章主要介紹了Java阻塞隊列BlockingQueue,文章通過隊列的類型展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • SpringBoot后端數據校驗實戰(zhàn)操作指南

    SpringBoot后端數據校驗實戰(zhàn)操作指南

    在項?開發(fā)中,對于前端提交的表單,后臺接?接收到表單數據后,為了保證程序的嚴謹性,通常后端會加?業(yè)務參數的合法校驗操作來避免程序的?技術性?bug,這篇文章主要給大家介紹了關于SpringBoot后端數據校驗的相關資料,需要的朋友可以參考下
    2022-07-07
  • SpringBoot如何優(yōu)雅的處理重復請求

    SpringBoot如何優(yōu)雅的處理重復請求

    對于一些用戶請求,在某些情況下是可能重復發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復了,可能會導致很嚴重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復請求的方法,需要的朋友可以參考下
    2023-12-12
  • Spring Boot企業(yè)常用的starter示例詳解

    Spring Boot企業(yè)常用的starter示例詳解

    這篇文章主要給大家介紹了關于Spring Boot企業(yè)常用starter的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • spring?jpa?審計功能自定義填充字段方式

    spring?jpa?審計功能自定義填充字段方式

    這篇文章主要介紹了spring?jpa審計功能自定義填充字段方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java讓多線程按順序執(zhí)行的幾種方法

    Java讓多線程按順序執(zhí)行的幾種方法

    本文主要介紹了Java讓多線程按順序執(zhí)行的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05

最新評論