在springboot项目中集成mybatis-plus是很方便开发的,最近看了一下plus的文档,简单用一下它的代码生成器,首先在一个简单的springboot项目中加入如下依赖
<!-- 引入mybatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!-- 引入mybatis-plus-generator依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<!-- 引入freemarker依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 引入mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
<scope>runtime</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
我这边呢习惯把mapper文件放在java源码路径下,而不是放在默认的resources目录下,项目启动有导致mapper注入不了,所以还得在pom的<build></build>标签之中加入如下配置,并在启动类上加上mapper扫描路径
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.ftl</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
@SpringBootApplication
@MapperScan(basePackages = {"com.fengyun.mallmanage"})
public class MallManageSystemApplication {
public static void main(String[] args) {
SpringApplication.run(MallManageSystemApplication.class, args);
}
}
基本依赖和配置结束了,我参考了plus的官方文档,根据自己的需求稍微修改了一下它的生成器代码,官网地址
/**
* mybatis-plus代码生成器,生成实体,mapper,mapper.xml,service,serviceImpl,controller
* 演示例子,执行 main 方法控制台输入表名回车自动生成对应项目目录中(目录要需要自行修改)
*/
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();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("YuanXing");
//是否打开输出的目录,默认true
gc.setOpen(false);
//覆盖已有的文件,默认false(第一次生成时放开)
// gc.setFileOverride(true);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
// 设置日期类型为Date(若不设置时间类型都会变成LocalDateTime部分连接池例如druid是无法识别的)
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/malldata-dev?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("密码");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(scanner("模块名"));
pc.setParent("com.fengyun.mallmanage");
//自定义实体包名(不同的模块自己手动修改)
pc.setEntity("mapper.goods.entity");
//自定义mapper包名(不同的模块自己手动修改)
pc.setMapper("mapper.goods");
//自定义mapper.xml包名(不同的模块自己手动修改)
pc.setXml("mapper.goods");
//自定义service包名(不同的模块自己手动修改)
pc.setService("service.goods");
//自定义serviceImpl包名(不同的模块自己手动修改)
pc.setServiceImpl("service.goods.impl");
//自定义controller包名(不同的模块自己手动修改)
pc.setController("controller.goods");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String xmlPath = "/templates/mapper.xml.ftl";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(xmlPath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/java/com/fengyun/mallmanage/mapper/goods"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//是否为lombok模型,默认为false
strategy.setEntityLombokModel(true);
//前后端分离时可开启
// strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//RequestMapping驼峰转连字符
// strategy.setControllerMappingHyphenStyle(true);
//生成实体时生成生成数据库字段注解
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
然后运行即可
需要注意的是由于我在代码生成器代码中包配置的时候注释掉了输入模块名(即这一段pc.setModuleName(scanner("模块名"));),所以会导致controller中的RequestMapping的路径有两个//表名的驼峰命名,假设输入模块名的话RequestMapping的路径就会是/模块名/表名的驼峰命名,但是这样的话生成的类的包就不是我现在这样的了,这个看自己的需求吧,具体可以看一下生成包的源码