Compare commits

..
14 Commits
16 changed files with 384 additions and 54 deletions
+33 -33
View File
@@ -31,7 +31,7 @@ Maven引用:
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.2.7</version>
<version>1.2.9</version>
</dependency>
```
---------------------------------------
@@ -348,7 +348,7 @@ public class Test {
// 构建插入数据
List<Tb> list = new ArrayList<>();
list.add(
new Tb.Builder()
Tb.builder()
.field1(0)
.field2("xx0")
.field3(0)
@@ -356,7 +356,7 @@ public class Test {
.build()
);
list.add(
new Tb.Builder()
Tb.builder()
.field1(1)
.field2("xx1")
.field3(1)
@@ -393,9 +393,9 @@ public class Test {
<!-- 逻辑删除-未删除值 -->
<property name="logicalUnDeleteValue" value="0"/>
<!-- 逻辑删除常量名称,不配置默认为 IS_DELETED -->
<property name="logicalDeleteValue" value="DEL"/>
<property name="logicalDeleteConstName" value="IS_DELETED"/>
<!-- 逻辑删除常量(未删除)名称,不配置默认为 NOT_DELETED -->
<property name="logicalUnDeleteValue" value="UN_DEL"/>
<property name="logicalUnDeleteConstName" value="NOT_DELETED"/>
</plugin>
<table tableName="tb">
@@ -434,7 +434,7 @@ public class Test {
);
// 4. 逻辑删除和未删除常量
Tb tb = new Tb.Builder()
Tb tb = Tb.builder()
.delFlag(Tb.IS_DELETED) // 删除
.delFlag(Tb.NOT_DELETED) // 未删除
.build()
@@ -486,7 +486,7 @@ public class Test {
// 3. 配合批量插入插件(BatchInsertPlugin)使用实现按需插入指定列
List<Tb> list = new ArrayList<>();
list.add(
new Tb.Builder()
Tb.builder()
.field1(0)
.field2("xx0")
.field3(0)
@@ -494,7 +494,7 @@ public class Test {
.build()
);
list.add(
new Tb.Builder()
Tb.builder()
.field1(1)
.field2("xx1")
.field3(1)
@@ -536,7 +536,7 @@ public class Test {
public class Test {
public static void main(String[] args) {
// 1. 未入库数据入库,执行insert
Tb tb = new Tb.Builder()
Tb tb = Tb.builder()
.field1(1)
.field2("xx0")
.delFlag(Tb.DEL_FLAG_ON)
@@ -547,7 +547,7 @@ public class Test {
int k1 = this.tbMapper.upsert(tb);
// 3. 类似insertSelective实现选择入库
Tb tb1 = new Tb.Builder()
Tb tb1 = Tb.builder()
.field1(1)
.field2("xx0")
.build();
@@ -557,7 +557,7 @@ public class Test {
// --------------------------------- allowMultiQueries=true ------------------------------
// 4. 开启allowMultiQueries后增加upsertByExample,upsertByExampleSelective但强力建议不要使用(需保证团队没有使用statement提交sql,否则会存在sql注入风险)
Tb tb2 = new Tb.Builder()
Tb tb2 = Tb.builder()
.field1(1)
.field2("xx0")
.field3(1003)
@@ -581,17 +581,17 @@ public class Test {
// upsertByExampleSelective 用法类似
// 当Model WithBLOBs 存在时上述方法增加对应的 WithBLOBs 方法,举例如下:
TbWithBLOBs tb3 = new Tb.Builder()
.field1(1)
.field2("xx0")
.delFlag(Tb.DEL_FLAG_ON)
.build();
TbWithBLOBs tb3 = Tb.builder()
.field1(1)
.field2("xx0")
.delFlag(Tb.DEL_FLAG_ON)
.build();
int k6 = this.tbMapper.upsertWithBLOBs(tb);
// --------------------------------- allowBatchUpsert=true ------------------------------
List<Tb> list = new ArrayList<>();
list.add(
new Tb.Builder()
Tb.builder()
.field1(0)
.field2("xx0")
.field3(0)
@@ -599,7 +599,7 @@ public class Test {
.build()
);
list.add(
new Tb.Builder()
Tb.builder()
.field1(1)
.field2("xx1")
.field3(1)
@@ -627,7 +627,7 @@ public class Test {
public static void main(String[] args) {
// ------------------------------ 新版本(SelectiveEnhancedPlugin)--------------------------------
// 1. 指定插入或更新字段
Tb tb = new Tb.Builder()
Tb tb = Tb.builder()
.field1(1)
.field2("xx2")
.field3(1)
@@ -656,7 +656,7 @@ public class Test {
// 2. 更新某些字段为null
this.tbMapper.updateByPrimaryKeySelective(
new Tb.Builder()
Tb.builder()
.id(1l)
.field1(null) // 方便展示,不用设也可以
.build(),
@@ -1104,7 +1104,7 @@ Mybatis Generator是原生支持自定义注释的(commentGenerator配置type
public class Test {
public static void main(String[] args) {
// 在构建更新对象时,配置了增量支持的字段会增加传入增量枚举的方法
Tb tb = new Tb.Builder()
Tb tb = Tb.builder()
.id(102)
.field1(1, Tb.Builder.Inc.INC) // 字段1 统计增加1
.field2(2, Tb.Builder.Inc.DEC) // 字段2 统计减去2
@@ -1177,13 +1177,13 @@ public class Test {
<xml>
<!-- 乐观锁插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.OptimisticLockerPlugin">
<!-- 是否启用自定义nextVersion, 默认不启用使用(使用sql的 set column = column + 1) -->
<property name="customizedNextVersion" value="true"/>
<!-- 是否启用自定义nextVersion,默认不启用(插件会默认使用sql的 set column = column + 1) -->
<property name="customizedNextVersion" value="false"/>
</plugin>
<table tableName="tb">
<!-- 这里可以单独表配置,覆盖全局配置 -->
<property name="customizedNextVersion" value="false"/>
<property name="customizedNextVersion" value="true"/>
<!-- 指定版本列 -->
<property name="versionColumn" value="version"/>
</table>
@@ -1214,21 +1214,21 @@ public class Test {
// ============================= 使用默认版本号生成策略 ===========================
this.tbMapper.updateWithVersionByPrimaryKey(
100, // 版本号
new Tb.Builder()
.id(102)
.field1("ts1")
.build()
Tb.builder()
.id(102)
.field1("ts1")
.build()
);
// 对应生成的Sql: update tb set version = version + 1, field1 = 'ts1' where version = 100 and id = 102
// ============================= 使用自定义版本号生成策略 ===========================
this.tbMapper.updateWithVersionByPrimaryKey(
100, // 版本号
new Tb.Builder()
.id(102)
.field1("ts1")
.nextVersion(System.currentTimeMillis()) // 传入nextVersion
.build()
Tb.builder()
.id(102)
.field1("ts1")
.nextVersion(System.currentTimeMillis()) // 传入nextVersion
.build()
);
// 对应生成的Sql: update tb set version = 1525773888559, field1 = 'ts1' where version = 100 and id = 102
}
+2 -2
View File
@@ -3,7 +3,7 @@
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.2.8</version>
<version>1.2.10</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
@@ -217,7 +217,7 @@
<connection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</connection>
<developerConnection>scm:git:https://github.com/itfsw/mybatis-generator-plugin.git</developerConnection>
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
<tag>V1.2.8</tag>
<tag>V1.2.10</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -52,7 +52,8 @@ public class BatchInsertPlugin extends BasePlugin {
// 插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.microsoft.jdbc.sqlserver.SQLServer".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.microsoft.sqlserver.jdbc.SQLServerDriver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
&& "com.microsoft.sqlserver.jdbc.SQLServerDriver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.mysql.cj.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!");
return false;
}
@@ -13,8 +13,6 @@ import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import java.util.List;
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
/**
* ---------------------------------------------------------------------------
* Example 增强插件
@@ -130,14 +128,7 @@ public class ExampleEnhancedPlugin extends BasePlugin {
// 方法体
sb.setLength(0);
if (stringHasValue(introspectedColumn.getTypeHandler())) {
sb.append("add");
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
sb.append("Criterion(");
} else {
sb.append("addCriterion(");
}
sb.append("addCriterion(");
sb.append("new StringBuilder(\"");
sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
sb.append(" ");
@@ -42,7 +42,8 @@ public class LimitPlugin extends BasePlugin {
@Override
public boolean validate(List<String> warnings) {
// 该插件只支持MYSQL
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.mysql.cj.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "只支持MySQL数据库!");
return false;
}
@@ -69,6 +69,8 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
if (this.versionColumn == null) {
warnings.add("itfsw(乐观锁插件):表" + introspectedTable.getFullyQualifiedTable() + "配置的版本列(" + introspectedTable.getTableConfigurationProperty(PRO_VERSION_COLUMN) + ")没有找到!");
}
} else {
this.versionColumn = null;
}
// 自定义nextVersion
@@ -169,11 +169,9 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
// warning has already been reported
if (introspectedColumn != null) {
if (gk.isJdbcStandard()) {
answer.addAttribute(new Attribute("useGeneratedKeys", "true"));
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
XmlElementGeneratorTools.useGeneratedKeys(answer, introspectedTable, "record.");
} else {
answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk));
answer.addElement(XmlElementGeneratorTools.getSelectKey(introspectedColumn, gk, "record."));
}
}
}
@@ -65,7 +65,8 @@ public class UpsertPlugin extends BasePlugin {
public boolean validate(List<String> warnings) {
// 插件使用前提是数据库为MySQL
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.mysql.cj.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
warnings.add("itfsw:插件" + this.getClass().getTypeName() + "插件使用前提是数据库为MySQL!");
return false;
}
@@ -52,11 +52,15 @@ public class XmlElementGeneratorTools {
* @return the selectKey element
*/
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
return getSelectKey(introspectedColumn, generatedKey, null);
}
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey, String prefix) {
String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();
XmlElement answer = new XmlElement("selectKey");
answer.addAttribute(new Attribute("resultType", identityColumnType));
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("order", generatedKey.getMyBatis3Order()));
answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));
@@ -71,9 +71,75 @@ public class BugFixedTest {
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns);
Assert.assertEquals(sql, "select id , field1 , `table` from tb Test WHERE ( Test.id < '160' ) order by `table` ASC");
ObjectUtil result1 = new ObjectUtil(((List)tbMapper.invoke("selectByExampleSelective", tbExample.getObject(), columns)).get(0));
ObjectUtil result1 = new ObjectUtil(((List) tbMapper.invoke("selectByExampleSelective", tbExample.getObject(), columns)).get(0));
Assert.assertEquals(result1.get("table"), "tb");
}
});
}
/**
* insertSelective 因为集成SelectiveEnhancedPlugin,传入参数变成map,自增ID返回要修正
* @throws Exception
*/
@Test
public void bug0002() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/bug-0002.xml");
tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/bug-0002.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
tb.set("field1", "ts1");
// selective
ObjectUtil TbColumnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#field1");
Object columns = Array.newInstance(TbColumnField1.getCls(), 1);
Array.set(columns, 0, TbColumnField1.getObject());
// sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "insertSelective", tb.getObject(), columns);
Assert.assertEquals(sql, "insert into tb ( field1 ) values ( 'ts1' )");
Object result = tbMapper.invoke("insertSelective", tb.getObject(), columns);
Assert.assertEquals(result, 1);
// 自增ID
Assert.assertEquals(tb.get("id"), 1L);
}
});
}
/**
* typeHandler 导致的问题
*/
@Test
public void issues36() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/issues-36.xml");
tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-36.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
// Example 编译报错
new ObjectUtil(loader, packagz + ".TbExample");
}
});
}
/**
* 乐观锁插件好像变量作用域问题导致,前一个表的配置会影响后一个表配置
*/
@Test
public void issues39() throws Exception {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/BugFixedTest/issues-39.xml");
tool.generate(() -> DBHelper.createDB("scripts/BugFixedTest/issues-39.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
// 第一个生成对应方法
Assert.assertEquals(tbMapper.getMethods(OptimisticLockerPlugin.METHOD_DELETE_WITH_VERSION_BY_EXAMPLE).size(), 1);
ObjectUtil tb1Mapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".Tb1Mapper")));
// 第一个生成对应方法
Assert.assertEquals(tb1Mapper.getMethods(OptimisticLockerPlugin.METHOD_DELETE_WITH_VERSION_BY_EXAMPLE).size(), 0);
}
});
}
}
@@ -0,0 +1,26 @@
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-06-27 11:17:08
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP TABLE IF EXISTS `tb`;
CREATE TABLE `tb` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`field1` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"/>
<!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject="">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="" targetProject="" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
@@ -0,0 +1,27 @@
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-06-27 11:17:08
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP TABLE IF EXISTS `tb`;
CREATE TABLE `tb` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`t_date` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"/>
<!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 数据Model属性对应Column获取插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!-- Selective选择插入更新增强插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"/>
<!-- Example Criteria 增强插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin"/>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject="">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="" targetProject="" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<columnOverride column="field1" javaType="java.sql.Array" typeHandler="org.apache.ibatis.type.ArrayTypeHandler"/>
</table>
</context>
</generatorConfiguration>
@@ -0,0 +1,38 @@
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-06-27 11:17:08
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP TABLE IF EXISTS `tb`;
CREATE TABLE `tb` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`vs` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for tb1
-- ----------------------------
DROP TABLE IF EXISTS `tb1`;
CREATE TABLE `tb1` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`vs` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"/>
<!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 乐观锁插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.OptimisticLockerPlugin"></plugin>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject="">
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="" targetProject="" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<property name="versionColumn" value="vs"/>
</table>
<table tableName="tb1">
</table>
</context>
</generatorConfiguration>