首先 要使用这东西 自动生成mapper dao entity 在pom.xml中加入如下代码
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--配置文件的位置--> <configurationFile>${basedir}/src/main/resources/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> </dependencies> </plugin>
然后idea右边的这个如果有 如下图标记处 说明引入成功
第一个问题来了 如果没有 看下仓库是不是这个包没下下来 把那个lastupdated文件删了 就能重新下了
然后在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> <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--> <classPathEntry location="G:\myRepository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://172.18.193.185:3306/sonar" userId="sonar" password="sonar"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.sitech.smartcity.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="mybatis/mysql" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.sitech.smartcity.mybatis" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="tb_rc_tableexpert_info"></table> </context> </generatorConfiguration>
把上面标红的地方改成自己的路径
然后点击下图所示的这个按键即可生成文件
第二个问题来了 这样子点击生成后的文件会有exmpale之类的文件 如果不想要 修改generatorConfig.xml中的表属性为
<table schema=""
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
tableName="tb_rc_tableexpert_info">
</table>
第三个问题又来了 有的表中字段是xx_xx这种的 默认驼峰命名 即xxXX 如果想使用原字段 要新增属性
<table schema=""
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
tableName="tb_rc_tableexpert_info">
<property name="useActualColumnNames" value="true" />
</table>