为什么要使用FluentMybatis
Fluent Mybatis比较吸引人的地方正如它所说的,1、No XML,No Mapper, No If else,No String
2、只需要Entity就可以实现强大的FluentAPI:支持分页、嵌套查询,AND OR组合,聚合函数...
第一步:添加依赖
注意:1、不可少了mybatis和springboot的整合包;2、junit依赖默认生效限制
<!--mybatis和springboot的整合包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 引入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>
<!--lombok-->
<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>
第二步:配置代码生成类
注意:代码生成类与启动类同一目录级别
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/fluent_mybatis?useUnicode=true$characterEncoding=utf-8";
//数据库用户名
static final String username = "root";
//数据库密码
static final String password = "zhangpei@123";
@Test
public void generate() throws Exception {
//引用配置类,build方法允许有多个配置类
FileGenerator.build(Empty.class);
}
@Tables(
//设置数据库连接信息
url = url,username = username,password = password,
//设置entity类生成src目录,相对于user.dir
srcDir = "src/main/java",
//设置entity类的package值
basePack = "com.smile.fluent",
//设置dao接口和实现的src目录,相对于user.dir
daoDir = "src/main/java",
//设置哪些表要生成Entity文件
tables = {@Table(value = "hello_world")}
)
static class Empty{ //类名随便取,只是配置定义的一个载体
}
}
第三步:指定代码生成类,生成Entity类和其它层级代码
生成代码后,可能会有报错,继续往下操作即可
第四步:重新构建项目,或者使用maven进行compile
编译后的项目,注意target下回生成以下文件
第五步:创建配置文件,定义MapperFactory bean
注意:一定要使用@MapperScan指定mapper类所在包路径
import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan({"com.smile.fluent.mapper"}) //生成mapper所在包路径
public class ApplicationConfig {
@Bean
public MapperFactory mapperFactory(){
return new MapperFactory();
}
}
第六步:书写controller,进行使用
import com.smile.fluent.entity.HelloWorldEntity;
import com.smile.fluent.mapper.HelloWorldMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Autowired
private HelloWorldMapper helloWorldMapper;
@GetMapping("/insert")
public void insert(){
helloWorldMapper.insert(new HelloWorldEntity()
.setYourName("sysadmin")
.setSayHello("Hello"));
HelloWorldEntity entity = helloWorldMapper.findOne(helloWorldMapper.query()
.where.sayHello().eq("Hello").
and.yourName().eq("sysadmin")
.end());
System.out.println(entity);
}
}
具体方法使用查阅官方文档