Spring Boot 简单整合 fluent-mybatis 实现数据的增删改查

总是喜欢去关注更多的新框架,发现了一个基本上不用写mapper和xml的框架。让我们来研究一下这个框架吧。

1. 新建Spring Boot项目

1.1 pom.xml配置
<properties>
	<java.version>1.8</java.version>
	<fluent-mybatis.version>1.6.13</fluent-mybatis.version>
</properties>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</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>
	</dependency>

	<!-- 引入fluent-mybatis 运行依赖包, scope为compile -->
	<dependency>
		<groupId>com.github.atool</groupId>
		<artifactId>fluent-mybatis</artifactId>
		<version>${fluent-mybatis.version}</version>
	</dependency>
	<!-- 引入fluent-mybatis-processor, scope设置为provider 编译需要,运行时不需要 -->
	<dependency>
		<groupId>com.github.atool</groupId>
		<artifactId>fluent-mybatis-processor</artifactId>
		<version>${fluent-mybatis.version}</version>
	</dependency>

	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>2.2.0</version>
	</dependency>
</dependencies>

我们这里引入了fluent-mybatis-processor就是想自动生成代码,尽量减少我们自己写代码。

1.2 application.yml配置
server:
  port: 8080 # 端口号
spring:
  datasource: # 数据库参数配置
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://IP:3306/数据库名称?useUnicode=true&characterEncoding=utf8
    username: 用户名
    password: 密码
1.3 EntityGeneratorTests.java 自动代码生成

这个我尝试过在非Test里面用main里面运行,结果总是失败,有可以运行的请告诉我哈!

@SpringBootTest
public class EntityGeneratorTests {

    // 数据源 url
    static final String url = "jdbc:mysql://IP:3306/数据库名称?useUnicode=true&characterEncoding=utf8";
    // 数据库用户名
    static final String username = "用户名";
    // 数据库密码
    static final String password = "密码";

    @Test
    public void generate() {
        // 引用配置类,build方法允许有多个配置类
        FileGenerator.build(Empty.class);
    }

    @Tables(
            // 设置数据库连接信息
            url = url, username = username, password = password,
            // 设置entity类生成src目录, 相对于 user.dir
            srcDir = "src/main/java",
            // 设置entity类的package值
            basePack = "xyz.zhouzhaodong.fluentmybatis",
            // 设置dao接口和实现的src目录, 相对于 user.dir
            daoDir = "src/main/java",
            // 设置哪些表要生成Entity文件
            tables = {@Table(value = {"user"})}
    )
    static class Empty { //类名随便取, 只是配置定义的一个载体
    }

}

运行后生成如下三个文件:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yltP5Ui8-1628237445499)(http://www.zhouzhaodong.xyz/upload/2021/08/image-5fc79cfbcceb4550b4fc379743d2cc7c.png)]

1.4 生成代码后编译会在target目录生成代码

image.png
运行后生成的代码如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EPuiWTZd-1628237445503)(http://www.zhouzhaodong.xyz/upload/2021/08/image-f8c28bcf966348c1bbe38e79eb80148e.png)]

1.5 启动类配置 @MapperScan

@MapperScan地址为上面target目录下的mapper
可参考我的:
image.png

1.6 新建UserController测试

可以参考官方文档进行研究,我这里只是简单的进行逻辑的实现。
文档地址为:
https://gitee.com/fluent-mybatis/fluent-mybatis/wikis/fluent%20mybatis%E7%89%B9%E6%80%A7%E6%80%BB%E8%A7%88?sort_id=4216053

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserDao userDao;

    @Resource
    UserMapper userMapper;

    /**
     * 根据ID查询数据1
     * @param id
     * @return
     */
    @GetMapping("/getByIdOne")
    public UserEntity getByIdOne(Integer id){
        return userDao.selectById(id);
    }

    /**
     * 根据ID查询数据2
     * @param id
     * @return
     */
    @GetMapping("/getByIdTwo")
    public UserEntity getByIdTwo(Integer id){
        UserQuery query = new UserQuery().where.id().eq(id).end();
        return userMapper.findOne(query);
    }

    /**
     * 根据ID删除
     * @param id
     */
    @GetMapping("/deleteById")
    public void deleteById(Integer id){
        userDao.deleteById(id);
    }

    /**
     * 根据ID进行更新
     * @param userEntity
     * @return
     */
    @PostMapping("/updateById")
    public UserEntity updateById(@RequestBody UserEntity userEntity){
        boolean b = userDao.updateById(userEntity);
        if (b){
            return userDao.selectById(userEntity.getId());
        }
        return null;
    }

    /**
     * 新增
     * @param userEntity
     * @return
     */
    @PostMapping("/insert")
    public Integer insert(@RequestBody UserEntity userEntity){
        return userDao.save(userEntity);
    }
}

