Table of Contents
Preface
1. Preparation work
2. Briefly introduce the official documents (you can skip to the code if you don’t want to read it)
3. Create a project and import dependencies with maven
4. Code Generator
5. Summary
Foreword
I would like to introduce to you an artifact that is easy to develop. It is a code generator provided by mybatis-plus. As the name suggests, it is easy to develop and quickly complete repeated basic operations to save everyone’s time
1. Preparation work
1. Prepare database tables and data
2. Prepare maven dependencies
3. Are you ready? Let’s start! ! !
2. Briefly introduce the official documents (if you don’t want to read it, you can skip to the code)
Official documentation (old version) Official documentation (new version)
We can see that the official is very considerate and provides renderings and examples.
According to the official documentation, we can know that MyBatis-Plus has removed the default dependencies of the code generator and template engine since 3.0.3
. We need to manually add relevant dependencies and the default engine template Velocity.
3. Create a project and import dependencies with maven
It is recommended that you create a springboot project. I used an empty maven project for testing here, which is more troublesome.
The code generator has two dependencies plus your database dependency and your springboot dependency
<!-- mybatis-plus generator --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- velocity template engine --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.5</version> <scope>compile</scope> </dependency>
Add an episode
If you create an empty maven project, you must add this springboot dependency, otherwise an error will be reported.
4. Code Generator
Modify it according to your own configuration
Note: The mapper layer generated here does not have the @mapper annotation. If you use mybatis, you need to add this annotation manually. Because mybatis-plus automatically scans the mapper layer, this annotation is canceled.
import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; public class Generator { public static void main(String[] args) { // Code generator AutoGenerator mpg = new AutoGenerator(); //Global configuration GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); // Get the current user's working directory gc.setOutputDir(projectPath + "/src/main/java"); // Set the output directory for generated code gc.setAuthor("xiaoyu"); // Set author information gc.setOpen(false); // Whether to open the generated folder gc.setSwagger2(true); // Whether to enable Swagger2 annotations for entity attributes gc.setServiceName("%sService"); // Name format of Service interface mpg.setGlobalConfig(gc); //Data source configuration DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/curd?useUnicode=true & amp;useSSL=false & amp;characterEncoding=utf8"); // Database connection URL dsc.setDriverName("com.mysql.jdbc.Driver"); // Database driver class dsc.setUsername("root"); // Database username dsc.setPassword("xiaoyu"); // Database password mpg.setDataSource(dsc); // Set data source configuration // package configuration PackageConfig pc = new PackageConfig(); pc.setModuleName("curd"); //Module name pc.setParent("com.test"); // Parent package name pc.setController("controller"); // Controller package name pc.setEntity("entity"); // Entity class package name pc.setService("service"); // Service package name pc.setMapper("mapper"); // Mapper package name pc.setServiceImpl("serviceImpl"); // Service implementation class package name mpg.setPackageInfo(pc); // Set package configuration //Strategy configuration StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); // Naming strategy from database table name to entity class name strategy.setColumnNaming(NamingStrategy.underline_to_camel); // Naming strategy from database field name to entity class attribute name strategy.setEntityLombokModel(true); // Use Lombok to generate entity classes strategy.setRestControllerStyle(true); // Generate RestController controller strategy.setSuperEntityColumns("id"); // Super class fields strategy.setInclude(("user").split(",")); // The tables that need to be generated, separate multiple tables with, strategy.setControllerMappingHyphenStyle(true); // Use hyphen style in controller mapping path strategy.setTablePrefix(pc.getModuleName() + "_"); // Table name prefix mpg.setStrategy(strategy); // Set strategy configuration //Execute code generation mpg.execute(); } }
5. Summary
The above is the simple configuration of the code generator. Its functions are not only these, it can also help you complete the operations of adding, deleting, modifying and checking with one click. I have not written it here. You can check the documentation by yourself.