二. 添加配置文件
在src/main/resources 目录下建立两个文件generator.properties 和 generatorConfig.xml,目录结构如下:
配置文件内容如下:
generator.properties:
jdbc.driverLocation=C:\\Users\\JYL\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.32\\mysql-connector-java-5.1.32.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql:///e3mall
jdbc.userId=root
jdbc.password=111
generatorConfig.xml可以只写这个不要properties文件
<?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>
<!--数据库驱动jar包的真实路径 -->
<classPathEntry location="${jdbc.driverLocation}"/> <!--maven仓库中绝对位置-->
<context id="context" targetRuntime="MyBatis3">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<!--如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true-->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--指定包名生成实体类 以及生成的地址 (可以自定义地址,如果路径不存在会自动创建) -->targetProject="D:/code_git/yc-vms-domain/src/main/java"
<javaModelGenerator targetPackage="com.e3mall.entity" targetProject=".\src\test">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! --> targetProject="src/main/resources"
<sqlMapGenerator targetPackage="mappings" targetProject=".\src\test">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! --> targetProject="src/main/java"
<javaClientGenerator targetPackage="com.e3mall.dao" targetProject=".\src\test"
type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_content"/>
<table schema="" tableName="tb_content_category"/>
<table schema="" tableName="tb_item"/>
<table schema="" tableName="tb_item_cat"/>
<table schema="" tableName="tb_item_desc"/>
<table schema="" tableName="tb_item_param"/>
<table schema="" tableName="tb_item_param_item"/>
<table schema="" tableName="tb_order"/>
<table schema="" tableName="tb_order_item"/>
<table schema="" tableName="tb_order_shipping"/>
<table schema="" tableName="tb_user"/>
<!--<table schema="db_wechat" tableName="t_automessage"-->
<!--enableCountByExample="false" enableDeleteByExample="false"-->
<!--enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--<table schema="db_wechat" tableName="t_command"-->
<!--enableCountByExample="false" enableDeleteByExample="false"-->
<!--enableSelectByExample="false" enableUpdateByExample="false"/>-->
</context>
</generatorConfiguration>
三. 设置启动,依次按图片操作
apply ok!
操作成功,代码生成。
<!– ———————– 华丽的分割线———— –>
我在操作成功之前遇到的问题
1.错误:Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project e3-manager-web: configfile G:\YiLiFang\yilifang\e3-parent\e3-manager\e3-manager-web\e3-manager-web\src\main\resources\Personal-GeneratorConfig.xml does not exist -> [Help 1] 这是版本问题
2.
这个红不影响功能,看着难受,解决方法:file-》setting-》languages&frameworks
将这句爆红的添加进去就好了。
3.configfile G:\YiLiFang\yilifang\e3-parent\e3-manager\e3-manager-web\src\main\resources\Personal-GeneratorConfig.xml does not exist
解决,pom文件中添加:src/main/resources/generatorConfig.xml
4.执行成功但没有生成代码:检查路径,检查需要生成表的表名是否正确(我的错误是这个,晕)