服务器之家

服务器之家 > 正文

Fluent Mybatis让你摆脱Xml文件的技巧

时间:2021-11-05 10:09     来源/作者:樱花、散落的季节╮

一、啥是fluent-mybatis

与mybatis-plus类似,是对mybaits进一步的封装,使之语法简洁明了,更重要的是不需要在自主创建xml文件,可以只用一个实体类对象,通过代码生成器,在编译的过程中生成所需要的各类文件,简化了项目的基础构建,提高开发效率。

Fluent Mybatis让你摆脱Xml文件的技巧

二、springboot + fluent-mybatis

1、创建数据库测试表

?
1
2
3
4
5
6
7
8
9
10
11
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文件如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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、代码生成器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_45624560/article/details/117692600

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部