接下来进行测试:

1.6.1 getByIdOne

image.png

1.6.2 getByTwo

image.png

1.6.3 deleteById

image.png

1.6.4 insert

image.png

1.6.5 updateById

image.png
简单测试通过!

源代码地址:

https://gitee.com/zhouzhaodong/springboot/tree/master/fluent-mybatis

个人博客地址:

http://www.zhouzhaodong.xyz/

### 回答1: fluent-mybatis是一个对MyBatis进行封装的库,使其使用起来更简单、更符合领域驱动设计(DDD)的理念。因此,fluent-mybatis通常被用于开发各种类型的项目中的数据访问层。我不知道是否有任何特别大的项目使用fluent-mybatis作为数据访问层的框架,如果有的话,我不能确定。 ### 回答2: Fluent-Mybatis是一个基于Mybatis的ORM框架,它提供了一种方便、灵活的方式来操作数据库。它的设计目标是简化数据库操作的流程,提升开发效率,并且能够应对大型项目的需求。 在大型项目中,数据量通常很大,操作复杂,所以需要一个高效的ORM框架来处理数据库。Fluent-Mybatis提供了灵活的查询方式,支持动态SQL,通过自动生成SQL语句,将复杂的数据库操作简化为简洁易懂的代码。同时,它还支持多种数据库,如MySQL、Oracle、SQL Server等,可以根据项目需要灵活选择。 在大型项目中,数据更新频繁,且可能涉及到多个表之间的关联操作。Fluent-Mybatis提供了事务支持和乐观锁机制,保证数据的一致性和并发操作的正确性。通过使用Fluent-Mybatis,开发人员可以方便地管理事务,实现数据的原子性操作,提高系统的稳定性和安全性。 此外,Fluent-Mybatis还提供了一些扩展功能,如分页查询、多条件查询、缓存支持等。这些功能在大型项目中尤其重要,能够提升系统的性能和用户体验。 总之,Fluent-Mybatis适用于各种大型项目,无论是电子商务、金融、物流等领域的系统,都可以通过使用它来简化数据库操作,提高开发效率,保证系统的稳定性和性能。 ### 回答3: Fluent-Mybatis是一个简化MyBatis开发的框架,它提供了一种更流畅、更便捷的方式来进行数据库操作。对于大型项目而言,Fluent-Mybatis具有以下优势和适用场景。 首先,Fluent-Mybatis具有简化SQL编写的功能。在大型项目中,通常需要编写大量的SQL语句来进行数据库操作,包括查询、插入、更新和删除等。Fluent-Mybatis可以通过提供一种更直观的API来简化SQL的编写,使得开发人员可以更快速地完成数据库操作。 其次,Fluent-Mybatis支持动态SQL的构建。在大型项目中,经常需要根据不同的条件来构建各种不同的SQL语句,以满足复杂的业务需求。Fluent-Mybatis提供了动态SQL的支持,可以根据不同的条件来动态构建SQL语句,从而提高了开发的灵活性和效率。 另外,Fluent-Mybatis还支持多种数据库的操作。在大型项目中,可能需要同时操作多个不同类型的数据库,如MySQL、Oracle、SQL Server等。Fluent-Mybatis提供了对多种数据库的支持,可以方便地进行数据库的切换和操作。 此外,Fluent-Mybatis还提供了缓存和性能优化的功能。在大型项目中,性能往往是一个非常关键的因素。Fluent-Mybatis支持对查询结果进行缓存,从而提高了查询的性能。同时,Fluent-Mybatis还提供了一些性能优化的策略和设置,帮助开发人员更好地优化数据库操作的性能。 总之,Fluent-Mybatis是一个适用于大型项目的框架,它通过简化SQL编写、支持动态SQL构建、多数据库操作和性能优化等功能,提高了开发人员在大型项目中的开发效率和数据库操作的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值