mybatis-plus简介
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。官网:https://mp.baomidou.com/
使用
- 引入pom包文件
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 这是mybatis-plus的代码自动生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 这是模板引擎依赖 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
<scope>runtime</scope>
</dependency>
这个是lombok,实体类的属性注解
mybatis-plus的依赖包,注意mybatis-plus的依赖包版本要统一,不然jar引用会冲突,另外freemarker一定要引,根据模板自动生成代码;
2. 编写generator自动生成代码
public static void main(String[] args) {
String author = "hmx";
String outPutDir = "E:/springboot-mybatis-plus/src/main";
String javaStr = "/java";
String resourceStr = "/resources";
AutoGenerator generator = new AutoGenerator();
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
generator.setTemplateEngine(new FreemarkerTemplateEngine());
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setAuthor(author);
globalConfig.setDateType(DateType.ONLY_DATE);
globalConfig.setOutputDir(outPutDir+javaStr);
globalConfig.setFileOverride(false);//是否覆盖同名文件
globalConfig.setActiveRecord(true);//之继承model就可实现增删改查
globalConfig.setEnableCache(false);//是否开启二级缓存
globalConfig.setBaseResultMap(true);
globalConfig.setBaseColumnList(false);
///设置全局参数
generator.setGlobalConfig(globalConfig);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setTypeConvert(new MySqlTypeConvert(){
/**
* 自定义数据类型转换(可选)
* @param globalConfig
* @param fieldType
* @return
*/
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
return super.processTypeConvert(globalConfig, fieldType);
}
});
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8");
dsc.setUsername("root");
dsc.setPassword("123");
generator.setDataSource(dsc);
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.wonders.springbootmybatisplus");
generator.setPackageInfo(packageConfig);
//自定义配置
InjectionConfig icf = new InjectionConfig() {
@Override
public void initMap() {
}
};
List<FileOutConfig> foCfgList = new ArrayList<>();
foCfgList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
return outPutDir+resourceStr+"/mapper/"
+"/"+tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
}
});
icf.setFileOutConfigList(foCfgList);
generator.setCfg(icf);
generator.setTemplate(new TemplateConfig().setXml(""));
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
strategyConfig.setSuperEntityColumns("id");
strategyConfig.setControllerMappingHyphenStyle(true);
strategyConfig.setTablePrefix(packageConfig.getModuleName() + "_");
// strategyConfig.setInclude(new String[]{"users"});
generator.setStrategy(strategyConfig);
generator.execute();
}
- 配置yml配置文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123
mybatis-plus:
mapper-locations: classpath:/mapper/**/*Mapper.xml
configuration:
# 输出sql语句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 8002
- mybatis-plus的分页功能(两种,只选择最简单的配置实现方式)
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/**
* 分页配置
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
调用
@RestController
@RequestMapping("/users")
public class UsersController {
@Autowired
IUsersService usersService;
@GetMapping("list")
public Object getUsers( ){
IPage<Users> page = new Page<>(1, 2);
QueryWrapper<Users> wrapper = new QueryWrapper<>();
Users user = new Users();
user.setName("虎");
wrapper.setEntity(user);
return usersService.page(page,wrapper);
}
}