只需要创建好表的结构,代码生成器通过简单的配置就可以生成Controller层,service层,Mapper层,entity层,这也泰裤辣粑。
pom.xml
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis插件代码生成器-->
<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.0</version>
</dependency>
这里都是各种配置,引入相关类的时候需要注意不要引错哦!
import com.baomidou.mybatisplus.annotation.DbType;
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;
/**
* @Author Ronghao.Guo
* @Date 2023/6/17 11:31
*/
public class CodeGet {
public static void main(String[] args) {
//1.创建代码生成器
AutoGenerator mpg = new AutoGenerator();
//2.全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("D:\\javaee\\ssyx-parent\\ssyx-parent\\service\\service-sys\\src\\main\\java"); //生成代码存放位置
gc.setServiceName("%sService");
gc.setAuthor("Ronghao.guo");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
//3.配置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/shequ-sys?characterEncoding=utf-8&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//4.包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.guo.ssyx");
pc.setMapper("sys");
pc.setController("controller");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
//5.策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("region", "ware", "region_ware"); //表名
strategyConfig.setNaming(NamingStrategy.underline_to_camel); // 数据库表映射到实体
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); //数据库表字段映射到属性
strategyConfig.setEntityLombokModel(true); //
strategyConfig.setRestControllerStyle(true); //restful api风格控制器
strategyConfig.setControllerMappingHyphenStyle(true); //url 中驼峰转连字符
mpg.setStrategy(strategyConfig);
//6.执行
mpg.execute();
}
}
效果展示
有了代码生成器两个小时的工作量2分钟完成了,真好用,家人们都肯用