亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:50日期:2023-10-18 12:35:09
目錄一、啥是Fluent-Mybatis二、SpringBoot + Fluent-Mybatis三、官方鏈接一、啥是Fluent-Mybatis

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

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

二、SpringBoot + Fluent-Mybatis

1、創建數據庫測試表

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

2、創建一個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', /** 如果表定義記錄創建,記錄修改,邏輯刪除字段 **/ 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', /** 如果表定義記錄創建,記錄修改,邏輯刪除字段 **/ gmtCreated = 'INSERT_DATE_TIME', /** 需要生成文件的表 **/ tables = @Table(value = {'table_name'}) ) static class Abc { }}

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

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

需要暫時注釋

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

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

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

此時TUserDaoImpl會有錯誤

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

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

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

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

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;@SpringBootTestclass FluentMybatisApplicationTests { @Resource private TUserDaoImpl dao; @Test void add() {TUserEntity entity = new TUserEntity().setAge(20).setEmail('[email protected]').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('[email protected]').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文件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 久久夜色撩人精品国产 | 中文在线播放 | 久久一区精品 | 最新九九精品 | 国产精品日韩高清在线蜜芽 | 国产免费高清视频在线观看不卡 | 无遮挡啪啪成人免费网站 | 久久久久777777人人人视频 | 小优视频在线观看网 | 欧美最黄视频 | 麻豆视频免费 | 开心午夜婷婷色婷在线 | 国产成人免费高清视频 | 日韩欧美久久一区二区 | 亚洲一级视频在线观看 | 欧美久久精品 | 精品一区二区三区在线观看l | 五月天婷婷视频 | 久在草视频| 国产精品嫩草视频永久网址 | 亚洲精品字幕一区二区三区 | 国产精品免费入口视频 | 亚洲在线免费观看视频 | 1024在线视频精品免费 | 成人久久18免费软件 | 国产爱视频 | 精品一区二区三区在线观看l | 欧美一级做a爰片免费 | 国产高清免费午夜在线视频 | 午夜精品在线视频 | 九九九国产在线 | 丁香综合 | 日本国产欧美色综合 | 免费一级毛片在线播放傲雪网 | 一级毛片女学护士 | 有码中文字幕 | 国产 网红 喷水 播放 | 在线成年人网站 | 亚洲夜色综合久久 | 亚洲视频污 | 国产亚洲精品久久久久91网站 |