Skip to content

Commit b67c0ba

Browse files
author
hewei
committed
pull#120 增加对老版本兼容,补充测试用例
1 parent 96da83a commit b67c0ba

File tree

5 files changed

+111
-36
lines changed

5 files changed

+111
-36
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,8 +1710,10 @@ public class Test {
17101710
<plugin type="com.itfsw.mybatis.generator.plugins.MapperAnnotationPlugin">
17111711
<!-- @Mapper 默认开启 -->
17121712
<property name="@Mapper" value="true"/>
1713-
<!-- @Repository 默认关闭,开启后解决IDEA工具@Autowired报错 -->
1714-
<property name="@Repository" value="false"/>
1713+
<!-- @Repository 开启后解决IDEA工具@Autowired报错 -->
1714+
<property name="@Repository" value="org.springframework.stereotype.Repository"/>
1715+
<!-- 其他自定义注解 -->
1716+
<property name="@DS(&quot;master&quot;)" value="com.baomidou.dynamic.datasource.annotation.DS"/>
17151717
</plugin>
17161718
</xml>
17171719
```

src/main/java/com/itfsw/mybatis/generator/plugins/MapperAnnotationPlugin.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,53 @@
2121
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
2222
import org.mybatis.generator.api.dom.java.Interface;
2323
import org.mybatis.generator.api.dom.java.TopLevelClass;
24+
import org.mybatis.generator.internal.util.StringUtility;
2425

2526
import java.util.Map;
2627
import java.util.Properties;
2728

2829
/**
2930
* ---------------------------------------------------------------------------
30-
*
31+
* <p>
3132
* ---------------------------------------------------------------------------
3233
*
3334
* @author: hewei
3435
* @time:2019/7/9 14:30
3536
* ---------------------------------------------------------------------------
3637
*/
3738
public class MapperAnnotationPlugin extends BasePlugin {
39+
/**
40+
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
41+
*
42+
* @param interfaze
43+
* @param topLevelClass
44+
* @param introspectedTable
45+
* @return
46+
*/
47+
@Override
48+
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
49+
Properties properties = getProperties();
50+
// 和官方插件一致支持,没有配置特殊注解时默认开启@Mapper
51+
if ("true".equalsIgnoreCase(properties.getProperty("@Mapper", "true")) && introspectedTable.getTargetRuntime() == IntrospectedTable.TargetRuntime.MYBATIS3) {
52+
// don't need to do this for MYBATIS3_DSQL as that runtime already adds this annotation
53+
interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
54+
interfaze.addAnnotation("@Mapper");
55+
}
3856

39-
/**
40-
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
41-
* @param introspectedTable
42-
* @return
43-
*/
44-
@Override
45-
public void initialized(IntrospectedTable introspectedTable) {
46-
super.initialized(introspectedTable);
47-
}
48-
49-
/**
50-
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
51-
* @param interfaze
52-
* @param topLevelClass
53-
* @param introspectedTable
54-
* @return
55-
*/
56-
@Override
57-
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
58-
IntrospectedTable introspectedTable) {
59-
60-
Properties properties = getProperties();
61-
String annotationName;
62-
String annotationImport;
63-
for (Map.Entry<Object, Object> prop : properties.entrySet()) {
64-
annotationName = (String) prop.getKey();
65-
annotationImport = (String) prop.getValue();
66-
interfaze.addImportedType(new FullyQualifiedJavaType(annotationImport));
67-
interfaze.addAnnotation(annotationName);
68-
}
57+
for (Map.Entry<Object, Object> prop : properties.entrySet()) {
58+
String annotationName = prop.getKey().toString().trim();
59+
String annotationImport = prop.getValue().toString().trim();
60+
// TODO 兼容老版本
61+
if ("@Repository".equals(annotationName) && StringUtility.isTrue(annotationImport)) {
62+
interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository"));
63+
interfaze.addAnnotation("@Repository");
64+
} else if (!"@Mapper".equals(annotationName)) {
65+
interfaze.addImportedType(new FullyQualifiedJavaType(annotationImport));
66+
interfaze.addAnnotation(annotationName);
67+
}
68+
}
6969

70-
return true;
71-
}
70+
return true;
71+
}
7272

7373
}

src/test/java/com/itfsw/mybatis/generator/plugins/MapperAnnotationPluginTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,37 @@ public void testDefault() throws Exception{
6565
}
6666
}
6767

68+
/**
69+
* 测试@Mapper false 并且自定义 @Repository
70+
* @throws Exception
71+
*/
72+
@Test
73+
public void testWithMapperFalseAndCustomRepository() throws Exception{
74+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator-with-mapper-false.xml");
75+
MyBatisGenerator myBatisGenerator = tool.generate();
76+
77+
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
78+
CompilationUnit compilationUnit = file.getCompilationUnit();
79+
if (compilationUnit instanceof Interface && compilationUnit.getType().getShortName().endsWith("Mapper")) {
80+
Interface interfaze = (Interface) compilationUnit;
81+
82+
Assert.assertEquals(interfaze.getAnnotations().size(), 2);
83+
Assert.assertEquals(interfaze.getAnnotations().get(0), "@DS(\"master\")");
84+
Assert.assertEquals(interfaze.getAnnotations().get(1), "@Repository");
85+
Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("org.springframework.test.Repository")));
86+
Assert.assertTrue(interfaze.getImportedTypes().contains(new FullyQualifiedJavaType("com.baomidou.dynamic.datasource.annotation.DS")));
87+
}
88+
}
89+
}
90+
91+
6892
/**
6993
* 测试配置Repository
7094
* @throws Exception
7195
*/
7296
@Test
7397
public void testWithRepository() throws Exception{
74-
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator-with-repository.xml");
98+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/MapperAnnotationPlugin/mybatis-generator-with-old-repository.xml");
7599
MyBatisGenerator myBatisGenerator = tool.generate();
76100

77101
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2019.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!DOCTYPE generatorConfiguration
19+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
20+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
21+
<generatorConfiguration>
22+
<properties resource="db.properties"/>
23+
<!--导入属性配置 -->
24+
<context id="default" targetRuntime="MyBatis3">
25+
<!-- 插件 -->
26+
<plugin type="com.itfsw.mybatis.generator.plugins.MapperAnnotationPlugin">
27+
<property name="@Mapper" value="false"/>
28+
<property name="@Repository" value="org.springframework.test.Repository"/>
29+
<property name="@DS(&quot;master&quot;)" value="com.baomidou.dynamic.datasource.annotation.DS"/>
30+
</plugin>
31+
32+
<!--jdbc的数据库连接 -->
33+
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"/>
34+
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
35+
targetPackage 指定生成的model生成所在的包名
36+
targetProject 指定在该项目下所在的路径 -->
37+
<javaModelGenerator targetPackage="" targetProject=""/>
38+
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
39+
<sqlMapGenerator targetPackage="" targetProject=""/>
40+
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
41+
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
42+
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
43+
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
44+
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
45+
46+
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
47+
<table tableName="tb"/>
48+
</context>
49+
</generatorConfiguration>

0 commit comments

Comments
 (0)