Mybatis-Plus是一个Mybatis的增强工具,在Mybatis的基础上,提供了一系列的实用功能,如物理分页、自动代码生成、性能分析、SQL注入器等等。Mybatis-Plus的目标是简化开发、提升效率、减少出错。
Mybatis-Plus的优点:
1.简化开发:Mybatis-Plus自动完成了增删改查的常规操作,大大缩短了开发周期。
2.提升效率:Mybatis-Plus提供了代码生成器,通过代码生成器可以快速生成Mapper、Service、Controller等常用文件,减少了大量重复工作。
3.减少出错:Mybatis-Plus提供了强大的条件构造器,避免了手写复杂的SQL语句,减少了出错的可能性。
4.性能优化:Mybatis-Plus提供了一系列的性能优化功能,如乐观锁、分页插件等。
Mybatis-Plus的使用:
1.引入依赖:
<!-- 引入Mybatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
2.配置Mybatis-Plus:
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
// 分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
3.使用Mybatis-Plus:
3.1.实体类
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
3.2.Mapper
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
}
3.3.Service
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
3.4.Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userService.getById(id);
}
@GetMapping("/list")
public List<User> listUser() {
return userService.list();
}
@PostMapping("")
public Boolean addUser(@RequestBody User user) {
return userService.save(user);
}
@PutMapping("")
public Boolean updateUser(@RequestBody User user) {
return userService.updateById(user);
}
@DeleteMapping("/{id}")
public Boolean deleteUser(@PathVariable Long id) {
return userService.removeById(id);
}
}
4.使用Mybatis-Plus代码生成器
Mybatis-Plus提供了一个代码生成器,可以根据数据库表生成Mapper、Service、Controller等常用文件,大大简化了开发工作。
4.1.引入依赖
<!-- 引入Mybatis-Plus代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.x.x</version>
</dependency>
4.2.配置文件
# 数据源配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
# Mybatis-Plus配置
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted
id-type: INPUT
# 代码生成器配置
generator:
packageName: com.example
tableNames:
- user
author: yonyou
outputDir: ./src/main/java
4.3.代码生成器
public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
gc.setAuthor("yonyou");
gc.setOpen(false);
gc.setFileOverride(true);
gc.setServiceName("%sService");
gc.setMapperName("%sMapper");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.example");
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("mapper");
pc.setEntity("entity");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setCapitalMode(true);
strategy.setEntityLombokModel(true);
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setRestful(true);
strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setLogicDeleteFieldName("deleted");
strategy.setControllerMappingHyphenStyle(true);
strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
mpg.setStrategy(strategy);
// 执行生成代码
mpg.execute();
}
}
5.总结
Mybatis-Plus是一个非常优秀的Mybatis增强工具,提供了一系列实用的功能,可以大大简化开发工作,提高开发效率。同时,Mybatis-Plus也提供了非常完善的文档,可以在使用过程中快速解决问题。