使用 maven的 mybatis-generator 插件生成mapper文件
首先我们需要导入插件
1.pom文件中
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!-- mybatis用于生成代码的配置文件 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> <scope>runtime</scope> </dependency> </dependencies> </plugin>
2.添加 generatorConfig.xml 配置文件
<?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> <!-- 引入外部的配置文件 用于引入属性--> <!-- 例子 : ${spring.datasource.driver-class-name} --> <properties resource="application-local.properties"></properties> <context id="mysql_tables" targetRuntime="MyBatis3"> <!-- 防止生成的代码中有很多注释,加入下面的配置控制 true 不生成注释--> <commentGenerator> <property name="suppressAllComments" value="true" /> <property name="suppressDate" value="true" /> </commentGenerator> <!-- 数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/day20?serverTimezone=GMT%2B8&characterEncoding=utf-8" userId="root" password="1234"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <javaTypeResolver > <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 数据表对应的model层 --> <javaModelGenerator targetPackage="com.asiainfo.bean" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sql mapper 映射配置文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- mybatis3中的mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.asiainfo.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO --> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="test2" domainObjectName = "testBean" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
3.运行maven命令
命令 : mybatis-generator:generate