SpringBoot 集成FluentMybatis 框架

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 集成FluentMybatis 框架相关的知识,希望对你有一定的参考价值。

FluentMybatis特性

FluentMybatis原理

 项目搭建

pom.xml 添加fluent-mybatis依赖

   <properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<fluent-mybatis.version>1.8.7</fluent-mybatis.version>
	</properties>

	<dependencies>

		<!-- 集成fluent-mybatis -->
		<!-- 引入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>
			<scope>provided</scope>
			<version>${fluent-mybatis.version}</version>
		</dependency>
	
	</dependencies>

完整pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.digipower</groupId>
	<artifactId>Single</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<fluent-mybatis.version>1.8.7</fluent-mybatis.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- 集成fluent-mybatis -->
		<!-- 引入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>
			<scope>provided</scope>
			<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>

		<!-- 集成mysql 驱动包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.11</version>
		</dependency>


		<!--集成lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- 集成guava 工具包 -->
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>30.1.1-jre</version>
		</dependency>
		<!-- 集成hutool-all 工具包 -->
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.5.2</version>
		</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>

表构建 

在数据库创建一张用户表。sql如下:

CREATE TABLE `ucas_auth_user`  (
  `sid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '主键',
  `user_pin` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '登录名',
  `user_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
  `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  `gender` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别(1:男,2:女)',
  `tel` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '常用电话',
  `phone` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '其他联系电话',
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电子邮件',
  `state` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '2' COMMENT '状态(1:禁用,2:启用)',
  `created_by` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建人',
  `created_dt` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `version` int(9) NULL DEFAULT 1 COMMENT '版本号',
  `updated_by` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新人',
  `updated_dt` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  `zone_org_code` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '区域机构',
  `organiztion_sid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '部门sid',
  `value1` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备用字段1',
  `value2` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备用字段2',
  `value3` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备用字段3',
  `delete_flag` int(1) NULL DEFAULT 1 COMMENT '删除标识(1:未删除,2:已删除)',
  `session_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '会话Id',
  `user_category` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户类别(系统管理员\\\\安全保密管理员\\\\安全审计员\\\\普通用户)',
  `unique_sid` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '操作日志记录uuid',
  PRIMARY KEY (`sid`) USING BTREE,
  UNIQUE INDEX `user_pin`(`user_pin`) USING BTREE COMMENT '账户名唯一性',
  UNIQUE INDEX `phone`(`phone`) USING BTREE COMMENT '手机号码唯一性'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
INSERT INTO `ucas_auth_user` VALUES ('1', 'superadmin', '超级管理员修改', '1', NULL, '', '18976390251', NULL, '2', NULL, NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, '2', NULL);
INSERT INTO `ucas_auth_user` VALUES ('2', '15815565311', '李朋鲜', '1', NULL, NULL, '15815565318', NULL, '2', NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, '1', NULL);
INSERT INTO `ucas_auth_user` VALUES ('3', '13713876102', '刘迅', '1', NULL, NULL, '13713876108', NULL, '2', NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, '1', NULL);
INSERT INTO `ucas_auth_user` VALUES ('4', '13392156623', '黄俊卿', '1', NULL, NULL, '13392156626', NULL, '2', NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, '1', NULL);

代码生成工具类 

注意:放到测试代码包中。结构如下图:

按照官方提供的简单样例来,生成代码如下:

package com.single;

import org.junit.Test;

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

public class EntityGenerator {
	// 数据源 url
	static final String url = "jdbc:mysql://127.0.0.1:3306/banan_test?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true";
	// 数据库用户名
	static final String username = "root";
	// 数据库密码
	static final String password = "123456";

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

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

}

 执行代码生成工具,生成如下包:

解决类找不到问题 

官方提供的解决方法

 简单表述:打开cmd 窗口,切换至Maven 项目所在文件夹地址,执行:mvn compile 

C:\\workspace\\Single>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.digipower:Single >------------------------
[INFO] Building Single 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-parent/2.1.9.RELEASE/spring-boot-parent-2.1.9.RELEASE.pom
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-parent/2.1.9.RELEASE/spring-boot-parent-2.1.9.RELEASE.pom (1.8 kB at 1.7 kB/s)
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-maven-plugin/2.1.9.RELEASE/spring-boot-maven-plugin-2.1.9.RELEASE.jar
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-maven-plugin/2.1.9.RELEASE/spring-boot-maven-plugin-2.1.9.RELEASE.jar (68 kB at 103 kB/s)
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ Single ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ Single ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\\workspace\\Single\\target\\classes
[INFO] /C:/workspace/Single/target/generated-sources/annotations/com/single/entity/helper/UcasAuthUserMapping.java:  某些输入文件使用了未经检查或不安全的操作。
[INFO] /C:/workspace/Single/target/generated-sources/annotations/com/single/entity/helper/UcasAuthUserMapping.java:  有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.832 s
[INFO] Finished at: 2021-11-03T11:08:32+08:00
[INFO] ------------------------------------------------------------------------

 编译结束后我们可以在target中,找到报错包位置中的编译文件。

拷贝FluentMybatis 框架生成的Java 文件,之前报错的类不再提示相关错误信息。 

以上是关于SpringBoot 集成FluentMybatis 框架的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成Kafka

SpringBoot.03.SpringBoot集成jsp

SpringBoot.03.SpringBoot集成jsp

SpringBoot.03.SpringBoot集成jsp

SpringBoot使用·上篇(SpringBoot注解+集成JDBC+集成druid(德鲁伊)+sql监控)

SpringBoot使用·上篇(SpringBoot注解+集成JDBC+集成druid(德鲁伊)+sql监控)