mybatis generaot
java 方式生成
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.3.7</version>
<scope>test</scope>
</dependency>
- 创建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="store" targetRuntime="MyBatis3">
</plugin>
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"></plugin>
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"></plugin>
<plugin type="com.itfsw.mybatis.generator.plugins.BatchInsertPlugin">
<property name="allowMultiQueries" value="false" />
</plugin>
<plugin type="com.itfsw.mybatis.generator.plugins.LimitPlugin">
<property name="startPage" value="0" />
</plugin>
<plugin type="com.itfsw.mybatis.generator.plugins.MapperAnnotationPlugin">
<property name="@Mapper" value="true"/>
<property name="@Repository" value="false"/>
</plugin>
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&autoReconnect=true&maxReconnects=1&initialTimeout=50000&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&connectTimeout=50000&socketTimeout=50000"
userId="root"
password="123456"></jdbcConnection>
<javaModelGenerator targetPackage="com.knowledge.dao.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.knowledge.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<table schema="knowledgeshare" tableName="%">
</table>
</context>
</generatorConfiguration>
- 使用java 类方式创建生成类 CodeMybatisGenerator:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class CodeMybatisGenerator {
public static void main(String[] args) throws FileNotFoundException {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
InputStream is = new FileInputStream("src/test/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
DefaultShellCallback shellCallback = new DefaultShellCallback(overwrite);
Configuration configuration = null;
try {
configuration = cp.parseConfiguration(is);
is.close();
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, shellCallback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
e.printStackTrace();
}
for (String warning : warnings) {
System.out.println(warning);
}
}
}
- 注意点:
- 使用main方式时,引入mysql-connector-java 依赖后,不需要再xml文件中手动指定mysql-connector-java jar包的路径了
使用maven 插件方式生成
- pom 中引入如下(指定配置文件位置以及依赖的jar包):
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
src/test/resources/generatorConfig.xml
</configurationFile>
<verbose>true</verbose>
<overwrite>false</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<dependency>
<groupId>groupId</groupId>
<artifactId>codegenerate</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 运行generator即可
- maven 插件所以来的jar包和项目不同,需要再build中额外添加所依赖的组件
自定义mybatis generator 介绍
- 继承PluginAdapter类,其中validate返回true;
- 实现增加mapper 注解,merge通过反射设置为false;
- 基础:
- 增加 import:
- 使用 FullyQualifiedJavaType,构造方法中添加参数完成的类路径
- 对应的类中调用 addImportedType 方法
- 添加接口注释:
- 调用addAnnotation 方式,参数为注释的完整字符串
- 增加model 类元素:
- 构建field 对象,设置元素的名称 ,可见性 ,类型
- 元素的类型 需要封装,如int 类型,需要调用
FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper()
得到对应int 类型实例的对象,注意getIntInstance() 中int; - 对应的类中添加addField();
- 增加对应元素的get set方法:
- 构建Method,设置可见性,名称,参数,方法体
- 参数要使用 Parameter对象,其构造函数中传入 类型包裹,名称
- 返回值使用 类型包裹
- 方法体使用字符串代码
FullyQualifiedJavaType javaType=new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper");
interfaze.addImportedType(javaType);
interfaze.addAnnotation("@Mapper");
Field limit = new Field();
limit.setName("limit");
limit.setVisibility(JavaVisibility.PRIVATE);
limit.setType(integerWrapper);
topLevelClass.addField(limit);
Method setLimit = new Method();
setLimit.setVisibility(JavaVisibility.PUBLIC);
setLimit.setName("setLimit");
setLimit.addParameter(new Parameter(integerWrapper, "limit"));
setLimit.addBodyLine("this.limit = limit;");
topLevelClass.addMethod(setLimit);
getLimit.setReturnType(integerWrapper);
- 实现需要改动的方法
- clientGenerated : java接口准备完成后调用
- modelExampleClassGenerated : model类的example 类准备完成后调用
- sqlMapSelectByExampleWithoutBLOBsElementGenerated xml 文件中SelectByExample类型准备完成后调用
- sqlMapInsertElementGenerated :insert 语句准备完成后调用
- sqlMapInsertSelectiveElementGenerated :InsertSelective语句准备完成后调用