FluentMybatis
1.创建项目
赘述无意义
2.导入依赖
<?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>
<groupId>com.example</groupId>
<artifactId>fluent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fluent</name>
<description>fluent</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<fluent-mybatis.version>1.9.7</fluent-mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.fluent.FluentApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.application.yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://127.0.0.1/fluentmybatis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true
username: root
password: 123456
jackson:
time-zone: GMT+8
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.编写entity
TMemberEntity.java
@FluentMybatis
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TMemberEntity extends RichEntity implements IEntity {
@TableId
private Long id;
private String userName;
private Byte isGirl;
private Integer age;
private String school;
@TableField(insert = "now()")
private String gmtCreated;
@TableField(insert = "now()",update = "now()")
private String gmtModified;
private Boolean idDeleted;
}
CountyDivisionEntity.java
@FluentMybatis
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CountyDivisionEntity extends RichEntity implements IEntity {
@TableId
private Long id;
private String province;
private String county;
private String city;
private Boolean isDeleted;
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField(insert = "now()", update = "now()")
private Date gmtCreate;
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField(update = "now()")
private Date gmtModified;
}
StudentEntity.java
@FluentMybatis
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class StudentEntity extends RichEntity implements IEntity {
@TableId
private Long id;
private Integer age;
private Integer grade;
private String userName;
private Byte genderMan;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
private String phone;
private Long bonusPoints;
private String status;
private Long homeCountyId;
private String address;
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField(insert = "now()",update = "now()")
private Date gmtCreated;
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField(update = "now()")
private Date gmtModified;
// @TableField(value = "1")
private Boolean isDeleted;
}
StudentScoreEntity.java
@FluentMybatis
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class StudentScoreEntity extends RichEntity implements IEntity {
@TableId
private Long id;
private Long studentId;
private Byte genderMan;
private Integer schoolTerm;
private String subject;
private Integer score;
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField(insert = "now()" , update = "now()")
private Date gmtCreate;
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField(update = "now()")
private Date gmtModified;
private Boolean isDeleted;
}
TMemberFavoriteEntity.java
@FluentMybatis
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TMemberFavoriteEntity extends RichEntity implements IEntity {
@TableId
private Long id;
private Long memberId;
private Byte favorite;
@TableField(insert = "now()")
private String gmtCreated;
@TableField(insert = "now()",update = "now()")
private String gmtModified;
private Boolean idDeleted;
}
TMemberLoveEntity.java
@FluentMybatis
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TMemberLoveEntity extends RichEntity implements IEntity {
@TableId
private Long id;
private Long girlId;
private Long boyId;
private String status;
@TableField(insert = "now()")
private String gmtCreated;
@TableField(insert = "now()",update = "now()")
private String gmtModified;
private Boolean idDeleted;
}
5.build一下(!!!!很重要!!!)
6.开始写数据库交互代码
这里我直接将业务逻辑写在了controller中,实际上这是不符合规范的,不要学习,我是图个简单才这么做的,同学们学习的时候还是要遵循规则
ExistsSelectController.java
@RestController
public class ExistsSelectController {
@Resource
private StudentMapper studentMapper;
@GetMapping("existsQuery")
public List<StudentEntity> existsQuery(){
StudentQuery query = new StudentQuery()
.where.isDeleted().isFalse()
.and.exists(new StudentScoreQuery()
.selectId()
.where.isDeleted().isFalse()
.and.schoolTerm().eq(2019)
.and.score().lt(60)
.and.subject().in(new String[]{"语文","数学"})
.and.studentId().apply("= student.id")
.end()
).end();
List<StudentEntity> list = studentMapper.listEntity(query);
return list;
}
}
InnerJoinController.java
@RestController
public class InnerJoinController {
@Resource
private StudentMapper studentMapper;
//两张表join查询
@GetMapping("innerJoinSelect")
public List<Map<String, Object>> innerJoinSelect(){
JoinQuery query = JoinBuilder
.<StudentQuery>from(new StudentQuery()
.select.userName().age().genderMan().end()
.where.isDeleted().isFalse()
.and.genderMan().eq(1).end())
.join(new CountyDivisionQuery()
.select.province().city().county().end()
.where.isDeleted().isFalse()
.and.province().eq("浙江省")
.and.city().eq("杭州市").end())
.on(l -> l.where.homeCountyId(), r -> r.where.id()).endJoin()
.build();
List<Map<String, Object>> maps = studentMapper.listMaps(query);
return maps;
}
//三张表join查询
@GetMapping ("threeTableQuery")
public List<Map<String, Object>> threeTableQuery(){
JoinQuery query = JoinBuilder
.<StudentQuery>from(new StudentQuery()
.select.userName().end()
.where.isDeleted().isFalse().end())
.join(new CountyDivisionQuery()
.where.isDeleted().isFalse()
.and.province().eq("浙江省 ")
.and.city().eq("杭州市").end())
.on(l -> l.where.homeCountyId(), r -> r.where.id()).endJoin()
.join(new StudentScoreQuery()
.select.subject().score().end()
.where.isDeleted().isFalse()
.and.schoolTerm().eq(2019)
.and.subject().in(new String[] {"语文 ", "数学 "})
.and.score().ge(90).end())
.on(l -> l.where.id(),r -> r.where.studentId()).endJoin()
.build();
List<Map<String, Object>> maps = studentMapper.listMaps(query);
return maps;
}
}
StudentInfoController.java
@RestController
@RequestMapping
public class StudentInfoController {
@Resource
private StudentMapper studentMapper;
//查--复合查询
@GetMapping("getStudentData")
public List<StudentEntity> getStudentData() {
StudentQuery query = new StudentQuery()
.where.isDeleted().isFalse()
.and.grade().eq(4)
.and.homeCountyId().in(new CountyDivisionQuery()
.selectId()
.where.isDeleted().isFalse()
.and.province().eq("浙江省")
.and.city().eq("杭州市")
.end()
).end();
List<StudentEntity> students = studentMapper.listEntity(query);
// System.out.println(students.toString());
return students;
}
//增
@PostMapping("saveStudentData")
public String saveStudentData(){
Calendar calendar = Calendar.getInstance();
calendar.set(1999,Calendar.NOVEMBER,12);
Date birthday = calendar.getTime();
StudentEntity studentEntity = StudentEntity.builder()
.age(18)
.grade(3)
.userName("lisi")
.genderMan((byte) 1)
.birthday(birthday)
.phone("13329987756")
.bonusPoints(9998L)
.status("在校")
.homeCountyId(2L)
.address("浙江省绍兴市柯桥区大鱼街道134号17栋1123室")
.gmtCreated(new Date())
.gmtModified(new Date())
.isDeleted(false)
.build();
int insertStatus = studentMapper.insert(studentEntity);
if(insertStatus > 0){
return "insert success";
}else {
return "insert fail";
}
}
//改
@PutMapping("updateStudentDate")
public String updateStudentData(){
StudentEntity studentEntity = StudentEntity.builder()
.id(2L)
.age(19)
.grade(4)
.userName("lisi-update")
.phone("13329987756")
.bonusPoints(99L)
.status("离校")
.gmtModified(new Date())
.isDeleted(false)
.build();
int updateStatus = studentMapper.updateById(studentEntity);
if(updateStatus > 0){
return "update success";
}else {
return "update fail";
}
}
//删
@DeleteMapping("deleteStudentData")
public String deleteStudentData(){
int i = studentMapper.deleteById(2);
if (i > 0) {
return "delete success";
} else {
return "delete fail";
}
}
做到这里似乎都完成了,不对!有两个很重要的地方得注意
(1).@mapperScan注解必须扫描的是mapper包,不可以为dao包
(2).得加入一个bean,我直接放在启动类了,这个问题不大
@Bean
public MapperFactory mapperFactory(){
return new MapperFactory();
}
7.完结撒花