生成实体+mapper.xml+dao层映射。
generator.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="my" targetRuntime="MyBatis3">
<!--自动实现Serializable接口-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<!-- 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库基本信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://ip:port/dbName?allowMultiQueries=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useUnicode=true&autoReconnect=true&autoReconnectForPools=true&useCompression=true&useAffectedRows=true&useSSL=false"
userId="userName"
password="passWord">
</jdbcConnection>
<!--生成实体类的位置以及包的名字-->
<!--同样Mac用户:targetProject 为全路径-->
<javaModelGenerator targetPackage="com.*.*.model" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置-->
<!--同样Mac用户:targetProject 为全路径-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成Dao类存放位置,mapper接口生成的位置-->
<!--同样Mac用户:targetProject 为全路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.*.*.dao" targetProject="src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<table schema="dbName" tableName="tableName"
domainObjectName="javaBeanName" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
gradle增加相关插件信息
plugins {
id "com.thinkimi.gradle.MybatisGenerator" version "2.4"
}
apply plugin: "com.thinkimi.gradle.MybatisGenerator"
mybatisGenerator {
verbose = true
configFile = 'src/main/resources/mybatis/generator.xml'
}
双击mbGenerator 生成。
如果提示报错:closing inbound before receiving peer's close_notify
将项目中的mysql的驱动暂时去掉,生成后再加回来即可。