Official Gitee address
Fluent – Mybatis official documentation
Environment to prepare
- The JDK: 1.8
- MySql: 5.7 (win10 installation tutorial: blog.csdn.net/baidu_36602…)
- Eclipse: 2021 09R
- SpringBoot: 2.6.0 (current stable version)
Quickly create a Springboot project
Official website: start.spring. IO /
Introduce the necessary Maven dependencies
<! -- Introducing fluent-Mybatis runtime dependency package Scope: compile -> <dependency> <groupId>com.github. Atool </groupId> <artifactId> Fluent -mybatis</artifactId> The < version > 1.9.1 < / version > < / dependency > <! -- Introduce Fluent-Mybatis processor, scope set to provider -> <dependency> <groupId>com.github. Atool </groupId> <artifactId> Fluent -mybatis-processor</artifactId> The < version > 1.9.1 < / version > < / dependency > <! -- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.2.0 < / version > < / dependency > <! --mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <! -- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>Copy the code
Eclipse installation lombok
-
The official website to download lombok. Jar (projectlombok.org/download).
-
Run the jar directly (the absolute path to the Java-jar lombok.jar package) as shown:
A screen will then display:Click Install/Update button to Install successfully, restart the IDE to use. Note: If you cannot open Eclipse after installing Lombok, please check whether the Eclipse directory contains Chinese. If so, change it to English.
Create databases and tables in MySql
create table hello_world ( id bigint unsigned auto_increment primary key, say_hello varchar(100) null, Gmt_created datetime DEFAULT NULL COMMENT 'create time ', Gmt_modified datetime DEFAULT NULL COMMENT 'update time ', Is_deleted TINyint (2) DEFAULT 0 COMMENT 'delete') ENGINE = InnoDB CHARACTER SET = utf8 COMMENT 'delete table ';Copy the code
Fluent-mybatis is used to generate the entity class corresponding to the data table
- Create a new test class EntityGeneratorDemo1
Code:
package com.example.demo;
import org.junit.jupiter.api.Test;
import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Tables;
import cn.org.atool.generator.annotation.Table;
public class EntityGeneratorDemo1 {
public static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis? useUnicode=true&characterEncoding=utf8";
@Test
public void generate() throws Exception {
FileGenerator.build(Empty.class);
}
@Tables(
// Set the database connection information
url = url, username = "root", password = "123456".// Set the entity class to generate the SRC directory, relative to user.dir
srcDir = "src/main/java".// Set the package value for the Entity class
basePack = "com.example.demo.entity".// Set the SRC directory for the DAO interface and implementation, relative to user.dir
daoDir = "src/main/java".// Set which tables to generate the Entity file
tables = {@Table(value = {"hello_world"})})static class Empty {}}Copy the code
- Run the test method and refresh the project to see the automatically generated class file:
3. Don’t panic, read on:
- Run the Maven command clean compile to generate the code in target, refresh the project and view the files in target:
5. How to reference the target class file? The official tutorial uses IDEA. Here thanks to the author of Fluent – Mybatis advice:Here’s the solution: Right-click the project name =>>Properties= > >Java Compiler= > >Annotation Processing= > > checkedEnable project specific settings= > > will beGenerated source directoryReplace with/target/generated – sources/annotations
Write a controller to run the project
- Add database information to the application.properties file
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/fluent_mybatis? useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
Copy the code
-
Start by adding web dependencies to the POM file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Copy the code
-
Write DemoController class
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.entity.HelloWorldEntity;
import com.example.demo.entity.mapper.HelloWorldMapper;
import com.example.demo.entity.wrapper.HelloWorldQuery;
import cn.org.atool.fluent.mybatis.model.StdPagedList;
@RestController
public class DemoController {
@Autowired
private HelloWorldMapper maper;
@GetMapping("/hello")
public String hello() {
return "hello";
}
@GetMapping("/list")
public StdPagedList<HelloWorldEntity> list() {
HelloWorldEntity entity = new HelloWorldEntity();
entity.setYourName("xiaomi");
maper.insert(entity);
HelloWorldQuery query = new HelloWorldQuery().orderBy.id().asc().end().limit(0.10);
StdPagedList<HelloWorldEntity> list = maper.stdPagedEntity(query);
returnlist; }}Copy the code
- Startup project error:
The solution is to add the missing bean to the startup class:
- Start the project at http://localhost:8080/list, you’re done:
Pit mining warning: Hot deployment cannot be used in Fluent-Mybatis project. The reason is that hot deployment will start different classloaders, so the same class will be compared into two classes in hot deployment
Gitee address
The above test project Gitee address: https://gitee.com/caojianbang168/fluent-mybatis-demo.git