0-、前言
在用mybatis开发项目中,数据库动辄上百张数据表,若是你一个一个去手动编写,比较耗费时间;还好,咱们有mybatis-generator插件,只需简单几步就能自动生成mybatis的model、mapper和Dao文件,很方便;java
题外话:注意,mybatis-generator的项目建议单独去建一个项目,生成model、mapper、dao后再根据须要拷到实际项目中去;不要集成到实际项目中,以避免对实际项目形成影响,由于集成在项目中,一不当心生成了,所有覆盖了原来的文件,那你本身添加的不少功能代码就game over了,切勿冒险;
1-、使用方法:
使用mybatis-generator很是简单,只需几步便可:mysql
1-一、新建一个项目,添加依赖:
注意,我指定了版本,其余版本屡次失败,我使用1.3.7版本,稳定好用不失败;里面的<configurationFile>是咱们下面新建的XML配置文件地址git
<dependencies> <!-- 一、添加mybatis.generator依赖 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies> <build> <plugins> <!--二、添加mybatis.generator插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <!--配置XML文件地址,如下为根目录,若是是resources,则为:src/main/resources/generatorConfig.xml --> <!-- <configurationFile>generatorConfig.xml</configurationFile>--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <!--容许移动生成的文件 --> <verbose>true</verbose> <!--是否覆盖 --> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
1-二、添加配置文件:
都加了注释,本身看注释便可,很少解释,须要说一下的是<!---6 ->,github
A、若是是单个生成表生成,就像6-1一个一个表添加就行;sql
B、若是要批量生成,就直接像6-2同样就行,能够用%匹配任意表:tableName="%"匹配全部表;tableName="t_sys_%"表示匹配全部t_sys_开头的表;能够根据本身须要来匹配任意表来生成;数据库
<?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> <!--一、指定特定数据库的jdbc驱动jar包的位置千万千万要指定正确,否则就建立不了文件--> <classPathEntry location="C:\maven\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--二、配置数据库链接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mydb" userId="root" password="88888888"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--三、指定Model生成的位置 --> <javaModelGenerator targetPackage="com.anson.model" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--四、指定sql映射文件生成的位置 --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--4.一、若是放程序包中--> <!-- <sqlMapGenerator --> <!-- targetPackage="com.anson.mapper"--> <!-- targetProject=".\src\main\java">--> <!-- <property name="enableSubPackages" value="true"/>--> <!-- </sqlMapGenerator>--> <!--五、指定dao接口生成的位置 .mapper接口 --> <!-- type生成类型含义,项目中基本都是用:XMLMAPPER type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.anson.dao" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 6-一、单个表生成策略 --> <!--生成对应表及类名--> <!-- <table tableName="t_dept" domainObjectName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">--> <!-- </table>--> <!-- 6-二、整个数据库批量生成策略 --> <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="false" /> <!-- 数据库表主键 --> <!-- <generatedKey column="id" sqlStatement="Mysql" identity="true" /> --> </table> </context> </generatorConfiguration>
好,完毕,就这么简单,下面开始生成:mybatis
1-三、在右侧maven中找到插件,直接双击就能生成了
双击成功生成后,能够看到项目中在本身XML文件填写的包和路径下,生成了相关文件app
把相关文件烤到你的项目中就能够直接使用了,怎么样,是否是很神奇,是否是很简单~dom