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

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

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

一、啥是Fluent-Mybatis

與Mybatis-Plus類(lèi)似,是對(duì)Mybaits進(jìn)一步的封裝,使之語(yǔ)法簡(jiǎn)潔明了,更重要的是不需要在自主創(chuàng)建Xml文件,可以只用一個(gè)實(shí)體類(lèi)對(duì)象,通過(guò)代碼生成器,在編譯的過(guò)程中生成所需要的各類(lèi)文件,簡(jiǎn)化了項(xiàng)目的基礎(chǔ)構(gòu)建,提高開(kāi)發(fā)效率。

在這里插入圖片描述

二、SpringBoot + Fluent-Mybatis

1、創(chuàng)建數(shù)據(jù)庫(kù)測(cè)試表

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)建時(shí)間',
    gmt_modified datetime null comment '記錄最后修改時(shí)間',
    is_deleted tinyint(2) default 0 null comment '邏輯刪除標(biāo)識(shí)'
);

2、創(chuàng)建一個(gè)Springboot項(xiàng)目,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驅(qū)動(dòng)-->
        <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依賴(lài)-->
        <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(
            /** 數(shù)據(jù)庫(kù)連接信息 **/
            url = "jdbc:mysql://IP:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai", 
            username = "XXXXXX",
            password = "XXXXXX",
            /** Entity類(lèi)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 {

    }
}

需要注意,默認(rèn)的是MySQL數(shù)據(jù)庫(kù),如果需要其他數(shù)據(jù)庫(kù),需要手動(dòng)配置dbType和driver,其他選項(xiàng)按照需要修改。例如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(
            /** 數(shù)據(jù)庫(kù)連接信息 **/
            dbType= DbType.ORACLE,
            driver= "oracle.jdbc.OracleDriver",
            url = "jdbc:oracle:thin:@IP:1521:orcl",
            username = "XXXXXX",
            password = "XXXXXX",
            /** Entity類(lèi)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方法啟動(dòng)執(zhí)行,生成的項(xiàng)目結(jié)構(gòu),如果在執(zhí)行是出現(xiàn)異常

在這里插入圖片描述

需要暫時(shí)注釋

在這里插入圖片描述

當(dāng)生成好對(duì)應(yīng)文件,項(xiàng)目目錄如下

在這里插入圖片描述

此時(shí)TUserDaoImpl會(huì)有錯(cuò)誤

在這里插入圖片描述

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

在這里插入圖片描述

執(zhí)行過(guò)后,異常消除。此時(shí)你就擁有的對(duì)數(shù)據(jù)庫(kù)此表的強(qiáng)大操作能力。

4、測(cè)試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(){
        /**
         * 分頁(yè)查詢(xún)構(gòu)造條件
         */
        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);
    }

}

三、官方鏈接

官方文檔

官方網(wǎng)站提供了完整切詳細(xì)的構(gòu)建和使用的文檔,本文的內(nèi)容僅為學(xué)習(xí)練習(xí)的Demo

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

相關(guān)文章

最新評(píng)論

长岭县| 长岛县| 蒙自县| 龙口市| 天气| 永康市| 固安县| 宁河县| 凤城市| 五莲县| 汉川市| 阿图什市| 西乡县| 惠安县| 报价| 灌阳县| 泽库县| 宝山区| 广德县| 钟祥市| 舟山市| 绥滨县| 政和县| 舞阳县| 错那县| 丁青县| 博爱县| 贵溪市| 金堂县| 尤溪县| 高唐县| 荔波县| 汉川市| 怀安县| 会东县| 乃东县| 星座| 虹口区| 阳谷县| 东兰县| 威宁|