The SSM framework automatically generates code through mybatis-generator,
1. Configure the maven environment in eclipse and create an SSM framework project.
2. Add plugin in 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> </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> </pluginManagement> </build>
Note:
1. Add the plug-in to pom in the <build> </build> label and specify the database driver here. Configure generatorConfig in the next step. when using xml, 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. You can adjust it according to your 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 description:
1. classPathEntry location does not need to be configured here, because the database driver has been configured in pom;
2. jdbcConnection configure the driver class, URL, user name and password according to your database;
3. Configure the pojo class to be generated for javaModelGenerator, sqlMapGenerator, and javaClientGenerator. The DAO class and mapper correspond to the xml file. The targetPackage attribute specifies the package name, And the targetProject specifies the path of your project,
4. Configure the ing between database tables and object classes in table labels. The tableName attribute specifies the table name and the domainObjectName specifies the generated pojo class name; <property name = "useActualColumnNames" value = "true"/> If configured, the property name of the generated object class is the same as that of the database.
The names of each field in the table are the same. If this parameter is not set, the field names in the table are automatically generated using the _ connected field. For example, I have a field named type_id, the generated property name is typeId;
We recommend that you use _ to separate words when creating a database table;
4. Click maven build after creation
Pop-up
Enter mybatis-generator: generate in goals, and then click Run
You can also use the maven command line to enter mvn mybatis-generator: generate
5. The execution result is as follows: