In the process of using mybatis, as more and more handwritten JavaBean s and XML are written, more and more errors are agreed. We certainly don't want to do so much repetitive work.
The knowledge points about MyBatis are summarized and a mind map is shared with you
Fortunately, mybatis provides us with powerful code generation -- MybatisGenerator.
Through simple configuration, we can generate various types of entity classes, Mapper interfaces, mapperlml files, Example objects, etc. Through these generated files, we can easily add, delete, modify and query a single table.
Tips: WeChat official account: programmer Bai Nannan, get daily push.
The following tools use IDEA
1.1 create Maven project
1.1.1 select new item from the menu
File | New | Project
1.1.2 select Maven on the left
Add description
Since we are just creating an ordinary project, click Next here.
1.1.3 enter GroupId and ArtifactId
- In my project,
GroupId: com homejim. Fill in mybatis generator with mybatis artifactid
Click Next.
1.1.4 Finish
Through the above steps, an ordinary Maven project is created.
1.2 configure the generator xml
In fact, the name doesn't matter, just follow the POM below XML file.
<?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> <classPathEntry location="C:\Users\\Administrator\\.m2\repository\\mysql\\mysql-connector-java\\8.0.12\\mysql-connector-java-8.0.12.jar"/> <context id="context" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="false"/> <property name="suppressDate" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="jim777"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.homejim.mybatis.entity" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis/mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.homejim.mybatis.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <table tableName="blog" /> </context> </generatorConfiguration>
Some contents need to be changed:
1. The full path of the local database driver jar package (must be changed).
2. Relevant configuration of database (must be changed)
3. Configuration of related tables (must be changed)
4. Location of entity class generation and storage.
5. The location where mapperlxml generated files are stored.
6. Mapper interface storage location.
If you don't know how to change it, please see the configuration details later.
1.3 configuring POM xml
Add some content to the original.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.homejim.mybatis</groupId> <artifactId>mybatis-generator</artifactId> <version>1.0-SNAPSHOT</version> <build> <finalName>mybatis-generator</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <configurationFile>src/main/resources/generator.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
Note that the file in the configurationFile refers to the generator xml. Therefore, the path is the relative path of the file, and the name is the same as that of the file.
At this point, mybatis generator can be used.
1.4 use and test
1.4.1 open Maven Projects view
On the IDEA, open:
View | Tools | Windwos | Maven Projects
Add description
1.4.2 double click mybatis generator in Maven projects
On the right, you can see Maven Projects. Locate the mybatis generator plug-in.
mybatis-generator | Plugins | mybatis-generator | mybatis-generator
Add description
1.4.3 double click to run
After running correctly, generate the code and get the following structure
Add description
It's not cool enough just to use it as simple as above. Then we can change the generator XML configuration file.
2.1 documentation
It is recommended to check the official documents.
Good English: http://www.mybatis.org/generator/configreference/xmlconfig.html
Chinese Translation: http://mbg.cndocs.ml/index.html
2.2 not available on the official website
2.2.1 property tag
This tag is only used to specify the attributes of elements on the official website. There is no detailed explanation on how to use it.
2.2.1.1 separator related
<property name="autoDelimitKeywords" value="true"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/>
The above configuration corresponds to mysql. When the fields in the database are the same as the keywords in the database, the separator will be used.
For example, our data column is delete. After the above configuration, it will become 'Delete' where it appears.
2.2.1.2 coding
The default is to use the encoding of the current system environment, which can be configured as GBK or UTF-8.
<property name="javaFileEncoding" value="UTF-8"/>
I think the project is UTF-8. If you specify to generate GBK, the automatically generated Chinese is garbled.
2.2.1.3 formatting
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
These are obviously customizable.
2.2.2 plugins label
The plugins tag is used to extend or modify the code generated by the code generator.
There is no such tag in the generated XML. This tag is used to configure the cache.
If we want to generate this tag, we can configure it in plugins.
<plugin type="org.mybatis.generator.plugins.CachePlugin" > <property name="cache_eviction" value="LRU"/> </plugin>
Add description
For example, you want to implement the Serializable interface in the generated JavaBean.
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
Add description
You can also customize plug-ins.
These plug-ins are very useful. I think we can open a special article to explain them in the future.
Look at the name, you know it is used to generate comments.
Default configuration:
<commentGenerator > <property name="suppressAllComments" value="false"/> <property name="suppressDate" value="false"/> <property name="addRemarkComments" value="false"/> </commentGenerator>
suppressAllComments: prevents comments from being generated. The default value is false.
suppressDate: prevent generated comments from containing time stamps. The default is false.
addRemarkComments: add the comments of the database in the comments. The default is false.
Another is that we can specify our custom annotation implementation class through the type attribute to generate the annotations we want. The custom implementation class needs to implement org mybatis. generator. api. CommentGenerator.
summary
The editor summarized the 2020 interview question. The modules included in this interview question are divided into 19 modules: Java foundation, container, multithreading, reflection, object copy, Java Web, exception, network, design pattern, Spring/Spring MVC, Spring Boot/Spring Cloud, Hibernate, MyBatis, RabbitMQ, Kafka, Zookeeper, MySQL, Redis and JVM.
Pay attention to my official account: programmer Bai Nannan, get the above information.