之前项目一直使用ssm较多,对springboot仅局限于知道,但是并未使用过.正好公司因为新项目需要用到springboot,mybatis_plus,所以学习了一下,使用了mp的代码生成;
LZ使用的开发环境及版本如下: JDK1.8 Eclipse4.7 springboot 2.1.4
整个目录结构如下图
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hxb</groupId> <artifactId>mptest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mptest</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath /> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 启动 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- MYSQL --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <!-- LOMBOK --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- MP --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.2.0</version> </dependency> <!-- MP + SPRINGBOOT --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- MP 代码生成器 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <!-- MP 代码生成默认模板(必须) --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <!-- 自定义模板 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建config文件夹,新建数据源配置文件和mp配置文件,代码如下
package com.hxb.config; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource; /** * @description:数据源配置文件 * @author: yshexiaobai * @Date: 2019年9月5日 上午11:20:40 */ @Configuration public class DataSourceConfig { @Bean(name="dataSource") public DataSource dataSource() { return new DruidDataSource(); } }
package com.hxb.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; /** * * @description: mybatisplus配置文件 * @author: yshexiaobai * @Date: 2019年9月5日 上午11:27:12 */ @Configuration public class MybatisPlusConfig { /** * 配置分页插件 * @return */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor page = new PaginationInterceptor(); page.setDialectType("mysql"); return page; } }
创建generator文件夹,并在文件夹新建类MPGenerator
package com.hxb.generator; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /** * * @description:MP代码生成器 * @author: yshexiaobai * @Date: 2019年9月5日 上午11:40:07 */ public class MPGenerator { public static void main(String[] args) { //创建代码生成器 AutoGenerator mpg = new AutoGenerator(); //指定模板引擎 默认velocity //mpg.setTemplateEngine(new FreemarkerTemplateEngine()); //全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOpen(false); gc.setOutputDir("D:\\personalspace\\mptest\\src\\main\\java"); gc.setFileOverride(true); //是否覆盖已有文件 gc.setBaseResultMap(true); //XML是否需要BaseResultMap gc.setBaseColumnList(true); //XML是否显示字段 gc.setControllerName("%sController"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setMapperName("%sMapper"); gc.setXmlName("%sMapper"); gc.setAuthor("yshexiaobai"); gc.setIdType(IdType.AUTO); mpg.setGlobalConfig(gc); //数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/dinnerbook?characterEncoding=utf8&useSSL=false"); dsc.setUsername("root"); dsc.setPassword("123456"); mpg.setDataSource(dsc); //策略配置 StrategyConfig sc = new StrategyConfig(); sc.setTablePrefix("tab_"); //表名前缀 sc.setNaming(NamingStrategy.underline_to_camel); //表名生成策略 sc.setEntityBuilderModel(true); sc.setEntityLombokModel(true); mpg.setStrategy(sc); //包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.hxb"); pc.setEntity("entity"); pc.setController("controller"); pc.setService("service"); pc.setServiceImpl("serviceImpl"); pc.setMapper("mapper"); pc.setXml("mapper"); mpg.setPackageInfo(pc); mpg.execute(); } }
运行MPGenerator,则开始自动生成文件.