文章目录
1.整合MyBatis-Plus
1.1.添加POM引用
<!--导入MyBatis的场景启动器-->
<!--<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>-->
<!--MyBatis Plus启动器-->
<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>
1.2.更新配置
# mybatis配置
#mybatis:
# 指定Mapper.xml位置
#mapper-locations: classpath:mapper/*.xml
# 给实体类取别名
#type-aliases-package: com.easyshop.pojo
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/*.xml
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 1
# 删除后
logic-delete-value: 0
type-aliases-package: com.easyshop.pojo
1.3.实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_content_category")
public class ContentCategory {
@TableId(value = "id",type= IdType.AUTO)
private Long id;
private String name;
private Long del;
}
1.4.mapper
public interface ContentCategoryMapper extends BaseMapper<ContentCategory> {
ContentCategory findById(Long id);
}
1.5.Service
public interface ContentCategoryService extends IService<ContentCategory> {
ContentCategory findById(Long id);
}
@Service
public class ContentCategoryServiceImpl extends ServiceImpl<ContentCategoryMapper, ContentCategory> implements ContentCategoryService {
@Autowired
ContentCategoryMapper contentCategoryMapper;
public ContentCategory findById(Long id) {
return contentCategoryMapper.findById(id);
}
}
1.6.分页插件
/**
* 配置分页插件
*
*/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
@Test
public void test4(){
QueryWrapper<Employee> qw = new QueryWrapper<Employee>();
qw.between("age",10,50);
IPage<Employee> pager = em.selectPage(new Page<Employee>(1, 2), qw);
List<Employee> emps = pager.getRecords();
for (Employee emp : emps) {
System.out.println(emp);
}
long pages = pager.getPages();
System.out.println("总页数是:"+pages);
long total = pager.getTotal();
System.out.println("总条数是:"+total);
}
2.代码生成工具
package com.easyshop.test;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Scanner;
/**
* 自动生成mybatisplus的相关代码
*/
public class GeneratorCodeConfig {
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir"); // D:\project2021\EasyShop\
System.out.println(projectPath);
gc.setOutputDir(projectPath + "/advertisement-service/src/main/java");
gc.setAuthor("bruce");
gc.setOpen(false);
//实体属性 Swagger2 注解
gc.setSwagger2(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//dsc.setUrl("jdbc:mysql://127.0.0.1:3306/easyshop_advertisement?useUnicode=true&characterEncoding=utf-8");
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/easyshop_advertisement?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(scanner("模块名"));
pc.setParent("com.easyshop");
pc.setEntity("pojo");
pc.setMapper("mapper");
pc.setService("service");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}