1. First, configure the maven environment with eclipse and create a project with SSM framework
2. Add plugin to pom.xml
<build> <finalName>ssm_web</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> <scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> </plugins> </pluginManagement> </build>
Notice:
1. To add plug-ins to the pom, you need to specify the database driver in the <build></build> tag, and specify the database driver here. Then when configuring generatorConfig.xml in the next step, you do not need to specify the local path of the database driver;
2. src/main/resources/mybatis-generator/generatorConfig.xml specifies the path of the generatorConfig.xml configuration file, and everyone can adjust it according to their actual situation;
3. GeneratorConfig.xml configuration 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="E:/maven/repository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar"/> --> <context id="my" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="false" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/crm" userId="root" password="root" /> <javaModelGenerator targetPackage="com.xdw.model" targetProject="F:/javawebworkspace/ssm_web/src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.xdw.mapping" targetProject="F:/javawebworkspace/ssm_web/src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator targetPackage="com.xdw.dao" targetProject="F:/javawebworkspace/ssm_web/src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="house_type" domainObjectName="HouseType"> <!-- <property name="useActualColumnNames" value="true"/> --> </table> </context></generatorConfiguration>
Configuration explanation:
1. The classPathEntry location does not need to be configured here, because the database driver has been configured in the pom before;
2. jdbcConnection configures the corresponding driver class, URL, username and password according to its own database;
3. javaModelGenerator, sqlMapGenerator, javaClientGenerator configure the corresponding pojo class to be generated, the xml file corresponding to the DAO class and mapper, the targetPackage property specifies the package name, the targetProject specifies the path to your project,
4. The table tag configures the mapping of database tables and entity classes. The tableName property specifies the table name, and the domainObjectName specifies the generated pojo class name; <property name="useActualColumnNames" value="true"/> If configured, then the attribute name of the generated entity class is the same as the database.
The name of each field in the table is the same. If it is not configured, the field name in the data table will be automatically generated through the _connected fields. For example, I have a field named type_id here, and the generated attribute name is typeId;
It is recommended that everyone use_separate words when building database tables;
4. After creation, click maven build
pop up
Fill in mybatis-generator:generate in goals, and click Run
You can also enter mvn mybatis-generator:generate using the maven command line
5. The execution results are as follows:
Summarize
The above is the SSM framework introduced by the editor to you automatically generate code through mybatis-generator. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!