IDEA Maven Mybatis generator example explanation of automatic code generation
1. Install and configure maven and configure maven in Idea
For the installation process steps, you can read the blog post above, which is introduced in detail.
2. Create a data table
DROP TABLE IF EXISTS `t_user`;CREATE TABLE `t_user` ( `id` varchar(100) NOT NULL, `username` varchar(20) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, `headerPic` varchar(60) DEFAULT NULL, `email` varchar(60) DEFAULT NULL, `sex` varchar(2) DEFAULT NULL, `create_time` datetime DEFAULT NULL, `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `is_delete` int(1) DEFAULT NULL, `address` varchar(200) DEFAULT NULL, `telephone` varchar(15) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Idea creates a maven project
1. Click create new project-》maven-》create from archetype->maven-archetype-webapp, and then click next, the steps are as shown in the figure:
2. Fill in groupId and ArtifactId: (the values of these two parameters are defined by yourself). The following paragraph was copied online so that everyone can better understand these two parameters.
groupid and artifactId are collectively referred to as "coordinates" and are proposed to ensure project uniqueness. If you want to get your project to the maven local repository, if you want to find your project, you must search based on these two ids.
It is generally divided into multiple segments. Here I will only talk about two segments. The first segment is the domain and the second segment is the company name. The domains are divided into org, com, cn, etc., among which org is a non-profit organization and com is a commercial organization. Take an example of the tomcat project of apache company: the groupId of this project is org.apache, its domain is org (because tomcat is a non-profit project), the company name is apache, and the artigactId is tomcat.
For example, if I create a project, I usually set groupId to cn.laok. cn means that the domain is China. laok is my personal abbreviation. artifactId is set to testProj, which means that the name of your project is testProj. According to this setting, your package structure should be started by cn.laok.testProj. If there is a UserDao, its full path is cn.laok.testProj.dao.UserDao.
3. Click next to configure maven information, as shown in the figure:
4. Click next to fill in the project name, as shown in the figure:
5. After the creation is completed, the structure of the project is as shown in the figure. Before generating the code, there is no need to create other folders, but the resources folder needs to be set to Resources Root (right-click the resources folder-》Mark Directory As->Resources Root)
4. Configure pom.xml and generatorConfig.xml
1. Add the following configuration to pom.xml:
<build> <finalName>create-code</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins></build>
2. Create generatorConfig.xml under the resources source folder
<?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="D:/Java/lib/mysql-connector-java-5.1.43-bin.jar" /> <context id="test" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- This element is used to remove whether the generated comment contains the generated date false: indicates protection--> <!-- If the generated date is modified, it will cause all properties of the entire entity class to change even if a field is modified, which is not conducive to version control, so it is set to true --> <property name="suppressDate" value="true" /> <!-- Whether to remove the automatically generated comments true: Yes: false: No--> <property name="suppressAllComments" value="false" /> </commentGenerator> <!--Database link URL, username, password--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/article" userId="root" password=""> </jdbcConnection> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- Define the package name and location folder of the generated model yourself-> <javaModelGenerator targetPackage="com.test.model" targetProject="target"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- Generate the package name and location folder of the mapping file to define it yourself-> <sqlMapGenerator targetPackage="com.test.mapping" targetProject="target"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- Generate the package name and location folder of the DAO to define it yourself-> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" implementationPackage="com.test.dao.impl" targetProject="target"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- Which tables to generate--> <table tableName="t_user" domainObjectName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context></generatorConfiguration>
3. After the configuration is completed, be sure to click Build->Rebuild project to generate the target folder. Otherwise, when producing the code, it will be produced under the target file. If there is no such folder, an error will be reported. Of course, it can also be configured and generated under other folders. The project structure is shown in the figure:
One thing to note: be sure to add the local mysql-connector-java-5.1.43-bin.jar to the configuration file.
Download address https://dev.mysql.com/downloads/connector/j/
Then unzip it to the local area, my configuration is as follows: <classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43-bin.jar" />
This requires everyone to configure it according to the path they store.
5. Execute the generated code
1. Click run->Edit configurations, as shown in the figure:
2. Then the Run Configuration box pops up and configure a name for the current configuration. Here it is called "generator", and then enter "mybatis-generator:generate -e" in the "Command line" option.
The "-e" option is added here to allow the plugin to output detailed information, which can help us locate the problem.
3. After the configuration is completed, click run-》run generator. If nothing unexpected happens, the info information of BUILD SUCCESS will appear in the console. The complete effect is shown in the figure:
There is something wrong with the writing. Please correct me. Thank you very much.
The above IDEA Maven Mybatis generator automatic code generation (example explanation) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.