MyBatis Generator(MBG)是mybatis的一个代码生成工具,他可以帮我们根据数据表生成对应的实体、dao、xml文件。具体的可以参考官网:http://mybatis.org/generator/index.html
使用方法:
先新建一个maven工程,在pom文件的build下增加一个plugin
<!--Mybatis Generator--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> </dependencies> <configuration> <!--配置文件的路径--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <!--是否覆盖同名文件(只是针对XML文件,java文件生成类似*.java.1、*.java.2形式的文件)--> <overwrite>false</overwrite> <!--是否将生成过程输出至控制台--> <verbose>true</verbose> </configuration> </plugin>
其中dependencies的部分是数据库的驱动,不同数据库需要自行进行替换
增加MyBatis Generator的配置文件,在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> <context id="generator" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <commentGenerator> <!-- 是否去除自动生成的日期 true:是 false:否 --> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 数据库链接URL,用户名、密码。url中的&符号要用&代替 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/dev?characterEncoding=utf8&useSSL=false" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <!-- 是否强制DECIMAL和NUMERIC字段使用java.math.BigDecimal--> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.lqc.entity" targetProject="src/main/java/"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="com.lqc.dao" targetProject="src/main/java/"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lqc.dao" targetProject="src/main/java/"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 需要生成的表 --> <table tableName="t_article" domainObjectName="Article" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!--mysql中text类型的字段指定生成为String类型(没有相关字段可以不写这一行)--> <columnOverride column="content" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
配置文件中各个配置的作用参考注释或者官方文档
至此配置的部分已经完成,接下来就是运行MyBatis Generator生成代码了,可以使用以下命令:
mvn mybatis-generator:generate
或者直接在idea的侧边栏如图所示位置双击
完整的代码参考:https://gitee.com/lqccan/blog-demo demo16 可以自行下载测试使用