Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4741d9ebe | ||
|
|
fef2b76d6e | ||
|
|
adbfe3a2db | ||
|
|
f3d2a6fdba | ||
|
|
9fa87b4222 | ||
|
|
fe83da74ba | ||
|
|
f50f031dea | ||
|
|
21743bf020 | ||
|
|
51eb354d83 | ||
|
|
d2ede434ed | ||
|
|
bde5387f41 | ||
|
|
5b89754a1e |
@@ -27,7 +27,7 @@ Maven引用:
|
||||
<dependency>
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
```
|
||||
---------------------------------------
|
||||
@@ -698,18 +698,20 @@ Mybatis Generator是原生支持自定义注释的(commentGenerator配置type
|
||||
|
||||
>warning: 模板引擎采用的是freemarker所以一些freemarker指令参数(如:<#if xx></#if>、${.now?string("yyyy-MM-dd HH:mm:ss")})都是可以使用的,请自己尝试。
|
||||
|
||||
>warning: [默认模板](https://github.com/itfsw/mybatis-generator-plugin/blob/master/src/main/resources/default-comment.ftl)
|
||||
|
||||
| 注释ID | 传入参数 | 备注 |
|
||||
| ----- | ----- | ---- |
|
||||
| addJavaFileComment | mgb<br>compilationUnit | Java文件注释 |
|
||||
| addComment | mgb<br>xmlElement | Xml节点注释 |
|
||||
| addRootComment | mgb<br>rootElement | Xml根节点注释 |
|
||||
| addFieldComment | mgb<br>field<br>introspectedTable<br>introspectedColumn | Java 字段注释(非生成Model对应表字段时,introspectedColumn可能不存在) |
|
||||
| addModelClassComment | mgb<br>topLevelClass<br>introspectedTable | 表Model类注释 |
|
||||
| addClassComment | mgb<br>innerClass<br>introspectedTable | 类注释 |
|
||||
| addEnumComment | mgb<br>innerEnum<br>introspectedTable | 枚举注释 |
|
||||
| addInterfaceComment | mgb<br>innerInterface<br>introspectedTable | 接口注释(itfsw插件新增类型) |
|
||||
| addGetterComment | mgb<br>method<br>introspectedTable<br>introspectedColumn | getter方法注释(introspectedColumn可能不存在) |
|
||||
| addSetterComment | mgb<br>method<br>introspectedTable<br>introspectedColumn | getter方法注释(introspectedColumn可能不存在) |
|
||||
| addJavaFileComment | mgb<br>[compilationUnit](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/java/CompilationUnit.java) | Java文件注释 |
|
||||
| addComment | mgb<br>[xmlElement](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/xml/XmlElement.java) | Xml节点注释 |
|
||||
| addRootComment | mgb<br>[rootElement](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/xml/XmlElement.java) | Xml根节点注释 |
|
||||
| addFieldComment | mgb<br>[field](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/java/Field.java)<br>[introspectedTable](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/IntrospectedTable.java)<br>[introspectedColumn](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/IntrospectedColumn.java) | Java 字段注释(非生成Model对应表字段时,introspectedColumn可能不存在) |
|
||||
| addModelClassComment | mgb<br>[topLevelClass](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/java/TopLevelClass.java)<br>introspectedTable | 表Model类注释 |
|
||||
| addClassComment | mgb<br>[innerClass](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/java/InnerClass.java)<br>introspectedTable | 类注释 |
|
||||
| addEnumComment | mgb<br>[innerEnum](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/java/InnerEnum.java)<br>introspectedTable | 枚举注释 |
|
||||
| addInterfaceComment | mgb<br>[innerInterface](https://github.com/itfsw/mybatis-generator-plugin/blob/master/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/InnerInterface.java)<br>introspectedTable | 接口注释(itfsw插件新增类型) |
|
||||
| addGetterComment | mgb<br>[method](https://github.com/mybatis/generator/blob/master/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/dom/java/Method.java)<br>introspectedTable<br>introspectedColumn | getter方法注释(introspectedColumn可能不存在) |
|
||||
| addSetterComment | mgb<br>method<br>introspectedTable<br>introspectedColumn | setter方法注释(introspectedColumn可能不存在) |
|
||||
| addGeneralMethodComment | mgb<br>method<br>introspectedTable | 方法注释 |
|
||||
|
||||
插件:
|
||||
@@ -1043,6 +1045,8 @@ public class Test {
|
||||
一般我们在做查询优化的时候会要求查询返回时不要返回自己不需要的字段数据,因为Sending data所花费的时间和加大内存的占用
|
||||
,所以我们看到官方对于大数据的字段会拆分成xxxWithBLOBs的操作,但是这种拆分还是不能精确到具体列返回。
|
||||
所以该插件的作用就是精确指定查询操作所需要返回的字段信息,实现查询的精确返回。
|
||||
>warning: 因为采用的是resultMap进行的属性绑定(即时设置了constructorBased=true也无效,因为参数个数不一致会导致异常,该插件也会另外生成一个基于属性绑定的resultMap),
|
||||
所以可能会出现list中存在null元素的问题,这个是mybatis自身机制造成的当查询出来的某行所有列都为null时不会生成对象(PS:其实这个不能算是错误,mybatis这样处理也说的通,但是在使用时还是要注意null的判断,当然如果有什么配置或者其他处理方式欢迎反馈哦)。
|
||||
|
||||
插件:
|
||||
```xml
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>com.itfsw</groupId>
|
||||
<artifactId>mybatis-generator-plugin</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.2</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
@@ -212,7 +212,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.1.0</tag>
|
||||
<tag>V1.1.2</tag>
|
||||
</scm>
|
||||
|
||||
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||
|
||||
@@ -25,6 +25,8 @@ import org.mybatis.generator.api.dom.xml.Document;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
|
||||
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithBLOBsElementGenerator;
|
||||
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithoutBLOBsElementGenerator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -42,6 +44,7 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
public static final String METHOD_SELECT_BY_EXAMPLE_SELECTIVE = "selectByExampleSelective";
|
||||
public static final String METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE = "selectByPrimaryKeySelective";
|
||||
public static final String METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE = "selectOneByExampleSelective";
|
||||
public static final String ID_FOR_PROPERTY_BASED_RESULT_MAP = "BasePropertyResultMap";
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
@@ -139,6 +142,29 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
*/
|
||||
@Override
|
||||
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
|
||||
// issues#16
|
||||
if (introspectedTable.isConstructorBased()) {
|
||||
XmlElement resultMapEle = new XmlElement("resultMap");
|
||||
resultMapEle.addAttribute(new Attribute("id", ID_FOR_PROPERTY_BASED_RESULT_MAP));
|
||||
String returnType;
|
||||
if (introspectedTable.getRules().generateBaseRecordClass()) {
|
||||
returnType = introspectedTable.getBaseRecordType();
|
||||
} else {
|
||||
returnType = introspectedTable.getPrimaryKeyType();
|
||||
}
|
||||
|
||||
resultMapEle.addAttribute(new Attribute("type", returnType));
|
||||
commentGenerator.addComment(resultMapEle);
|
||||
|
||||
if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
|
||||
addResultMapElementsWithBLOBs(resultMapEle, introspectedTable);
|
||||
} else if (introspectedTable.getRules().generateBaseResultMap()) {
|
||||
addResultMapElementsWithoutBLOBs(resultMapEle, introspectedTable);
|
||||
}
|
||||
document.getRootElement().getElements().add(0, resultMapEle);
|
||||
}
|
||||
|
||||
|
||||
// 生成返回字段节点
|
||||
XmlElement columnsEle = new XmlElement("foreach");
|
||||
columnsEle.addAttribute(new Attribute("collection", "selective"));
|
||||
@@ -151,13 +177,25 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
commentGenerator.addComment(selectByExampleSelectiveEle);
|
||||
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("id", METHOD_SELECT_BY_EXAMPLE_SELECTIVE));
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("resultType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
// issues#16
|
||||
if (introspectedTable.isConstructorBased()) {
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("resultMap", ID_FOR_PROPERTY_BASED_RESULT_MAP));
|
||||
} else {
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
|
||||
}
|
||||
selectByExampleSelectiveEle.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
selectByExampleSelectiveEle.addElement(new TextElement("select"));
|
||||
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
|
||||
selectByExampleSelectiveEle.addElement(new TextElement("'" + introspectedTable.getSelectByExampleQueryId() + "' as QUERYID,"));
|
||||
}
|
||||
|
||||
// issues#20
|
||||
XmlElement ifDistinctElement = new XmlElement("if");
|
||||
ifDistinctElement.addAttribute(new Attribute("test", "example.distinct"));
|
||||
ifDistinctElement.addElement(new TextElement("distinct"));
|
||||
selectByExampleSelectiveEle.addElement(ifDistinctElement);
|
||||
|
||||
selectByExampleSelectiveEle.addElement(columnsEle);
|
||||
selectByExampleSelectiveEle.addElement(new TextElement("from " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
|
||||
|
||||
@@ -171,11 +209,16 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
FormatTools.addElementWithBestPosition(document.getRootElement(), selectByExampleSelectiveEle);
|
||||
|
||||
// 2. selectByPrimaryKeySelective
|
||||
XmlElement selectByPrimaryKeySelectiveEle = new XmlElement("select");
|
||||
XmlElement selectByPrimaryKeySelectiveEle = new XmlElement("select");
|
||||
commentGenerator.addComment(selectByPrimaryKeySelectiveEle);
|
||||
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("id", METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE));
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("resultType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
// issues#16
|
||||
if (introspectedTable.isConstructorBased()) {
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("resultMap", ID_FOR_PROPERTY_BASED_RESULT_MAP));
|
||||
} else {
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
|
||||
}
|
||||
selectByPrimaryKeySelectiveEle.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
selectByPrimaryKeySelectiveEle.addElement(new TextElement("select"));
|
||||
@@ -190,14 +233,14 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
|
||||
sb.setLength(0);
|
||||
if (and) {
|
||||
sb.append(" and ");
|
||||
sb.append(" and ");
|
||||
} else {
|
||||
sb.append("where ");
|
||||
sb.append("where ");
|
||||
and = true;
|
||||
}
|
||||
|
||||
sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
|
||||
sb.append(" = ");
|
||||
sb.append(" = ");
|
||||
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, introspectedTable.getRules().generatePrimaryKeyClass() ? "record." : null));
|
||||
selectByPrimaryKeySelectiveEle.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
@@ -210,7 +253,12 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
commentGenerator.addComment(selectOneByExampleSelectiveEle);
|
||||
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("id", METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE));
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("resultType", introspectedTable.getRules().calculateAllFieldsClass().getFullyQualifiedName()));
|
||||
// issues#16
|
||||
if (introspectedTable.isConstructorBased()) {
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("resultMap", ID_FOR_PROPERTY_BASED_RESULT_MAP));
|
||||
} else {
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("resultMap", introspectedTable.getBaseResultMapId()));
|
||||
}
|
||||
selectOneByExampleSelectiveEle.addAttribute(new Attribute("parameterType", "map"));
|
||||
|
||||
selectOneByExampleSelectiveEle.addElement(new TextElement("select"));
|
||||
@@ -236,4 +284,58 @@ public class SelectSelectivePlugin extends BasePlugin {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param answer
|
||||
* @param introspectedTable
|
||||
* @see ResultMapWithoutBLOBsElementGenerator#addResultMapElements(XmlElement)
|
||||
*/
|
||||
private void addResultMapElementsWithoutBLOBs(XmlElement answer, IntrospectedTable introspectedTable) {
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
|
||||
XmlElement resultElement = new XmlElement("id");
|
||||
|
||||
resultElement.addAttribute(new Attribute("column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
|
||||
resultElement.addAttribute(new Attribute("property", introspectedColumn.getJavaProperty()));
|
||||
resultElement.addAttribute(new Attribute("jdbcType", introspectedColumn.getJdbcTypeName()));
|
||||
|
||||
if (stringHasValue(introspectedColumn.getTypeHandler())) {
|
||||
resultElement.addAttribute(new Attribute("typeHandler", introspectedColumn.getTypeHandler()));
|
||||
}
|
||||
|
||||
answer.addElement(resultElement);
|
||||
}
|
||||
|
||||
List<IntrospectedColumn> columns = introspectedTable.getBaseColumns();
|
||||
for (IntrospectedColumn introspectedColumn : columns) {
|
||||
XmlElement resultElement = new XmlElement("result");
|
||||
|
||||
resultElement.addAttribute(new Attribute("column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
|
||||
resultElement.addAttribute(new Attribute("property", introspectedColumn.getJavaProperty()));
|
||||
resultElement.addAttribute(new Attribute("jdbcType", introspectedColumn.getJdbcTypeName()));
|
||||
|
||||
if (stringHasValue(introspectedColumn.getTypeHandler())) {
|
||||
resultElement.addAttribute(new Attribute("typeHandler", introspectedColumn.getTypeHandler()));
|
||||
}
|
||||
answer.addElement(resultElement);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param answer
|
||||
* @param introspectedTable
|
||||
* @see ResultMapWithBLOBsElementGenerator#addResultMapElements(XmlElement)
|
||||
*/
|
||||
private void addResultMapElementsWithBLOBs(XmlElement answer, IntrospectedTable introspectedTable) {
|
||||
for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
|
||||
XmlElement resultElement = new XmlElement("result");
|
||||
resultElement.addAttribute(new Attribute("column", MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
|
||||
resultElement.addAttribute(new Attribute("property", introspectedColumn.getJavaProperty()));
|
||||
resultElement.addAttribute(new Attribute("jdbcType", introspectedColumn.getJdbcTypeName()));
|
||||
|
||||
if (stringHasValue(introspectedColumn.getTypeHandler())) {
|
||||
resultElement.addAttribute(new Attribute("typeHandler", introspectedColumn.getTypeHandler()));
|
||||
}
|
||||
answer.addElement(resultElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,24 +79,33 @@ public class SelectSelectivePluginTest {
|
||||
|
||||
// 2. 执行sql
|
||||
List list = (List) tbMapper.invoke("selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(list.size(), 3);
|
||||
Assert.assertEquals(list.size(), 4);
|
||||
int index = 0;
|
||||
for (Object obj : list) {
|
||||
ObjectUtil objectUtil = new ObjectUtil(obj);
|
||||
// 没有查询这两个字段
|
||||
if (objectUtil.get("id") != null || objectUtil.get("field2") != null) {
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
if (index == 0) {
|
||||
Assert.assertEquals(objectUtil.get("field1"), "fd1");
|
||||
} else if (index == 1) {
|
||||
Assert.assertNull(objectUtil.get("field1"));
|
||||
if (index == 1) {
|
||||
Assert.assertNull(obj);
|
||||
} else {
|
||||
Assert.assertEquals(objectUtil.get("field1"), "fd3");
|
||||
ObjectUtil objectUtil = new ObjectUtil(obj);
|
||||
// 没有查询这两个字段
|
||||
if (objectUtil.get("id") != null || objectUtil.get("field2") != null) {
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
if (index == 0) {
|
||||
Assert.assertEquals(objectUtil.get("field1"), "fd1");
|
||||
} else {
|
||||
Assert.assertEquals(objectUtil.get("field1"), "fd3");
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
// 3. 测试 distinct
|
||||
tbExample.invoke("setDistinct", true);
|
||||
String sql1 = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(sql1, "select distinct field1 from tb WHERE ( id < '100' ) order by field2 asc");
|
||||
List list1 = (List) tbMapper.invoke("selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(list1.size(), 3);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -188,4 +197,119 @@ public class SelectSelectivePluginTest {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试生成的方法
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testSelectiveWithOrWithoutConstructorBased() throws Exception {
|
||||
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectSelectivePlugin/mybatis-generator-with-constructorBased-false.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试sql
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
|
||||
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
|
||||
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
|
||||
criteria.invoke("andIdLessThan", 100l);
|
||||
tbExample.set("orderByClause", "field1 asc");
|
||||
|
||||
ObjectUtil columnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#tsF1");
|
||||
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
|
||||
Object columns1 = Array.newInstance(columnField1.getCls(), 1);
|
||||
Array.set(columns1, 0, columnField1.getObject());
|
||||
|
||||
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(sql, "select field1 from tb WHERE ( id < '100' ) order by field1 asc");
|
||||
|
||||
ObjectUtil columnField2 = new ObjectUtil(loader, packagz + ".Tb$Column#field2");
|
||||
Object columns2 = Array.newInstance(columnField1.getCls(), 2);
|
||||
Array.set(columns2, 0, columnField1.getObject());
|
||||
Array.set(columns2, 1, columnField2.getObject());
|
||||
|
||||
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns2);
|
||||
Assert.assertEquals(sql, "select field1 , field2 from tb WHERE ( id < '100' ) order by field1 asc");
|
||||
|
||||
|
||||
// 2. 执行sql
|
||||
List list = (List) tbMapper.invoke("selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(list.size(), 4);
|
||||
int index = 0;
|
||||
for (Object obj : list) {
|
||||
if (index == 0) {
|
||||
Assert.assertNull(obj);
|
||||
} else {
|
||||
ObjectUtil objectUtil = new ObjectUtil(obj);
|
||||
// 没有查询这两个字段
|
||||
if (objectUtil.get("id") != null || objectUtil.get("field2") != null) {
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
if (index == 1) {
|
||||
Assert.assertEquals(objectUtil.get("tsF1"), "fd1");
|
||||
} else {
|
||||
Assert.assertEquals(objectUtil.get("tsF1"), "fd3");
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tool = MyBatisGeneratorTool.create("scripts/SelectSelectivePlugin/mybatis-generator-with-constructorBased-true.xml");
|
||||
tool.generate(new AbstractShellCallback() {
|
||||
@Override
|
||||
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
|
||||
// 1. 测试sql
|
||||
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
|
||||
|
||||
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
|
||||
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
|
||||
criteria.invoke("andIdLessThan", 100l);
|
||||
tbExample.set("orderByClause", "field1 asc");
|
||||
|
||||
ObjectUtil columnField1 = new ObjectUtil(loader, packagz + ".Tb$Column#tsF1");
|
||||
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
|
||||
Object columns1 = Array.newInstance(columnField1.getCls(), 1);
|
||||
Array.set(columns1, 0, columnField1.getObject());
|
||||
|
||||
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(sql, "select field1 from tb WHERE ( id < '100' ) order by field1 asc");
|
||||
|
||||
ObjectUtil columnField2 = new ObjectUtil(loader, packagz + ".Tb$Column#field2");
|
||||
Object columns2 = Array.newInstance(columnField1.getCls(), 2);
|
||||
Array.set(columns2, 0, columnField1.getObject());
|
||||
Array.set(columns2, 1, columnField2.getObject());
|
||||
|
||||
sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExampleSelective", tbExample.getObject(), columns2);
|
||||
Assert.assertEquals(sql, "select field1 , field2 from tb WHERE ( id < '100' ) order by field1 asc");
|
||||
|
||||
|
||||
// 2. 执行sql
|
||||
List list = (List) tbMapper.invoke("selectByExampleSelective", tbExample.getObject(), columns1);
|
||||
Assert.assertEquals(list.size(), 4);
|
||||
int index = 0;
|
||||
for (Object obj : list) {
|
||||
if (index == 0) {
|
||||
Assert.assertNull(obj);
|
||||
} else {
|
||||
ObjectUtil objectUtil = new ObjectUtil(obj);
|
||||
// 没有查询这两个字段
|
||||
if (objectUtil.get("id") != null || objectUtil.get("field2") != null) {
|
||||
Assert.assertTrue(false);
|
||||
}
|
||||
if (index == 1) {
|
||||
Assert.assertEquals(objectUtil.get("tsF1"), "fd1");
|
||||
} else {
|
||||
Assert.assertEquals(objectUtil.get("tsF1"), "fd3");
|
||||
}
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright (c) 2017.
|
||||
~
|
||||
~ 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 configuration PUBLIC "-//mybatis.org//DTDSQL Map Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
<properties resource="db.properties" />
|
||||
|
||||
<settings>
|
||||
<setting name="callSettersOnNulls" value="true"/>
|
||||
</settings>
|
||||
|
||||
<!-- 配置环境,制定数据库连接信息 -->
|
||||
<environments default="test">
|
||||
<environment id="test">
|
||||
<transactionManager type="JDBC" />
|
||||
<dataSource type="POOLED">
|
||||
<property name="driver" value="${db.driver}"/>
|
||||
<property name="url" value="${db.url}"/>
|
||||
<property name="username" value="${db.username}"/>
|
||||
<property name="password" value="${db.password}"/>
|
||||
</dataSource>
|
||||
</environment>
|
||||
</environments>
|
||||
|
||||
<mappers>
|
||||
<!-- Mapper扫描包,必须同目录同名称下-->
|
||||
<mapper resource="E:\work_java\mybatis-generator-plugin\target\test-tmp\123456\com\itfsw\mybatis\generator\plugins\dao\TbMapper.xml"/>
|
||||
</mappers>
|
||||
</configuration>
|
||||
@@ -32,6 +32,7 @@ CREATE TABLE `tb` (
|
||||
INSERT INTO `tb` VALUES ('1', 'fd1', null);
|
||||
INSERT INTO `tb` VALUES ('2', null, '2');
|
||||
INSERT INTO `tb` VALUES ('3', 'fd3', '3');
|
||||
INSERT INTO `tb` VALUES ('4', 'fd3', '4');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_blobs
|
||||
|
||||
+58
@@ -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">
|
||||
<!-- 插件 -->
|
||||
<!-- Select Selective插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
|
||||
<!-- 数据Model属性对应Column获取插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
|
||||
<!-- 查询单条数据插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
|
||||
|
||||
<!--jdbc的数据库连接 -->
|
||||
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
|
||||
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
|
||||
targetPackage 指定生成的model生成所在的包名
|
||||
targetProject 指定在该项目下所在的路径 -->
|
||||
<javaModelGenerator targetPackage="" targetProject="">
|
||||
<!-- 是否对model添加 构造函数 -->
|
||||
<property name="constructorBased" value="false"/>
|
||||
</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"/>
|
||||
|
||||
<columnOverride column="field1" property="tsF1"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
+58
@@ -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">
|
||||
<!-- 插件 -->
|
||||
<!-- Select Selective插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
|
||||
<!-- 数据Model属性对应Column获取插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
|
||||
<!-- 查询单条数据插件 -->
|
||||
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
|
||||
|
||||
<!--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"/>
|
||||
</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"/>
|
||||
|
||||
<columnOverride column="field1" property="tsF1"/>
|
||||
</table>
|
||||
</context>
|
||||
</generatorConfiguration>
|
||||
Reference in New Issue
Block a user