MBG介绍
扩展Mybatis的插件,根据表自动生成xml映射器、接口、实体类
在maven工程里使用,需要在pml文件中添加依赖,然后配置xml文件进行使用
具体下载、使用步骤参考列举的文献,我只做重点步骤的列举
参考文献
github.com/mybatis/gen… (官网,可以看最新版本)
blog.csdn.net/qq_36761831… (MBG下载)
mapperhelper.github.io/docs/3.usem… (MBG使用)
blog.csdn.net/qq_35598865… (mybatis.org/dtd/mybatis-generator-config_1_0.dtd标红)
www.jianshu.com/p/e09d2370b… (Mybatis Generator最完整配置详解)
pml依赖
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!-- mbg的合适配置文件 -->
<configurationFile>${basedir}/src/main/resources/mbg.xml</configurationFile>
<!-- 显示执行的细节:在控制台上打印日志 -->
<verbose>true</verbose>
<!-- 覆盖已有文件 -->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!--为插件引入JDBC驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
</plugin>
MGB的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="mycontext" targetRuntime="MyBatis3">
<!--是否生成注释-->
<commentGenerator>
<!--不生成注释-->
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<!--JDBC连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/bsbdj?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<!--不适用BigDecimal ,如果设置为true Mybatis中所有的数字类型统一会被设置为BigDecimal(大数字)
false的时候,则根据数据库字段类型动态生成对应的Integer、Long、Double、Float
通常情况下都是false
-->
<property name="forceBigDecimals" value="false"></property>
</javaTypeResolver>
<!--实体类的生成位置-->
<javaModelGenerator targetPackage="com.zaishixiaoyao.crawlerbsbdj.entity" targetProject="D:\ideademo\crawlerbsbdj\src\main\java">
<!-- 根据包名自动生成对应的目录 -->
<property name="enableSubPackages" value="true"></property>
</javaModelGenerator>
<!--mapper.xml的生成位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="D:\ideademo\crawlerbsbdj\src\main\resources\mybatis">
<property name="enableSubPackages" value="true"></property>
</sqlMapGenerator>
<!-- Mapper接口的生成位置:XMLMAPPER ,代表生成xml文件-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zaishixiaoyao.crawlerbsbdj.mapper" targetProject="D:\ideademo\crawlerbsbdj\src\main\java">
<property name="enableSubPackages" value="true"></property>
</javaClientGenerator>
<!-- 生成Mapper: tableName是表的名称 , domainObjectName是实体类名称-->
<table tableName="t_content" domainObjectName="Content" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
<!--MGB会将表中的text类型数据转换为BLOB类型,我们需要设置让MGB以为表中的text类型是varchar类型,将其转换为String类型-->
<columnOverride column="content_text" javaType="java.lang.String" jdbcType="VARCHAR"></columnOverride>
</table>
<table tableName="t_comment" domainObjectName="Comment" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
</table>
<table tableName="t_forum" domainObjectName="Forum" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
</table>
<table tableName="t_image" domainObjectName="Image" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
<!-- 设置直接回填自动生成的ID号 -->
<!-- 在insert语句执行完后, 执行mysql数据库获取最新主键的语句: select LAST_ID() , 并将新纪录的主键值回填到实体类的ImageId属性中 -->
<!-- 只适用于自动生成的主键-->
<generatedKey column="image_id" sqlStatement="mysql" type="post" identity="true"></generatedKey>
</table>
<table tableName="t_video" domainObjectName="Video" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
<generatedKey column="video_id" sqlStatement="mysql" type="post" identity="true"></generatedKey>
</table>
<table tableName="t_user" domainObjectName="User" enableSelectByExample="false"
enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>