Skip to content

Commit 2a9860a

Browse files
committed
所有 RuntimeException 异常改为 tk.mybatis.mapper.MapperException 异常
1 parent 146b267 commit 2a9860a

File tree

11 files changed

+41
-30
lines changed

11 files changed

+41
-30
lines changed

src/main/java/tk/mybatis/mapper/entity/Config.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package tk.mybatis.mapper.entity;
2626

27+
import tk.mybatis.mapper.MapperException;
2728
import tk.mybatis.mapper.code.IdentityDialect;
2829
import tk.mybatis.mapper.code.Style;
2930
import tk.mybatis.mapper.util.SimpleTypeUtil;
@@ -298,7 +299,7 @@ public void setProperties(Properties properties) {
298299
try {
299300
this.style = Style.valueOf(styleStr);
300301
} catch (IllegalArgumentException e) {
301-
throw new RuntimeException(styleStr + "不是合法的Style值!");
302+
throw new MapperException(styleStr + "不是合法的Style值!");
302303
}
303304
} else {
304305
//默认驼峰

src/main/java/tk/mybatis/mapper/entity/EntityTable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.ibatis.mapping.ResultMap;
2929
import org.apache.ibatis.mapping.ResultMapping;
3030
import org.apache.ibatis.session.Configuration;
31+
import tk.mybatis.mapper.MapperException;
3132
import tk.mybatis.mapper.util.StringUtil;
3233

3334
import javax.persistence.Table;
@@ -201,7 +202,7 @@ public ResultMap getResultMap(Configuration configuration) {
201202
try {
202203
builder.typeHandler(entityColumn.getTypeHandler().newInstance());
203204
} catch (Exception e) {
204-
throw new RuntimeException(e);
205+
throw new MapperException(e);
205206
}
206207
}
207208
List<ResultFlag> flags = new ArrayList<ResultFlag>();

src/main/java/tk/mybatis/mapper/entity/Example.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.ibatis.reflection.MetaObject;
2828
import org.apache.ibatis.reflection.SystemMetaObject;
2929
import org.apache.ibatis.type.TypeHandler;
30+
import tk.mybatis.mapper.MapperException;
3031
import tk.mybatis.mapper.mapperhelper.EntityHelper;
3132
import tk.mybatis.mapper.util.StringUtil;
3233

@@ -131,7 +132,7 @@ private String property(String property) {
131132
if (propertyMap.containsKey(property)) {
132133
return propertyMap.get(property).getColumn();
133134
} else if (notNull) {
134-
throw new RuntimeException("当前实体类不包含名为" + property + "的属性!");
135+
throw new MapperException("当前实体类不包含名为" + property + "的属性!");
135136
} else {
136137
return null;
137138
}
@@ -277,7 +278,7 @@ private String column(String property) {
277278
if (propertyMap.containsKey(property)) {
278279
return propertyMap.get(property).getColumn();
279280
} else if (exists) {
280-
throw new RuntimeException("当前实体类不包含名为" + property + "的属性!");
281+
throw new MapperException("当前实体类不包含名为" + property + "的属性!");
281282
} else {
282283
return null;
283284
}
@@ -287,7 +288,7 @@ private String property(String property) {
287288
if (propertyMap.containsKey(property)) {
288289
return property;
289290
} else if (exists) {
290-
throw new RuntimeException("当前实体类不包含名为" + property + "的属性!");
291+
throw new MapperException("当前实体类不包含名为" + property + "的属性!");
291292
} else {
292293
return null;
293294
}
@@ -307,7 +308,7 @@ public List<Criterion> getCriteria() {
307308

308309
protected void addCriterion(String condition) {
309310
if (condition == null) {
310-
throw new RuntimeException("Value for condition cannot be null");
311+
throw new MapperException("Value for condition cannot be null");
311312
}
312313
if (condition.startsWith("null")) {
313314
return;
@@ -318,7 +319,7 @@ protected void addCriterion(String condition) {
318319
protected void addCriterion(String condition, Object value, String property) {
319320
if (value == null) {
320321
if (notNull) {
321-
throw new RuntimeException("Value for " + property + " cannot be null");
322+
throw new MapperException("Value for " + property + " cannot be null");
322323
} else {
323324
return;
324325
}
@@ -332,7 +333,7 @@ protected void addCriterion(String condition, Object value, String property) {
332333
protected void addCriterion(String condition, Object value1, Object value2, String property) {
333334
if (value1 == null || value2 == null) {
334335
if (notNull) {
335-
throw new RuntimeException("Between values for " + property + " cannot be null");
336+
throw new MapperException("Between values for " + property + " cannot be null");
336337
} else {
337338
return;
338339
}

src/main/java/tk/mybatis/mapper/generator/MapperPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.mybatis.generator.config.CommentGeneratorConfiguration;
3535
import org.mybatis.generator.config.Context;
3636
import org.mybatis.generator.internal.util.StringUtility;
37+
import tk.mybatis.mapper.MapperException;
3738

3839
import java.util.HashSet;
3940
import java.util.List;
@@ -77,7 +78,7 @@ public void setProperties(Properties properties) {
7778
this.mappers.add(mapper);
7879
}
7980
} else {
80-
throw new RuntimeException("Mapper插件缺少必要的mappers属性!");
81+
throw new MapperException("Mapper插件缺少必要的mappers属性!");
8182
}
8283
String caseSensitive = this.properties.getProperty("caseSensitive");
8384
if (StringUtility.stringHasValue(caseSensitive)) {

src/main/java/tk/mybatis/mapper/mapperhelper/EntityHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.apache.ibatis.type.JdbcType;
2828
import org.apache.ibatis.type.UnknownTypeHandler;
29+
import tk.mybatis.mapper.MapperException;
2930
import tk.mybatis.mapper.annotation.ColumnType;
3031
import tk.mybatis.mapper.annotation.NameStyle;
3132
import tk.mybatis.mapper.code.IdentityDialect;
@@ -63,7 +64,7 @@ public class EntityHelper {
6364
public static EntityTable getEntityTable(Class<?> entityClass) {
6465
EntityTable entityTable = entityTableMap.get(entityClass);
6566
if (entityTable == null) {
66-
throw new RuntimeException("无法获取实体类" + entityClass.getCanonicalName() + "对应的表名!");
67+
throw new MapperException("无法获取实体类" + entityClass.getCanonicalName() + "对应的表名!");
6768
}
6869
return entityTable;
6970
}
@@ -291,7 +292,7 @@ private static void processField(EntityTable entityTable, Style style, EntityFie
291292
if (field.isAnnotationPresent(SequenceGenerator.class)) {
292293
SequenceGenerator sequenceGenerator = field.getAnnotation(SequenceGenerator.class);
293294
if (sequenceGenerator.sequenceName().equals("")) {
294-
throw new RuntimeException(entityTable.getEntityClass() + "字段" + field.getName() + "的注解@SequenceGenerator未指定sequenceName!");
295+
throw new MapperException(entityTable.getEntityClass() + "字段" + field.getName() + "的注解@SequenceGenerator未指定sequenceName!");
295296
}
296297
entityColumn.setSequenceName(sequenceGenerator.sequenceName());
297298
} else if (field.isAnnotationPresent(GeneratedValue.class)) {
@@ -320,7 +321,7 @@ private static void processField(EntityTable entityTable, Style style, EntityFie
320321
entityColumn.setGenerator(generator);
321322
}
322323
} else {
323-
throw new RuntimeException(field.getName()
324+
throw new MapperException(field.getName()
324325
+ " - 该字段@GeneratedValue配置只允许以下几种形式:" +
325326
"\n1.全部数据库通用的@GeneratedValue(generator=\"UUID\")" +
326327
"\n2.useGeneratedKeys的@GeneratedValue(generator=\\\"JDBC\\\") " +

src/main/java/tk/mybatis/mapper/mapperhelper/FieldHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package tk.mybatis.mapper.mapperhelper;
2626

27+
import tk.mybatis.mapper.MapperException;
2728
import tk.mybatis.mapper.entity.EntityField;
2829

2930
import javax.persistence.Entity;
@@ -207,7 +208,7 @@ public List<EntityField> getProperties(Class<?> entityClass) {
207208
try {
208209
beanInfo = Introspector.getBeanInfo(entityClass);
209210
} catch (IntrospectionException e) {
210-
throw new RuntimeException(e);
211+
throw new MapperException(e);
211212
}
212213
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
213214
for (PropertyDescriptor desc : descriptors) {
@@ -244,7 +245,7 @@ public List<EntityField> getProperties(Class<?> entityClass) {
244245
try {
245246
beanInfo = Introspector.getBeanInfo(entityClass);
246247
} catch (IntrospectionException e) {
247-
throw new RuntimeException(e);
248+
throw new MapperException(e);
248249
}
249250
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
250251
for (PropertyDescriptor desc : descriptors) {
@@ -290,7 +291,7 @@ private void _getFields(Class<?> entityClass, List<EntityField> fieldList, Map<S
290291
EntityField entityField = new EntityField(field, null);
291292
if (field.getGenericType() != null && field.getGenericType() instanceof TypeVariable) {
292293
if (genericMap == null || !genericMap.containsKey(((TypeVariable) field.getGenericType()).getName())) {
293-
throw new RuntimeException(entityClass + "字段" + field.getName() + "的泛型类型无法获取!");
294+
throw new MapperException(entityClass + "字段" + field.getName() + "的泛型类型无法获取!");
294295
} else {
295296
entityField.setJavaType(genericMap.get(((TypeVariable) field.getGenericType()).getName()));
296297
}

src/main/java/tk/mybatis/mapper/mapperhelper/MapperHelper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.ibatis.builder.annotation.ProviderSqlSource;
3232
import org.apache.ibatis.mapping.MappedStatement;
3333
import org.apache.ibatis.session.Configuration;
34+
import tk.mybatis.mapper.MapperException;
3435
import tk.mybatis.mapper.entity.Config;
3536
import tk.mybatis.mapper.provider.EmptyProvider;
3637
import tk.mybatis.mapper.util.StringUtil;
@@ -139,7 +140,7 @@ private MapperTemplate fromMapperClass(Class<?> mapperClass) {
139140
if (templateClass == null) {
140141
templateClass = tempClass;
141142
} else if (templateClass != tempClass) {
142-
throw new RuntimeException("一个通用Mapper中只允许存在一个MapperTemplate子类!");
143+
throw new MapperException("一个通用Mapper中只允许存在一个MapperTemplate子类!");
143144
}
144145
}
145146
if (templateClass == null || !MapperTemplate.class.isAssignableFrom(templateClass)) {
@@ -149,14 +150,14 @@ private MapperTemplate fromMapperClass(Class<?> mapperClass) {
149150
try {
150151
mapperTemplate = (MapperTemplate) templateClass.getConstructor(Class.class, MapperHelper.class).newInstance(mapperClass, this);
151152
} catch (Exception e) {
152-
throw new RuntimeException("实例化MapperTemplate对象失败:" + e.getMessage());
153+
throw new MapperException("实例化MapperTemplate对象失败:" + e.getMessage());
153154
}
154155
//注册方法
155156
for (String methodName : methodSet) {
156157
try {
157158
mapperTemplate.addMethodMap(methodName, templateClass.getMethod(methodName, MappedStatement.class));
158159
} catch (NoSuchMethodException e) {
159-
throw new RuntimeException(templateClass.getCanonicalName() + "中缺少" + methodName + "方法!");
160+
throw new MapperException(templateClass.getCanonicalName() + "中缺少" + methodName + "方法!");
160161
}
161162
}
162163
return mapperTemplate;
@@ -190,7 +191,7 @@ public void registerMapper(String mapperClass) {
190191
try {
191192
registerMapper(Class.forName(mapperClass));
192193
} catch (ClassNotFoundException e) {
193-
throw new RuntimeException("注册通用Mapper[" + mapperClass + "]失败,找不到该通用Mapper!");
194+
throw new MapperException("注册通用Mapper[" + mapperClass + "]失败,找不到该通用Mapper!");
194195
}
195196
}
196197

@@ -244,7 +245,7 @@ public void setSqlSource(MappedStatement ms) {
244245
mapperTemplate.setSqlSource(ms);
245246
}
246247
} catch (Exception e) {
247-
throw new RuntimeException(e);
248+
throw new MapperException(e);
248249
}
249250
}
250251

src/main/java/tk/mybatis/mapper/mapperhelper/MapperTemplate.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.ibatis.scripting.defaults.RawSqlSource;
3636
import org.apache.ibatis.scripting.xmltags.*;
3737
import org.apache.ibatis.session.Configuration;
38+
import tk.mybatis.mapper.MapperException;
3839
import tk.mybatis.mapper.entity.EntityColumn;
3940
import tk.mybatis.mapper.entity.EntityTable;
4041
import tk.mybatis.mapper.entity.IDynamicTableName;
@@ -72,7 +73,7 @@ public MapperTemplate(Class<?> mapperClass, MapperHelper mapperHelper) {
7273
*/
7374
public static Class<?> getMapperClass(String msId) {
7475
if (msId.indexOf(".") == -1) {
75-
throw new RuntimeException("当前MappedStatement的id=" + msId + ",不符合MappedStatement的规则!");
76+
throw new MapperException("当前MappedStatement的id=" + msId + ",不符合MappedStatement的规则!");
7677
}
7778
String mapperClassStr = msId.substring(0, msId.lastIndexOf("."));
7879
try {
@@ -218,7 +219,7 @@ private void checkCache(MappedStatement ms) throws Exception {
218219
*/
219220
public void setSqlSource(MappedStatement ms) throws Exception {
220221
if (this.mapperClass == getMapperClass(ms.getId())) {
221-
throw new RuntimeException("请不要配置或扫描通用Mapper接口类:" + this.mapperClass);
222+
throw new MapperException("请不要配置或扫描通用Mapper接口类:" + this.mapperClass);
222223
}
223224
Method method = methodMap.get(getMethodName(ms));
224225
try {
@@ -239,14 +240,14 @@ else if (String.class.equals(method.getReturnType())) {
239240
//替换原有的SqlSource
240241
setSqlSource(ms, sqlSource);
241242
} else {
242-
throw new RuntimeException("自定义Mapper方法返回类型错误,可选的返回类型为void,SqlNode,String三种!");
243+
throw new MapperException("自定义Mapper方法返回类型错误,可选的返回类型为void,SqlNode,String三种!");
243244
}
244245
//cache
245246
checkCache(ms);
246247
} catch (IllegalAccessException e) {
247-
throw new RuntimeException(e);
248+
throw new MapperException(e);
248249
} catch (InvocationTargetException e) {
249-
throw new RuntimeException(e.getTargetException() != null ? e.getTargetException() : e);
250+
throw new MapperException(e.getTargetException() != null ? e.getTargetException() : e);
250251
}
251252
}
252253

@@ -287,7 +288,7 @@ public Class<?> getEntityClass(MappedStatement ms) {
287288
}
288289
}
289290
}
290-
throw new RuntimeException("无法获取Mapper<T>泛型类型:" + msId);
291+
throw new MapperException("无法获取Mapper<T>泛型类型:" + msId);
291292
}
292293

293294
/**

src/main/java/tk/mybatis/mapper/provider/IdsProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tk.mybatis.mapper.provider;
22

33
import org.apache.ibatis.mapping.MappedStatement;
4+
import tk.mybatis.mapper.MapperException;
45
import tk.mybatis.mapper.entity.EntityColumn;
56
import tk.mybatis.mapper.mapperhelper.EntityHelper;
67
import tk.mybatis.mapper.mapperhelper.MapperHelper;
@@ -39,7 +40,7 @@ public String deleteByIds(MappedStatement ms) {
3940
sql.append(column.getColumn());
4041
sql.append(" in (${_parameter})");
4142
} else {
42-
throw new RuntimeException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
43+
throw new MapperException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
4344
}
4445
return sql.toString();
4546
}
@@ -64,7 +65,7 @@ public String selectByIds(MappedStatement ms) {
6465
sql.append(column.getColumn());
6566
sql.append(" in (${_parameter})");
6667
} else {
67-
throw new RuntimeException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
68+
throw new MapperException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
6869
}
6970
return sql.toString();
7071
}

src/main/java/tk/mybatis/mapper/provider/base/BaseInsertProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package tk.mybatis.mapper.provider.base;
2626

2727
import org.apache.ibatis.mapping.MappedStatement;
28+
import tk.mybatis.mapper.MapperException;
2829
import tk.mybatis.mapper.entity.EntityColumn;
2930
import tk.mybatis.mapper.mapperhelper.EntityHelper;
3031
import tk.mybatis.mapper.mapperhelper.MapperHelper;
@@ -88,7 +89,7 @@ public String insert(MappedStatement ms) {
8889
if (column.getGenerator() != null && column.getGenerator().equals("JDBC")) {
8990
continue;
9091
}
91-
throw new RuntimeException(ms.getId() + "对应的实体类" + entityClass.getCanonicalName() + "中包含多个MySql的自动增长列,最多只能有一个!");
92+
throw new MapperException(ms.getId() + "对应的实体类" + entityClass.getCanonicalName() + "中包含多个MySql的自动增长列,最多只能有一个!");
9293
}
9394
//插入selectKey
9495
newSelectKeyMappedStatement(ms, column);
@@ -179,7 +180,7 @@ public String insertSelective(MappedStatement ms) {
179180
if (column.getGenerator() != null && column.getGenerator().equals("JDBC")) {
180181
continue;
181182
}
182-
throw new RuntimeException(ms.getId() + "对应的实体类" + entityClass.getCanonicalName() + "中包含多个MySql的自动增长列,最多只能有一个!");
183+
throw new MapperException(ms.getId() + "对应的实体类" + entityClass.getCanonicalName() + "中包含多个MySql的自动增长列,最多只能有一个!");
183184
}
184185
//插入selectKey
185186
newSelectKeyMappedStatement(ms, column);

wiki/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* 增加参数 `checkExampleEntityClass`,默认 `false` 用于校验通用 Example 构造参数 entityClass 是否和当前调用的 Mapper<EntityClass> 类型一致 #201
88
* 增加参数 `useSimpleType`,默认 `false`,启用后判断实体类属性是否为表字段时校验字段是否为简单类型,如果不是就忽略该属性,这个配置优先级高于所有注解
99
* 增加参数 `simpleTypes`,默认的简单类型在 `SimpleTypeUtil` 中,使用该参数可以增加额外的简单类型,通过逗号隔开的全限定类名添加
10+
* 所有 `RuntimeException` 异常改为 `tk.mybatis.mapper.MapperException` 异常
1011

1112
##3.3.9 - 2016-09-04
1213

0 commit comments

Comments
 (0)