1. Use the maven project and add dependencies
<!-- mybatis-plus begin --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.2.0</version> </dependency>
There is also a database connection
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>compile</scope> </dependency>
Finally, the source code
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.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import org.junit.jupiter.api.Test; /** * mybatis-plus Automatically generate code* * @author Terry * @version 1.0 * @date 2018-05-16 09:35 */ public class SimpleMp { @Test public void generateCode() { //Specify the package name String packageName = "com.hciot.hhhh"; //user -> UserService, set to true: user -> IUserService boolean serviceNameStartWithI = false; //Specify the generated table name String[] tableNames = new String[]{"data_air_sensor_co", "order_product", "relation_device_gateway"}; generateByTables(serviceNameStartWithI, packageName, tableName); } /** * Automatically generate based on the table * * @param serviceNameStartWithI Default is false * @param packageName PackageName PackageNames TableName Name* @author Terry */ private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) { //Configure dataSourceConfig dataSourceConfig = getDataSourceConfig(); // Policy configuration StrategyConfig strategyConfig = getStrategyConfig(tableNames); // Global variable configuration GlobalConfig globalConfig = getGlobalConfig(serviceNameStartWithI); // Package name configuration PackageConfig packageConfig = getPackageConfig(packageName); //Automatically generate atuoGenerator(dataSourceConfig, strategyConfig, globalConfig, packageConfig); } /** * Integration* * @param dataSourceConfig Configuration data source* @param strategyConfig Policy configuration* @param config Global variable configuration* @param packageConfig Package name configuration* @author Terry */ private void atuoGenerator(DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig, GlobalConfig config, PackageConfig packageConfig) { new AutoGenerator() .setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo(packageConfig) .execute(); } /** * Set package name* * @param packageName Parent path package name* @return PackageConfig Package name configuration* @author Terry */ private PackageConfig getPackageConfig(String packageName) { return new PackageConfig() .setParent(packageName) .setXml("mapper") .setMapper("dao") .setController("controller") .setEntity("entity"); } /** * Global Configuration* * @param serviceNameStartWithI false * @return GlobalConfig * @author Terry */ private GlobalConfig getGlobalConfig(boolean serviceNameStartWithI) { GlobalConfig globalConfig = new GlobalConfig(); globalConfig .setBaseColumnList(true) .setBaseResultMap(true) .setActiveRecord(false) .setAuthor("Terry") //Set the output path.setOutputDir(getOutputDir("mybatis-plus")) .setFileOverride(true); if (!serviceNameStartWithI) { //Set the service name globalConfig.setServiceName("%sService"); } return globalConfig; } /** * Return project path* * @param projectName Project name* @return Project path* @author Terry */ private String getOutputDir(String projectName) { String path = this.getClass().getClassLoader().getResource("").getPath(); int index = path.indexOf(projectName); return path.substring(1, index) + projectName + "/src/main/java/"; } /** * Policy Configuration* * @param tableNames TableNames Table name* @return StrategyConfig * @author Terry */ private StrategyConfig getStrategyConfig(String... tableNames) { return new StrategyConfig() // Global capitalization naming ORACLE Note.setCapitalMode(true) .setEntityLombokModel(false) // Table name, field name, whether to use underscore naming (default false) .setDbColumnUnderline(true) // Naming strategy from database table to file.setNaming(NamingStrategy.underline_to_camel) // Table name that needs to be generated, multiple table names are passed through arrays.setInclude(tableNames); } /** * Configure data source* * @return DataSourceConfig * @author Terry */ private DataSourceConfig getDataSourceConfig() { String dbUrl = "jdbc:mysql://localhost:3306/test"; return new DataSourceConfig().setDbType(DbType.MYSQL) .setUrl(dbUrl) .setUsername("root") .setPassword("root") .setDriverName("com.mysql.jdbc.Driver"); } /** * Automatically generate based on the table* * @param packageName PackageName PackageName* @param tableNames TableNames TableNames */ @SuppressWarnings("unused") private void generateByTables(String packageName, String... tableNames) { generateByTables(true, packageName, tableNames); } }Summarize
The above is the implementation code based on the Mybatis plus automatic code generator introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!