This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

MP provides a large number of custom Settings, and the generated code is perfectly capable of meeting all types of requirements

  1. MP code generator and Mybatis MBG code generator:

MP code generators are based on Java code. MBG does code generation based on XML files

MyBatis code generator can generate: entity class, Mapper interface, Mapper mapping file

MP code generator can generate: entity class (can choose whether to support AR), Mapper interface, Mapper mapping file, Service layer, Controller layer.

In MP, we suggest that the name of the table and the name of the table field should be named in camel’s name. If the name of the table and the name of the table field are named in the way of the underline, please turn on the global underscore switch. If the name of the table field is named in different ways, please note to specify. The reason for doing this is to avoid the performance cost of mapping to the entity class so that fields can be mapped directly to the entity class. Of course, if you don’t have to consider this performance cost in your project, then you can use sliding-line without any problem. You can simply configure the dbColumnUnderline property when you generate the code

Add the dependent

Code generator dependencies

< the dependency > < groupId > org. Apache. Velocity < / groupId > < artifactId > velocity - engine - core < / artifactId > < version > 2.0 < / version >  </dependency>Copy the code

Log output dependency

<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> Slf4j </groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency>Copy the code

The test code

After the dependency is added, we need to write the test code (does anyone really think we don’t have to write any code?).

Public class test {/* * code generated * */ @test public void demo1(){//1. GlobalConfig config = new GlobalConfig(); Config.setactiverecord (true) // Whether ar.setauthor (" LL ") // author name .setOutputDir("D:\\Java\\MyBatisPlus\\mp01\\ SRC \\main\ Java ") // Generate path.setFileOverride (true) // File override .setidType (idtype.auto) // Primary key policy.setServicename ("%sService") // Whether the generated service interface name starts with I. SetBaseResultMap (true) // Result set mapping .setBaseColumnList(true); DataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL) .setDriverName("com.mysql.jdbc.Driver") .setUrl("jdbc:mysql://localhost:3306/mp") .setUsername("root") .setPassword("666666"); //3. StrategyConfig StrategyConfig = new StrategyConfig(); StrategyConfig. SetCapitalMode (true). / / global capital named setDbColumnUnderline (true) / / table name, field name, SetNaming (namingStrategy.underline_to_camel)// Naming policy for database table mapping to experiment.settablePrefix (" tBL_ ")// table prefix .setInclude("tbl_employee"); PackageConfig PackageConfig =new PackageConfig(); packageConfig.setParent("com.ll.mp") .setMapper("mapper") .setService("service") .setController("controller") .setEntity("beans") .setXml("mapper"); AutoGenerator =new AutoGenerator(); autoGenerator.setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo(packageConfig); / / execution autoGenerator. The execute (); }}Copy the code

This is the directory structure we generated