This article explains the implementation of mybatis generator on spring boot
Because in some small enterprises, mybatis is more and more popular than hibernate, so I think it is necessary to write a more simple and understandable article to teach you how to integrate mybatis on spring boot and use its reverse engineering
Prerequisite preparation:
Tool used: jdk1 8、IntelliJ IDEA、Mysql
First, let’s take a look at the basic directory structure:
Table structure created by database:
Maven dependent packages and plug-ins(only mybatis related packages are posted):
Maven plug-in dependency:
application. Properties file configuration:
generatorConfig.xml(reverse engineering configuration file. If you use Maven plug-in to start, please keep the file name consistent)
<?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="DB2Tables" targetRuntime="MyBatis3">
<!-- Remove comments -- >
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- Database connection required -- >
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/SSM?useUnicode=true&characterEncoding=utf-8&useSSL=false"
userId= <!-- Fill in your own -- >
password= <!-- Ditto -- >
>
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Specify the location of JavaBean generation. Location of JavaBean generation -- >
<javaModelGenerator targetPackage="cn.lxt.bean" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- Location of SQL mapping file generation -- >
<sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Specify the location of Dao interface generation -- >
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.lxt.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- Table specifies the generation strategy of each table -- >
<table tableName="user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
The above code and configuration preparation have been completed
Let’s teach you how to use idea to start Maven’s plug-in
In the configuration interface, we can read the following error messages and jump out of the configuration interface
After the plug-in is set up
After running, if successful, the POJO class, Dao interface and mapper of the response will be generated at the specified location (which can be configured in generatorconfig.xml, see the comments for details) XML database file
Of course, if you cannot use Maven plug-in due to many problems (daily) on your computer, you can create a test class in Src / main / Java folder and write the following code:
Note that write your own reverse configuration file path in new file (“”)!
public class GeneratorTest {
public static void main(String[] args)throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("/Users/apple/Documents/Spring-Boot/src/main/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
Then running this class alone can also automatically generate code!
The above is the reverse engineering implementation of mybatis based on spring boot
Thank you for your attention ~ remember to like it before you leave