java数据库代码生成工具代码
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; public class CreateCodeGenerator { public static void main(String[] args) { /* 以下是需要的maven依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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.3</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> */ //获取代码生成器的对象 AutoGenerator autoGenerator = new AutoGenerator(); //设置数据库相关配置 DataSourceConfig dataSource = new DataSourceConfig(); dataSource.setDriverName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/xxxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai"); dataSource.setUsername("root"); dataSource.setPassword("xxxxx"); autoGenerator.setDataSource(dataSource); //设置全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java"); //设置代码生成位置 globalConfig.setOpen(false); //设置生成完毕后是否打开生成代码所在的目录 globalConfig.setAuthor("xxxxx"); //设置作者 globalConfig.setFileOverride(true); //设置是否覆盖原始生成的文件 globalConfig.setMapperName("%sMapper"); //设置数据层接口名,%s为占位符,指代模块名称 globalConfig.setServiceName("%sService"); //格式化service接口文件名称,%s进行匹配表名,如**Service globalConfig.setServiceImplName("%sServiceImpl"); //格式化service实现类文件名称,%s进行匹配表名,如 **ServiceImpl globalConfig.setIdType(IdType.ASSIGN_ID); //设置Id生成策略 globalConfig.setSwagger2(true); autoGenerator.setGlobalConfig(globalConfig); //设置包名相关配置 PackageConfig packageInfo = new PackageConfig(); packageInfo.setParent("com.xxx.xxxx"); //设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径 packageInfo.setEntity("entity"); //设置实体类包名 packageInfo.setMapper("mapper"); //设置数据层包名 autoGenerator.setPackageInfo(packageInfo); //策略设置 StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setEntityLombokModel(true); //设置是否启用lombok strategyConfig.setNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略:下划线转驼峰命 strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略:下划线转驼峰命 autoGenerator.setStrategy(strategyConfig); //执行生成操作 autoGenerator.execute(); } }
------
网上找了很久都没有比较合适的生成工具,有些工具下载下来还报病毒,挺郁闷的,干脆麻烦点自己按文档搞一个