1.在项目的Test文件中创建生成器的类,运行main方法:
public class CodeGenerator { public static void main(String[] args) { String url = "jdbc:mysql:///xdb"; String username = "root"; String password = "123456"; String moduleName = "sys";//生成的业务逻辑包名 String mapperLocation = "C:\\Users\\dacai\\Desktop\\神盾局特工管理系统\\源码\\x-admin\\src\\main\\resources\\mapper\\" + moduleName;//设置映射文件的位置 String tables = "x_user,x_role,x_menu,x_user_role,x_role_menu";//根据实际数据业务设置对应的表 FastAutoGenerator.create(url, username, password) .globalConfig(builder -> { builder.author("laocai") // 设置作者 //.enableSwagger() // 开启 swagger 模式 //.fileOverride() // 覆盖已生成文件 .outputDir("C:\\Users\\dacai\\Desktop\\神盾局特工管理系统\\源码\\x-admin\\src\\main\\java"); // 指定业务包输出目录 }) .packageConfig(builder -> { builder.parent("com.lantu") // 设置父包名 .moduleName(moduleName) // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.xml, mapperLocation)); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.addInclude(tables) // 设置需要生成的表名 .addTablePrefix("x_"); // 设置过滤表前缀,如果不设置生成的实体类就会有x_}) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); }
2.生成对应的业务逻辑类,以及映射文件(提前在resource目录下面创建好对应的映射文件)。