1、导入依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency>
2、代码生成器模板
@Test public void test01(){ //获取当前项目地址 String project_path = System.getProperty("user.dir"); FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/test? characterEncoding=utf-8&userSSL=false", "username", "pwd") .globalConfig(builder -> { builder.author("xiaobing") // 设置作者 //.enableSwagger() // 开启 swagger 模式 .fileOverride() // 覆盖已生成文件 .outputDir(project_path+"/src/main/java"); // 指定输出目录 }) .packageConfig(builder -> { builder.parent("com.xiaobing") // 设置父包名 .moduleName("mybatisx") // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, project_path+"/src/main/resources/mapper")); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder .addInclude("t_user") // 设置需要生成的表名,需要提前到数据库建表且IDEA连接上库表 .addTablePrefix("t_", "c_"); // 设置过滤表前缀 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker 引擎模板,默认的是Velocity引擎模板 .execute(); }
3、代码执行之前 => 生成代码 => 生成之后
4、简单完善下controller进行测试
@RestController @RequestMapping("/mybatisx/user") public class UserController { @Autowired IUserService iUserService; @GetMapping("by/{id}") public User getUserById(@PathVariable("id") Integer id){ return iUserService.getById(id); } }
5、启动项目访问接口
_ _ |_ _ _|_. ___ _ | _ | | |\/|_)(_| | |_\ |_)||_|_\ / | 3.5.1 2022-09-22 15:39:17.246 INFO 16992 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-09-22 15:39:17.253 INFO 16992 --- [ main] c.x.mybatisx.MybatisxDemoApplication : Started MybatisxDemoApplication in 1.625 seconds (JVM running for 2.08)标签:代码生成,MybatisPlus,builder,设置,path,main,id From: https://www.cnblogs.com/xiaozhaoboke/p/16719599.html