https://www.jianshu.com/p/5f23ba919d3c,在此文的基础上记录了实际配置过程中遇到的问题
0. 测试数据库创建脚本:
DROP TABLE IF EXISTS `user_t`;
CREATE TABLE `user_t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(40) NOT NULL,
`password` varchar(255) NOT NULL,
`age` int(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `user_t` */
insert into `user_t`(`id`,`user_name`,`password`,`age`) values (1,'测试','sfasgfaf',24);
- 在pom.xml中添加maven依赖:
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.4</version>
</dependency>
- 在pom.xml中添加插件:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.4</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
- 在src/main/resources中创建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>
<!--使用本地仓库的路径,mac上缺省的maven本地仓库在/User/{user.home}/.m2/目录下-->
<!--如果使用的数据是mysql8,需要将jdbc升级至5.5或8,否则会出现Unknown system variable 'query_cache_size'的错误
参考连接:https://github.com/sysown/proxysql/issues/1408-->
<classPathEntry
location="/Users/hs/.m2/repository/mysql/mysql-connector-java/8.0.12/mysql-connector-java-8.0.12.jar"/>
<context id="MysqlTables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8"
userId="root" password="admin">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--配置生成的Bean的存放目录等-->
<javaModelGenerator targetPackage="com.cmos.ssm.beans" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
<property name="useActualColumnNames" value="true"/>
</javaModelGenerator>
<!--配置生成的SQLmap的存放目录等-->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--配置生成的DAO的存放目录等-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.cmos.ssm.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--配置数据库中的表名,生成的Bean名,以及生成哪些方法-->
<table tableName="user_t" domainObjectName="User" enableCountByExample="false"
enableUpdateByExample="true" enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
- 使用终端进入工程目录,执行命令:mvn mybatis-generator:generate