环境jar包下载:
mysql-connector-java-8.0.13
mybatis-generator-core-1.3.7
配置generatorConfig.xml:
需要注意的是MySQL url的配置,因为都用的最新版本,所以跟有些百度的不同。
- allowPublicKeyRetrieval=true 防止出现 Public Key Retrieval is not allowed。
这个东西在maven项目里配置jdbc时可以不加,但在这第一次运行时要加。 - 如果数据库是MySQL 8.x 自定义的表与系统表有同名时,会自动生产两张表的对应代码,而且会有很多冲突和错误,此时设置table的schema也没有效果
jdbcConnection里添加
<property name="nullCatalogMeansCurrent" value="true" />
<?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>
<!--MySQL连接驱动-->
<classPathEntry location="mysql-connector-java-8.0.13.jar" />
<!--数据库链接URL,用户名、密码 -->
<context id="MySQL" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"
userId="root"
password="123456">
<!--如果数据库是MySQL 8.x 自定义的表与系统表有同名时,会自动生产两张表的对应代码,而且会有很多冲突和错误,此时设置table的schema也没有效果 -->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<!--是否启用java.math.BigDecimal-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="shopping.po" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射mapper.xml的包名和位置-->
<sqlMapGenerator targetPackage="shopping.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="shopping.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="items" domainObjectName="Items" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="orderdetail" domainObjectName="Orderdetail" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="orders" domainObjectName="Orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
命令行下生成代码
1、在cmd命令行定位到D:\projects\mybatis-generator-core-1.3.7\lib
2、输入命令java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
正常情况下会提示操作成功。
生成结果