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 文件,之前报错的类不再提示相关错误信息。 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot集成mybatis框架的步骤如下: 1. 首先,在项目的pom.xml文件中添加mybatis的依赖。你可以使用以下的依赖配置来添加mybatis-spring-boot-starter依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> ``` 2. 然后,在springboot的启动类上添加@MapperScan注解,指定mapper接口的包名,让springboot能够扫描到这些接口: ```java @MapperScan(basePackages = "mapper类所放的包名") @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 3. 接下来,需要进行mybatis的配置。在application.properties或application.yml文件中添加mybatis的相关配置: ```yaml # mybatis配置 mybatis: check-config-location: true # 检查mybatis的配置文件位置 config-location: "classpath:mybatis/mybatis-config.xml" # mybatis的配置文件路径 mapper-locations: "classpath:mybatis/mapper/*Mapper.xml" # mapper接口的xml文件路径 type-aliases-package: "com.example.awesomespring.dao.entity.*" # mapper接口对应的实体类路径 ``` 通过以上步骤,你就成功地集成mybatis框架springboot项目中。这样,你就可以使用mybatis来进行数据库的持久化操作了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [手把手教你springboot集成mybatis](https://blog.csdn.net/Trouvailless/article/details/126315399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值