关注微信公众号【码小白吖】,获取回复“mybatis-plus”获取项目资源
创建项目
用IDEA快速创建空的SpringBoot项目
引入依赖
<!--数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--逆向生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!--模板-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在src/main/resources 目录下创建 templates 目录及entity.java 、mapper.xml 文件用来作为文件模板
创建java.class
在项目的src/main/java 文件夹下任意创建一个java类 代码如下:
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入要生成的表名:");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig()
.setOutputDir(System.getProperty("user.dir") + "/src/main/java")
.setAuthor("@码小白")
.setOpen(false)
.setFileOverride(true)
.setIdType(IdType.NONE)
.setBaseResultMap(true)
.setBaseColumnList(true)
.setServiceName("OldService");
;
DataSourceConfig dsc = new DataSourceConfig()
.setUrl("你自己的数据库地址")
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUsername("username")
.setPassword("password")
.setDbType(DbType.MYSQL);
PackageConfig pc = new PackageConfig()
.setParent("org.baomidou")
.setEntity("PO")
.setService("service")
.setMapper("mapper")
.setModuleName("maxiaobai");
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
List<FileOutConfig> focList = new ArrayList<>();
String templatePath = "/templates/mapper.xml";
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(com.baomidou.mybatisplus.generator.config.po.TableInfo tableInfo) {
return System.getProperty("user.dir") + "/src/main/resources/mapper/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
StrategyConfig strategyConfig = new StrategyConfig()
.setCapitalMode(true)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setEntityLombokModel(true)
.setRestControllerStyle(true)
.setSuperEntityColumns("id")
.setInclude(scanner("").split(","))
.setControllerMappingHyphenStyle(true)
.setTablePrefix(pc.getModuleName() + "_");
mpg.setGlobalConfig(gc);
mpg.setDataSource(dsc);
mpg.setPackageInfo(pc);
mpg.setCfg(cfg);
mpg.setTemplate(templateConfig);
mpg.setStrategy(strategyConfig);
mpg.execute();
}
执行结果如下:
关注微信公众号【码小白吖】,获取回复“mybatis-plus”获取项目资源
|