Using mybatis generator to generate code in spring boot

Please create your springboot project first Add mybatis generator to the project 1. Add plugin in pom <plugin> <groupId>org.mybatis.gener...

Please create your springboot project first

Add mybatis generator to the project

1. Add plugin in pom

<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> </plugin>

2. Create the generatorConfig.xml file in the resource directory

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai" userId="root" password="123456"> <property name="nullCatalogMeansCurrent" value="true" /> </jdbcConnection> <javaModelGenerator targetPackage="com.example.demo.model" targetProject="C:\...\project\project-module\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- mappers Generated in targetProject(Namely resources)Directory --> <sqlMapGenerator targetPackage="mappers" targetProject="C:\...\project\project-module\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="C:\...\project\project-module\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="%"> </table> </context> </generatorConfiguration>

3. Create maven Run/Debug configuration

working directory is the project directory (the parent of src). command line is as follows:
-Dmybatis.generator.overwrite=true mybatis-generator:generate

After adding a custom query to the generated mapper and interface in development, it is recommended to set the overwrite parameter to false, otherwise the entity, mapper class and mapper xml file in the project will be rewritten during generation, resulting in the loss of properties, functions and custom queries

Reference mybatis in the project

1. Add mybatis dependency in pom

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>

2. Add mybatis-config.xml in the resource directory

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J" /> </settings> <typeAliases> <package name="com.example.demo.model" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value="" /> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/demo" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <package name="com.example.demo.mapper" /> </mappers> </configuration>

3. Add MapperScan to application main class

@SpringBootApplication @MapperScan({"com.example.demo.mapper"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

4. Add mapper dependency injection

@Autowired private DemoMapper demoMapper; @GetMapping("/demos") public List<Demo> GetList() { try { return demo.selectAll(); } catch (Exception ex) { return null; } }

Reference translation: how-to-build-project-by-spring-boot-mybatis-mybatis-generator

31 January 2020, 14:29 | Views: 10030

Add new comment

For adding a comment, please log in
or create account

0 comments