fluent-mybatis教程
1.引入依赖****
<properties>
<java.version>8</java.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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<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>
<scope>provided</scope>
<version>${fluent-mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
2.配置mapper
@Configuration
@MapperScan({"com.jpa.mapper"})
public class FlutMybatisConfig {
@Bean
public MapperFactory mapperFactory(){
return new MapperFactory();
}
}
** 3.自动生成代码**
package com.jpa;
import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import org.junit.Test;
/**
* FluentMybatis的Entity代码生成
*/
public class EntityGenerator {
//数据源url
static final String url = "jdbc:mysql://localhost:3306/sakila?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
//数据库用户名
static final String username = "root";
//数据库密码
static final String password = "123456";
@Test
public void generate() throws Exception {
FileGenerator.build(User.class);
}
@Tables(
//设置数据库连接信息
url = url,username = username,password = password,
srcDir = "src/main/java",
basePack = "com.jpa",
daoDir = "src/main/java",
tables = {@Table(value = "ucas_auth_user")}
)
static class User{
}
}
4.controller实现
package com.jpa.controller;
import com.jpa.entity.UcasAuthUserEntity;
import com.jpa.mapper.UcasAuthUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UcasAuthUserEntityController {
@Autowired
private UcasAuthUserMapper ucasAuthUserMapper;
@GetMapping("/query")
public UcasAuthUserEntity query(){
UcasAuthUserEntity entity = ucasAuthUserMapper.findOne(ucasAuthUserMapper.query()
.where.phone().eq("18976390251")
.end());
return entity;
}
}
**````
**5.数据源yml配置**
````json
server:
port: 7777
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/sakila?characterEncoding=utf8&serverTimezone=UTC
username: root
password: 123456
6.表结构
-- sakila.ucas_auth_user definition
CREATE TABLE `ucas_auth_user` (
`sid` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL COMMENT '主键',
`user_pin` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '登录名',
`user_name` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '用户名',
`password` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '密码',
`gender` varchar(4) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '性别(1:男,2:女)',
`tel` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '常用电话',
`phone` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '其他联系电话',
`email` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '电子邮件',
`state` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT '2' COMMENT '状态(1:禁用,2:启用)',
`created_by` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '创建人',
`created_dt` datetime DEFAULT NULL COMMENT '创建时间',
`version` int DEFAULT '1' COMMENT '版本号',
`updated_by` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '更新人',
`updated_dt` datetime DEFAULT NULL COMMENT '更新时间',
`zone_org_code` varchar(32) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '区域机构',
`organiztion_sid` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '部门sid',
`value1` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '备用字段1',
`value2` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '备用字段2',
`value3` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '备用字段3',
`delete_flag` int DEFAULT '1' COMMENT '删除标识(1:未删除,2:已删除)',
`session_id` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '会话Id',
`user_category` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '用户类别(系统管理员\\安全保密管理员\\安全审计员\\普通用户)',
`unique_sid` varchar(64) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL COMMENT '操作日志记录uuid',
PRIMARY KEY (`sid`) USING BTREE,
UNIQUE KEY `user_pin` (`user_pin`) USING BTREE COMMENT '账户名唯一性',
UNIQUE KEY `phone` (`phone`) USING BTREE COMMENT '手机号码唯一性'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC COMMENT='用户表';
** 7.数据库新增脚步**
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);
**8.效果 **