添加各大插件
// 执行分析插件
@Configuration
public class MybatisPlusConfig {
@Bean
public SqlExplainInterceptor sqlExplainInterceptor(){
SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
List<ISqlParser> sqlParserList = new ArrayList<>();
sqlParserList.add(new BlockAttackSqlParser());
sqlExplainInterceptor.setSqlParserList(sqlParserList);
return sqlExplainInterceptor;
}
}
备注:mybatisplus中添加执行分析插件可以避免全表删除,等不安全的sql操作
分页插件
@Configuration
/*@MapperScan("com.dm.krystal")*/ /* KrystalApiApplication在启动文件上写了,这里不加*/
public class MyBatisPlusConfig {
/**
* mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
性能分析插件
自动填充
实现mybatisplus中的方法
自动填充优化
乐观锁例如添加版本号
MP配置类
自动填充需要实现功能,需要加入到bean中(@Component)
执行 SQL 分析打印
yml配置
mybatisplus代码生成器
package com.gds;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
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;
/**
* @Author 龚道松
* @Date 2019/12/12 22:38
* @Version 1.0
**/
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
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();
gc.setOutputDir("G:\\IDEAProjects\\demospring-boot2\\spring\\src\\main\\java");
gc.setAuthor("gds")
.setActiveRecord(true); // 作者
gc.setOpen(false)
.setIdType(IdType.AUTO);
gc.setFileOverride(true)
.setBaseResultMap(true)
.setBaseColumnList(true);// 文件覆盖
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl("jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
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.gds")
.setMapper("mapper")
.setService("service")
.setEntity("entity")
.setController("controller");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setCapitalMode(true)
.setInclude("user");
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id");
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
TemplateConfig templateConfig = new TemplateConfig();
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
}
}
实体类:
package com.gds.sys.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
*
* </p>
*
* @author gds
* @since 2019-12-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User extends Model<User> {
private static final long serialVersionUID = 1L;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
/**
* version
* @return
*/
// 乐观锁添加版本号字段
@Version
private Integer version;
/**
* id
* @return
*/
private Long id;
/**
* 逻辑删除标识(0未删除,1删除)
* @return
*/
@TableLogic
@TableField(select=false) // 逻辑删除的时候不显示deleted
private Integer deleted;
/**
* 创建时间
* @return
*/
@TableField(fill = FieldFill.INSERT) // 插入数据是添加时间
private LocalDateTime createTime;
/**
* 修改时间
* @return
*/
@TableField(fill = FieldFill.UPDATE) // 更新时修改时间
private LocalDateTime updateTime;
@Override
protected Serializable pkVal() {
return null;
}
}
启动类:
package com.gds;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.gds.mapper")
@SpringBootApplication
public class DemospringBoot2Application {
public static void main(String[] args) {
SpringApplication.run(DemospringBoot2Application.class, args);
}
}