added data
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- hf_dataset_features_format.json +3 -0
- test/1273091433/hole_and_PP_contexts.json +3 -0
- test/1273091433/hole_and_bm25_contexts.json +3 -0
- test/1273091433/hole_and_randomNN_contexts.json +3 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/annotation/FieldName.java +17 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/AddGroup.java +10 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/BeanValidators.java +116 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/DefaultGroup.java +9 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/EditGroup.java +9 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/config/Global.java +184 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/filter/GetAndPostEncodingFilter.java +110 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/filter/PageCachingFilter.java +25 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/BeanMapper.java +57 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/JaxbMapper.java +169 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/JsonMapper.java +261 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/adapters/MapAdapter.java +30 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/adapters/MapConvertor.java +63 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/ActEntity.java +57 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/BaseDao.java +13 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/BaseEntity.java +186 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/CrudDao.java +82 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/DataEntity.java +127 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/MapperLoader.java +212 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/Page.java +574 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/Parameter.java +43 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/TreeDao.java +30 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/TreeEntity.java +85 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/annotation/MyBatisDao.java +32 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/Dialect.java +33 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/DB2Dialect.java +89 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/DerbyDialect.java +44 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/H2Dialect.java +45 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/HSQLDialect.java +50 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/MySQLDialect.java +53 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/OracleDialect.java +68 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/PostgreSQLDialect.java +48 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServer2005Dialect.java +95 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServerDialect.java +55 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SybaseDialect.java +47 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/BaseInterceptor.java +110 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/PaginationInterceptor.java +128 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/PreparePaginationInterceptor.java +89 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/SQLHelper.java +195 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PageConfiguration.java +37 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PaginationMapperMethod.java +191 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PaginationMapperProxy.java +104 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PaginationMapperRegistry.java +36 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/security/Cryptos.java +260 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/security/Digests.java +123 -0
- test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/security/shiro/HasAnyPermissionsTag.java +40 -0
hf_dataset_features_format.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e256dd30a7dab39f000bdef2c826f7dab30ed37c0a1afdd8f81a4b2038d48d37
|
3 |
+
size 385
|
test/1273091433/hole_and_PP_contexts.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:676087e34b7099811a4a0ea0ec0ba6c315e48a1768849612e45bdf91395a93e3
|
3 |
+
size 245279386
|
test/1273091433/hole_and_bm25_contexts.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e6ef224f438703e5d7652433290deae66c31f3b1621799cad655dafb10df5d9
|
3 |
+
size 1021162476
|
test/1273091433/hole_and_randomNN_contexts.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ffaddc976c31bce0f8d9a85d9dc9bd718551ee26a93e8a12d66732e1ff932f12
|
3 |
+
size 290503368
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/annotation/FieldName.java
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.annotation;
|
2 |
+
|
3 |
+
import java.lang.annotation.ElementType;
|
4 |
+
import java.lang.annotation.Retention;
|
5 |
+
import java.lang.annotation.RetentionPolicy;
|
6 |
+
import java.lang.annotation.Target;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* bean中文名注解
|
10 |
+
*/
|
11 |
+
@Target(ElementType.METHOD)
|
12 |
+
@Retention(RetentionPolicy.RUNTIME)
|
13 |
+
public @interface FieldName {
|
14 |
+
|
15 |
+
String value();
|
16 |
+
|
17 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/AddGroup.java
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.beanvalidator;
|
2 |
+
|
3 |
+
/**
|
4 |
+
* 添加Bean验证组
|
5 |
+
* @author ThinkGem
|
6 |
+
*
|
7 |
+
*/
|
8 |
+
public interface AddGroup {
|
9 |
+
|
10 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/BeanValidators.java
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright (c) 2005-2012 springside.org.cn
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.beanvalidator;
|
5 |
+
|
6 |
+
import java.util.List;
|
7 |
+
import java.util.Map;
|
8 |
+
import java.util.Set;
|
9 |
+
|
10 |
+
import javax.validation.ConstraintViolation;
|
11 |
+
import javax.validation.ConstraintViolationException;
|
12 |
+
import javax.validation.Validator;
|
13 |
+
|
14 |
+
import com.google.common.collect.Lists;
|
15 |
+
import com.google.common.collect.Maps;
|
16 |
+
|
17 |
+
/**
|
18 |
+
* JSR303 Validator(Hibernate Validator)工具类.
|
19 |
+
*
|
20 |
+
* ConstraintViolation中包含propertyPath, message 和invalidValue等信息.
|
21 |
+
* 提供了各种convert方法,适合不同的i18n需求:
|
22 |
+
* 1. List<String>, String内容为message
|
23 |
+
* 2. List<String>, String内容为propertyPath + separator + message
|
24 |
+
* 3. Map<propertyPath, message>
|
25 |
+
*
|
26 |
+
* 详情见wiki: https://github.com/springside/springside4/wiki/HibernateValidator
|
27 |
+
* @author calvin
|
28 |
+
* @version 2013-01-15
|
29 |
+
*/
|
30 |
+
public class BeanValidators {
|
31 |
+
|
32 |
+
/**
|
33 |
+
* 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException.
|
34 |
+
*/
|
35 |
+
@SuppressWarnings({ "unchecked", "rawtypes" })
|
36 |
+
public static void validateWithException(Validator validator, Object object, Class<?>... groups)
|
37 |
+
throws ConstraintViolationException {
|
38 |
+
Set constraintViolations = validator.validate(object, groups);
|
39 |
+
if (!constraintViolations.isEmpty()) {
|
40 |
+
throw new ConstraintViolationException(constraintViolations);
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
/**
|
45 |
+
* 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>中为List<message>.
|
46 |
+
*/
|
47 |
+
public static List<String> extractMessage(ConstraintViolationException e) {
|
48 |
+
return extractMessage(e.getConstraintViolations());
|
49 |
+
}
|
50 |
+
|
51 |
+
/**
|
52 |
+
* 辅助方法, 转换Set<ConstraintViolation>为List<message>
|
53 |
+
*/
|
54 |
+
@SuppressWarnings("rawtypes")
|
55 |
+
public static List<String> extractMessage(Set<? extends ConstraintViolation> constraintViolations) {
|
56 |
+
List<String> errorMessages = Lists.newArrayList();
|
57 |
+
for (ConstraintViolation violation : constraintViolations) {
|
58 |
+
errorMessages.add(violation.getMessage());
|
59 |
+
}
|
60 |
+
return errorMessages;
|
61 |
+
}
|
62 |
+
|
63 |
+
/**
|
64 |
+
* 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为Map<property, message>.
|
65 |
+
*/
|
66 |
+
public static Map<String, String> extractPropertyAndMessage(ConstraintViolationException e) {
|
67 |
+
return extractPropertyAndMessage(e.getConstraintViolations());
|
68 |
+
}
|
69 |
+
|
70 |
+
/**
|
71 |
+
* 辅助方法, 转换Set<ConstraintViolation>为Map<property, message>.
|
72 |
+
*/
|
73 |
+
@SuppressWarnings("rawtypes")
|
74 |
+
public static Map<String, String> extractPropertyAndMessage(Set<? extends ConstraintViolation> constraintViolations) {
|
75 |
+
Map<String, String> errorMessages = Maps.newHashMap();
|
76 |
+
for (ConstraintViolation violation : constraintViolations) {
|
77 |
+
errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage());
|
78 |
+
}
|
79 |
+
return errorMessages;
|
80 |
+
}
|
81 |
+
|
82 |
+
/**
|
83 |
+
* 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath message>.
|
84 |
+
*/
|
85 |
+
public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e) {
|
86 |
+
return extractPropertyAndMessageAsList(e.getConstraintViolations(), " ");
|
87 |
+
}
|
88 |
+
|
89 |
+
/**
|
90 |
+
* 辅助方法, 转换Set<ConstraintViolations>为List<propertyPath message>.
|
91 |
+
*/
|
92 |
+
@SuppressWarnings("rawtypes")
|
93 |
+
public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations) {
|
94 |
+
return extractPropertyAndMessageAsList(constraintViolations, " ");
|
95 |
+
}
|
96 |
+
|
97 |
+
/**
|
98 |
+
* 辅助方法, 转换ConstraintViolationException中的Set<ConstraintViolations>为List<propertyPath +separator+ message>.
|
99 |
+
*/
|
100 |
+
public static List<String> extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) {
|
101 |
+
return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator);
|
102 |
+
}
|
103 |
+
|
104 |
+
/**
|
105 |
+
* 辅助方法, 转换Set<ConstraintViolation>为List<propertyPath +separator+ message>.
|
106 |
+
*/
|
107 |
+
@SuppressWarnings("rawtypes")
|
108 |
+
public static List<String> extractPropertyAndMessageAsList(Set<? extends ConstraintViolation> constraintViolations,
|
109 |
+
String separator) {
|
110 |
+
List<String> errorMessages = Lists.newArrayList();
|
111 |
+
for (ConstraintViolation violation : constraintViolations) {
|
112 |
+
errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage());
|
113 |
+
}
|
114 |
+
return errorMessages;
|
115 |
+
}
|
116 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/DefaultGroup.java
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.beanvalidator;
|
2 |
+
|
3 |
+
/**
|
4 |
+
* 默认Bean验证组
|
5 |
+
* @author ThinkGem
|
6 |
+
*/
|
7 |
+
public interface DefaultGroup {
|
8 |
+
|
9 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/beanvalidator/EditGroup.java
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.beanvalidator;
|
2 |
+
|
3 |
+
/**
|
4 |
+
* 编辑Bena验证组
|
5 |
+
* @author ThinkGem
|
6 |
+
*/
|
7 |
+
public interface EditGroup {
|
8 |
+
|
9 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/config/Global.java
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.config;
|
5 |
+
|
6 |
+
import java.io.File;
|
7 |
+
import java.io.IOException;
|
8 |
+
import java.util.Map;
|
9 |
+
|
10 |
+
import org.springframework.core.io.DefaultResourceLoader;
|
11 |
+
|
12 |
+
import com.ckfinder.connector.ServletContextFactory;
|
13 |
+
import com.google.common.collect.Maps;
|
14 |
+
import com.thinkgem.jeesite.common.utils.PropertiesLoader;
|
15 |
+
import com.thinkgem.jeesite.common.utils.StringUtils;
|
16 |
+
|
17 |
+
/**
|
18 |
+
* 全局配置类
|
19 |
+
* @author ThinkGem
|
20 |
+
* @version 2014-06-25
|
21 |
+
*/
|
22 |
+
public class Global {
|
23 |
+
|
24 |
+
/**
|
25 |
+
* 当前对象实例
|
26 |
+
*/
|
27 |
+
private static Global global = new Global();
|
28 |
+
|
29 |
+
/**
|
30 |
+
* 保存全局属性值
|
31 |
+
*/
|
32 |
+
private static Map<String, String> map = Maps.newHashMap();
|
33 |
+
|
34 |
+
/**
|
35 |
+
* 属性文件加载对象
|
36 |
+
*/
|
37 |
+
private static PropertiesLoader loader = new PropertiesLoader("jeesite.properties");
|
38 |
+
|
39 |
+
/**
|
40 |
+
* 显示/隐藏
|
41 |
+
*/
|
42 |
+
public static final String SHOW = "1";
|
43 |
+
public static final String HIDE = "0";
|
44 |
+
|
45 |
+
/**
|
46 |
+
* 是/否
|
47 |
+
*/
|
48 |
+
public static final String YES = "1";
|
49 |
+
public static final String NO = "0";
|
50 |
+
|
51 |
+
/**
|
52 |
+
* 对/错
|
53 |
+
*/
|
54 |
+
public static final String TRUE = "true";
|
55 |
+
public static final String FALSE = "false";
|
56 |
+
|
57 |
+
/**
|
58 |
+
* 上传文件基础虚拟路径
|
59 |
+
*/
|
60 |
+
public static final String USERFILES_BASE_URL = "/userfiles/";
|
61 |
+
|
62 |
+
/**
|
63 |
+
* 获取当前对象实例
|
64 |
+
*/
|
65 |
+
public static Global getInstance() {
|
66 |
+
return global;
|
67 |
+
}
|
68 |
+
|
69 |
+
/**
|
70 |
+
* 获取配置
|
71 |
+
* @see ${fns:getConfig('adminPath')}
|
72 |
+
*/
|
73 |
+
public static String getConfig(String key) {
|
74 |
+
String value = map.get(key);
|
75 |
+
if (value == null){
|
76 |
+
value = loader.getProperty(key);
|
77 |
+
map.put(key, value != null ? value : StringUtils.EMPTY);
|
78 |
+
}
|
79 |
+
return value;
|
80 |
+
}
|
81 |
+
|
82 |
+
/**
|
83 |
+
* 获取管理端根路径
|
84 |
+
*/
|
85 |
+
public static String getAdminPath() {
|
86 |
+
return getConfig("adminPath");
|
87 |
+
}
|
88 |
+
|
89 |
+
/**
|
90 |
+
* 获取前端根路径
|
91 |
+
*/
|
92 |
+
public static String getFrontPath() {
|
93 |
+
return getConfig("frontPath");
|
94 |
+
}
|
95 |
+
|
96 |
+
/**
|
97 |
+
* 获取URL后缀
|
98 |
+
*/
|
99 |
+
public static String getUrlSuffix() {
|
100 |
+
return getConfig("urlSuffix");
|
101 |
+
}
|
102 |
+
|
103 |
+
/**
|
104 |
+
* 是否是演示模式,演示模式下不能修改用户、角色、密码、菜单、授权
|
105 |
+
*/
|
106 |
+
public static Boolean isDemoMode() {
|
107 |
+
String dm = getConfig("demoMode");
|
108 |
+
return "true".equals(dm) || "1".equals(dm);
|
109 |
+
}
|
110 |
+
|
111 |
+
/**
|
112 |
+
* 在修改系统用户和角色时是否同步到Activiti
|
113 |
+
*/
|
114 |
+
public static Boolean isSynActivitiIndetity() {
|
115 |
+
String dm = getConfig("activiti.isSynActivitiIndetity");
|
116 |
+
return "true".equals(dm) || "1".equals(dm);
|
117 |
+
}
|
118 |
+
|
119 |
+
/**
|
120 |
+
* 页面获取常量
|
121 |
+
* @see ${fns:getConst('YES')}
|
122 |
+
*/
|
123 |
+
public static Object getConst(String field) {
|
124 |
+
try {
|
125 |
+
return Global.class.getField(field).get(null);
|
126 |
+
} catch (Exception e) {
|
127 |
+
// 异常代表无配置,这里什么也不做
|
128 |
+
}
|
129 |
+
return null;
|
130 |
+
}
|
131 |
+
|
132 |
+
/**
|
133 |
+
* 获取上传文件的根目录
|
134 |
+
* @return
|
135 |
+
*/
|
136 |
+
public static String getUserfilesBaseDir() {
|
137 |
+
String dir = getConfig("userfiles.basedir");
|
138 |
+
if (StringUtils.isBlank(dir)){
|
139 |
+
try {
|
140 |
+
dir = ServletContextFactory.getServletContext().getRealPath("/");
|
141 |
+
} catch (Exception e) {
|
142 |
+
return "";
|
143 |
+
}
|
144 |
+
}
|
145 |
+
if(!dir.endsWith("/")) {
|
146 |
+
dir += "/";
|
147 |
+
}
|
148 |
+
// System.out.println("userfiles.basedir: " + dir);
|
149 |
+
return dir;
|
150 |
+
}
|
151 |
+
|
152 |
+
/**
|
153 |
+
* 获取工程路径
|
154 |
+
* @return
|
155 |
+
*/
|
156 |
+
public static String getProjectPath(){
|
157 |
+
// 如果配置了工程路径,则直接返回,否则自动获取。
|
158 |
+
String projectPath = Global.getConfig("projectPath");
|
159 |
+
if (StringUtils.isNotBlank(projectPath)){
|
160 |
+
return projectPath;
|
161 |
+
}
|
162 |
+
try {
|
163 |
+
File file = new DefaultResourceLoader().getResource("").getFile();
|
164 |
+
if (file != null){
|
165 |
+
while(true){
|
166 |
+
File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
|
167 |
+
if (f == null || f.exists()){
|
168 |
+
break;
|
169 |
+
}
|
170 |
+
if (file.getParentFile() != null){
|
171 |
+
file = file.getParentFile();
|
172 |
+
}else{
|
173 |
+
break;
|
174 |
+
}
|
175 |
+
}
|
176 |
+
projectPath = file.toString();
|
177 |
+
}
|
178 |
+
} catch (IOException e) {
|
179 |
+
e.printStackTrace();
|
180 |
+
}
|
181 |
+
return projectPath;
|
182 |
+
}
|
183 |
+
|
184 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/filter/GetAndPostEncodingFilter.java
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.filter;
|
2 |
+
|
3 |
+
import java.io.IOException;
|
4 |
+
import java.io.UnsupportedEncodingException;
|
5 |
+
import java.util.Map;
|
6 |
+
|
7 |
+
import javax.servlet.Filter;
|
8 |
+
import javax.servlet.FilterChain;
|
9 |
+
import javax.servlet.FilterConfig;
|
10 |
+
import javax.servlet.ServletException;
|
11 |
+
import javax.servlet.ServletRequest;
|
12 |
+
import javax.servlet.ServletResponse;
|
13 |
+
import javax.servlet.http.HttpServletRequest;
|
14 |
+
import javax.servlet.http.HttpServletRequestWrapper;
|
15 |
+
/**
|
16 |
+
* 解决页面post请求或者是get请求的乱码问题
|
17 |
+
* @author admin
|
18 |
+
*
|
19 |
+
*/
|
20 |
+
public class GetAndPostEncodingFilter implements Filter {
|
21 |
+
|
22 |
+
@Override
|
23 |
+
public void init(FilterConfig filterConfig) throws ServletException {
|
24 |
+
// TODO Auto-generated method stub
|
25 |
+
|
26 |
+
}
|
27 |
+
|
28 |
+
@Override
|
29 |
+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
30 |
+
throws IOException, ServletException {
|
31 |
+
//处理请求乱码
|
32 |
+
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
33 |
+
HttpServletRequest myRequest = new MyRequest(httpServletRequest);
|
34 |
+
|
35 |
+
//处理响应乱码
|
36 |
+
response.setContentType("text/html;charset=utf-8");
|
37 |
+
chain.doFilter(myRequest, response);
|
38 |
+
}
|
39 |
+
|
40 |
+
@Override
|
41 |
+
public void destroy() {
|
42 |
+
// TODO Auto-generated method stub
|
43 |
+
|
44 |
+
}
|
45 |
+
|
46 |
+
}
|
47 |
+
|
48 |
+
//自定义request对象
|
49 |
+
class MyRequest extends HttpServletRequestWrapper{
|
50 |
+
|
51 |
+
private HttpServletRequest request;
|
52 |
+
private boolean hasEncode;
|
53 |
+
|
54 |
+
public MyRequest(HttpServletRequest request) {
|
55 |
+
super(request);
|
56 |
+
this.request=request;
|
57 |
+
}
|
58 |
+
|
59 |
+
//对需要增强的方法 进行覆盖
|
60 |
+
@Override
|
61 |
+
@SuppressWarnings("unchecked")
|
62 |
+
public Map<String,String[]> getParameterMap() {
|
63 |
+
//先获得请求方式
|
64 |
+
String method = request.getMethod();
|
65 |
+
if(method.equalsIgnoreCase("post")){ //post方式请求
|
66 |
+
try {
|
67 |
+
request.setCharacterEncoding("utf-8");
|
68 |
+
return request.getParameterMap();
|
69 |
+
} catch (UnsupportedEncodingException e) {
|
70 |
+
e.printStackTrace();
|
71 |
+
}
|
72 |
+
}else if(method.equalsIgnoreCase("get")){ //get方式请求
|
73 |
+
Map<String,String[]> parameterMap = request.getParameterMap();
|
74 |
+
if(!hasEncode){
|
75 |
+
for(String parameterName:parameterMap.keySet()){
|
76 |
+
String[] values = parameterMap.get(parameterName);
|
77 |
+
if(values!=null){
|
78 |
+
for (int i = 0; i < values.length; i++) {
|
79 |
+
try {
|
80 |
+
values[i] = new String(values[i].getBytes("ISO-8859-1"),"utf-8");
|
81 |
+
} catch (UnsupportedEncodingException e) {
|
82 |
+
e.printStackTrace();
|
83 |
+
}
|
84 |
+
}
|
85 |
+
}
|
86 |
+
}
|
87 |
+
hasEncode = true;
|
88 |
+
}
|
89 |
+
return parameterMap;
|
90 |
+
}
|
91 |
+
return super.getParameterMap();
|
92 |
+
}
|
93 |
+
|
94 |
+
@Override
|
95 |
+
public String getParameter(String name) {
|
96 |
+
Map<String,String[]> parameterMap = getParameterMap();
|
97 |
+
String[] values = parameterMap.get(name);
|
98 |
+
if(values==null){
|
99 |
+
return null;
|
100 |
+
}
|
101 |
+
return values[0];
|
102 |
+
}
|
103 |
+
|
104 |
+
@Override
|
105 |
+
public String[] getParameterValues(String name) {
|
106 |
+
Map<String,String[]> parameterMap = getParameterMap();
|
107 |
+
String[] values=parameterMap.get(name);
|
108 |
+
return values;
|
109 |
+
}
|
110 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/filter/PageCachingFilter.java
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.filter;
|
5 |
+
|
6 |
+
import net.sf.ehcache.CacheManager;
|
7 |
+
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;
|
8 |
+
|
9 |
+
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
|
10 |
+
|
11 |
+
/**
|
12 |
+
* 页面高速缓存过滤器
|
13 |
+
* @author ThinkGem
|
14 |
+
* @version 2013-8-5
|
15 |
+
*/
|
16 |
+
public class PageCachingFilter extends SimplePageCachingFilter {
|
17 |
+
|
18 |
+
private CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);
|
19 |
+
|
20 |
+
@Override
|
21 |
+
protected CacheManager getCacheManager() {
|
22 |
+
return cacheManager;
|
23 |
+
}
|
24 |
+
|
25 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/BeanMapper.java
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright (c) 2005-2012 springside.org.cn
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.mapper;
|
5 |
+
|
6 |
+
import java.util.Collection;
|
7 |
+
import java.util.List;
|
8 |
+
|
9 |
+
import org.dozer.DozerBeanMapper;
|
10 |
+
|
11 |
+
import com.google.common.collect.Lists;
|
12 |
+
|
13 |
+
/**
|
14 |
+
* 简单封装Dozer, 实现深度转换Bean<->Bean的Mapper.实现:
|
15 |
+
*
|
16 |
+
* 1. 持有Mapper的单例.
|
17 |
+
* 2. 返回值类型转换.
|
18 |
+
* 3. 批量转换Collection中的所有对象.
|
19 |
+
* 4. 区分创建新的B对象与将对象A值复制到已存在的B对象两种函数.
|
20 |
+
*
|
21 |
+
* @author calvin
|
22 |
+
* @version 2013-01-15
|
23 |
+
*/
|
24 |
+
public class BeanMapper {
|
25 |
+
|
26 |
+
/**
|
27 |
+
* 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
|
28 |
+
*/
|
29 |
+
private static DozerBeanMapper dozer = new DozerBeanMapper();
|
30 |
+
|
31 |
+
/**
|
32 |
+
* 基于Dozer转换对象的类型.
|
33 |
+
*/
|
34 |
+
public static <T> T map(Object source, Class<T> destinationClass) {
|
35 |
+
return dozer.map(source, destinationClass);
|
36 |
+
}
|
37 |
+
|
38 |
+
/**
|
39 |
+
* 基于Dozer转换Collection中对象的类型.
|
40 |
+
*/
|
41 |
+
@SuppressWarnings("rawtypes")
|
42 |
+
public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
|
43 |
+
List<T> destinationList = Lists.newArrayList();
|
44 |
+
for (Object sourceObject : sourceList) {
|
45 |
+
T destinationObject = dozer.map(sourceObject, destinationClass);
|
46 |
+
destinationList.add(destinationObject);
|
47 |
+
}
|
48 |
+
return destinationList;
|
49 |
+
}
|
50 |
+
|
51 |
+
/**
|
52 |
+
* 基于Dozer将对象A的值拷贝到对象B中.
|
53 |
+
*/
|
54 |
+
public static void copy(Object source, Object destinationObject) {
|
55 |
+
dozer.map(source, destinationObject);
|
56 |
+
}
|
57 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/JaxbMapper.java
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright (c) 2005-2012 springside.org.cn
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.mapper;
|
5 |
+
|
6 |
+
import java.io.StringReader;
|
7 |
+
import java.io.StringWriter;
|
8 |
+
import java.util.Collection;
|
9 |
+
import java.util.concurrent.ConcurrentHashMap;
|
10 |
+
import java.util.concurrent.ConcurrentMap;
|
11 |
+
|
12 |
+
import javax.xml.bind.JAXBContext;
|
13 |
+
import javax.xml.bind.JAXBElement;
|
14 |
+
import javax.xml.bind.JAXBException;
|
15 |
+
import javax.xml.bind.Marshaller;
|
16 |
+
import javax.xml.bind.Unmarshaller;
|
17 |
+
import javax.xml.bind.annotation.XmlAnyElement;
|
18 |
+
import javax.xml.namespace.QName;
|
19 |
+
|
20 |
+
import org.springframework.http.converter.HttpMessageConversionException;
|
21 |
+
import org.springframework.util.Assert;
|
22 |
+
|
23 |
+
import com.thinkgem.jeesite.common.utils.Exceptions;
|
24 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
25 |
+
import com.thinkgem.jeesite.common.utils.StringUtils;
|
26 |
+
|
27 |
+
/**
|
28 |
+
* 使用Jaxb2.0实现XML<->Java Object的Mapper.
|
29 |
+
*
|
30 |
+
* 在创建时需要设定所有需要序列化的Root对象的Class.
|
31 |
+
* 特别支持Root对象是Collection的情形.
|
32 |
+
*
|
33 |
+
* @author calvin
|
34 |
+
* @version 2013-01-15
|
35 |
+
*/
|
36 |
+
@SuppressWarnings("rawtypes")
|
37 |
+
public class JaxbMapper {
|
38 |
+
|
39 |
+
private static ConcurrentMap<Class, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class, JAXBContext>();
|
40 |
+
|
41 |
+
/**
|
42 |
+
* Java Object->Xml without encoding.
|
43 |
+
*/
|
44 |
+
public static String toXml(Object root) {
|
45 |
+
Class clazz = Reflections.getUserClass(root);
|
46 |
+
return toXml(root, clazz, null);
|
47 |
+
}
|
48 |
+
|
49 |
+
/**
|
50 |
+
* Java Object->Xml with encoding.
|
51 |
+
*/
|
52 |
+
public static String toXml(Object root, String encoding) {
|
53 |
+
Class clazz = Reflections.getUserClass(root);
|
54 |
+
return toXml(root, clazz, encoding);
|
55 |
+
}
|
56 |
+
|
57 |
+
/**
|
58 |
+
* Java Object->Xml with encoding.
|
59 |
+
*/
|
60 |
+
public static String toXml(Object root, Class clazz, String encoding) {
|
61 |
+
try {
|
62 |
+
StringWriter writer = new StringWriter();
|
63 |
+
createMarshaller(clazz, encoding).marshal(root, writer);
|
64 |
+
return writer.toString();
|
65 |
+
} catch (JAXBException e) {
|
66 |
+
throw Exceptions.unchecked(e);
|
67 |
+
}
|
68 |
+
}
|
69 |
+
|
70 |
+
/**
|
71 |
+
* Java Collection->Xml without encoding, 特别支持Root Element是Collection的情形.
|
72 |
+
*/
|
73 |
+
public static String toXml(Collection<?> root, String rootName, Class clazz) {
|
74 |
+
return toXml(root, rootName, clazz, null);
|
75 |
+
}
|
76 |
+
|
77 |
+
/**
|
78 |
+
* Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形.
|
79 |
+
*/
|
80 |
+
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
|
81 |
+
try {
|
82 |
+
CollectionWrapper wrapper = new CollectionWrapper();
|
83 |
+
wrapper.collection = root;
|
84 |
+
|
85 |
+
JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
|
86 |
+
CollectionWrapper.class, wrapper);
|
87 |
+
|
88 |
+
StringWriter writer = new StringWriter();
|
89 |
+
createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
|
90 |
+
|
91 |
+
return writer.toString();
|
92 |
+
} catch (JAXBException e) {
|
93 |
+
throw Exceptions.unchecked(e);
|
94 |
+
}
|
95 |
+
}
|
96 |
+
|
97 |
+
/**
|
98 |
+
* Xml->Java Object.
|
99 |
+
*/
|
100 |
+
@SuppressWarnings("unchecked")
|
101 |
+
public static <T> T fromXml(String xml, Class<T> clazz) {
|
102 |
+
try {
|
103 |
+
StringReader reader = new StringReader(xml);
|
104 |
+
return (T) createUnmarshaller(clazz).unmarshal(reader);
|
105 |
+
} catch (JAXBException e) {
|
106 |
+
throw Exceptions.unchecked(e);
|
107 |
+
}
|
108 |
+
}
|
109 |
+
|
110 |
+
/**
|
111 |
+
* 创建Marshaller并设定encoding(可为null).
|
112 |
+
* 线程不安全,需要每次创建或pooling。
|
113 |
+
*/
|
114 |
+
public static Marshaller createMarshaller(Class clazz, String encoding) {
|
115 |
+
try {
|
116 |
+
JAXBContext jaxbContext = getJaxbContext(clazz);
|
117 |
+
|
118 |
+
Marshaller marshaller = jaxbContext.createMarshaller();
|
119 |
+
|
120 |
+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
121 |
+
|
122 |
+
if (StringUtils.isNotBlank(encoding)) {
|
123 |
+
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
|
124 |
+
}
|
125 |
+
|
126 |
+
return marshaller;
|
127 |
+
} catch (JAXBException e) {
|
128 |
+
throw Exceptions.unchecked(e);
|
129 |
+
}
|
130 |
+
}
|
131 |
+
|
132 |
+
/**
|
133 |
+
* 创建UnMarshaller.
|
134 |
+
* 线程不安全,需要每次创建或pooling。
|
135 |
+
*/
|
136 |
+
public static Unmarshaller createUnmarshaller(Class clazz) {
|
137 |
+
try {
|
138 |
+
JAXBContext jaxbContext = getJaxbContext(clazz);
|
139 |
+
return jaxbContext.createUnmarshaller();
|
140 |
+
} catch (JAXBException e) {
|
141 |
+
throw Exceptions.unchecked(e);
|
142 |
+
}
|
143 |
+
}
|
144 |
+
|
145 |
+
protected static JAXBContext getJaxbContext(Class clazz) {
|
146 |
+
Assert.notNull(clazz, "'clazz' must not be null");
|
147 |
+
JAXBContext jaxbContext = jaxbContexts.get(clazz);
|
148 |
+
if (jaxbContext == null) {
|
149 |
+
try {
|
150 |
+
jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
|
151 |
+
jaxbContexts.putIfAbsent(clazz, jaxbContext);
|
152 |
+
} catch (JAXBException ex) {
|
153 |
+
throw new HttpMessageConversionException("Could not instantiate JAXBContext for class [" + clazz
|
154 |
+
+ "]: " + ex.getMessage(), ex);
|
155 |
+
}
|
156 |
+
}
|
157 |
+
return jaxbContext;
|
158 |
+
}
|
159 |
+
|
160 |
+
/**
|
161 |
+
* 封装Root Element 是 Collection的情况.
|
162 |
+
*/
|
163 |
+
public static class CollectionWrapper {
|
164 |
+
|
165 |
+
@XmlAnyElement
|
166 |
+
protected Collection<?> collection;
|
167 |
+
}
|
168 |
+
|
169 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/JsonMapper.java
ADDED
@@ -0,0 +1,261 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.mapper;
|
5 |
+
|
6 |
+
import java.io.IOException;
|
7 |
+
import java.util.List;
|
8 |
+
import java.util.Map;
|
9 |
+
import java.util.TimeZone;
|
10 |
+
|
11 |
+
import org.apache.commons.lang3.StringEscapeUtils;
|
12 |
+
import org.apache.commons.lang3.StringUtils;
|
13 |
+
import org.slf4j.Logger;
|
14 |
+
import org.slf4j.LoggerFactory;
|
15 |
+
|
16 |
+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
17 |
+
import com.fasterxml.jackson.core.JsonGenerator;
|
18 |
+
import com.fasterxml.jackson.core.JsonProcessingException;
|
19 |
+
import com.fasterxml.jackson.core.JsonParser.Feature;
|
20 |
+
import com.fasterxml.jackson.databind.DeserializationFeature;
|
21 |
+
import com.fasterxml.jackson.databind.JavaType;
|
22 |
+
import com.fasterxml.jackson.databind.JsonSerializer;
|
23 |
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
24 |
+
import com.fasterxml.jackson.databind.SerializationFeature;
|
25 |
+
import com.fasterxml.jackson.databind.SerializerProvider;
|
26 |
+
import com.fasterxml.jackson.databind.module.SimpleModule;
|
27 |
+
import com.fasterxml.jackson.databind.util.JSONPObject;
|
28 |
+
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
|
29 |
+
import com.google.common.collect.Lists;
|
30 |
+
import com.google.common.collect.Maps;
|
31 |
+
|
32 |
+
/**
|
33 |
+
* 简单封装Jackson,实现JSON String<->Java Object的Mapper.
|
34 |
+
* 封装不同的输出风格, 使用不同的builder函数创建实例.
|
35 |
+
* @author ThinkGem
|
36 |
+
* @version 2013-11-15
|
37 |
+
*/
|
38 |
+
public class JsonMapper extends ObjectMapper {
|
39 |
+
|
40 |
+
private static final long serialVersionUID = 1L;
|
41 |
+
|
42 |
+
private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
|
43 |
+
|
44 |
+
private static JsonMapper mapper;
|
45 |
+
|
46 |
+
public JsonMapper() {
|
47 |
+
this(Include.NON_EMPTY);
|
48 |
+
}
|
49 |
+
|
50 |
+
public JsonMapper(Include include) {
|
51 |
+
// 设置输出时包含属性的风格
|
52 |
+
if (include != null) {
|
53 |
+
this.setSerializationInclusion(include);
|
54 |
+
}
|
55 |
+
// 允许单引号、允许不带引号的字段名称
|
56 |
+
this.enableSimple();
|
57 |
+
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
|
58 |
+
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
59 |
+
// 空值处理为空串
|
60 |
+
this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){
|
61 |
+
@Override
|
62 |
+
public void serialize(Object value, JsonGenerator jgen,
|
63 |
+
SerializerProvider provider) throws IOException,
|
64 |
+
JsonProcessingException {
|
65 |
+
jgen.writeString("");
|
66 |
+
}
|
67 |
+
});
|
68 |
+
// 进行HTML解码。
|
69 |
+
this.registerModule(new SimpleModule().addSerializer(String.class, new JsonSerializer<String>(){
|
70 |
+
@Override
|
71 |
+
public void serialize(String value, JsonGenerator jgen,
|
72 |
+
SerializerProvider provider) throws IOException,
|
73 |
+
JsonProcessingException {
|
74 |
+
jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
|
75 |
+
}
|
76 |
+
}));
|
77 |
+
// 设置时区
|
78 |
+
this.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT+8:00")
|
79 |
+
}
|
80 |
+
|
81 |
+
/**
|
82 |
+
* 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper,建议在外部接口中使用.
|
83 |
+
*/
|
84 |
+
public static JsonMapper getInstance() {
|
85 |
+
if (mapper == null){
|
86 |
+
mapper = new JsonMapper().enableSimple();
|
87 |
+
}
|
88 |
+
return mapper;
|
89 |
+
}
|
90 |
+
|
91 |
+
/**
|
92 |
+
* 创建只输出初始值被改变的属性到Json字符串的Mapper, 最节约的存储方式,建议在内部接口中使用。
|
93 |
+
*/
|
94 |
+
public static JsonMapper nonDefaultMapper() {
|
95 |
+
if (mapper == null){
|
96 |
+
mapper = new JsonMapper(Include.NON_DEFAULT);
|
97 |
+
}
|
98 |
+
return mapper;
|
99 |
+
}
|
100 |
+
|
101 |
+
/**
|
102 |
+
* Object可以是POJO,也可以是Collection或数组。
|
103 |
+
* 如果对象为Null, 返回"null".
|
104 |
+
* 如果集合为空集合, 返回"[]".
|
105 |
+
*/
|
106 |
+
public String toJson(Object object) {
|
107 |
+
try {
|
108 |
+
return this.writeValueAsString(object);
|
109 |
+
} catch (IOException e) {
|
110 |
+
logger.warn("write to json string error:" + object, e);
|
111 |
+
return null;
|
112 |
+
}
|
113 |
+
}
|
114 |
+
|
115 |
+
/**
|
116 |
+
* 反序列化POJO或简单Collection如List<String>.
|
117 |
+
*
|
118 |
+
* 如果JSON字符串为Null或"null"字符串, 返回Null.
|
119 |
+
* 如果JSON字符串为"[]", 返回空集合.
|
120 |
+
*
|
121 |
+
* 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String,JavaType)
|
122 |
+
* @see #fromJson(String, JavaType)
|
123 |
+
*/
|
124 |
+
public <T> T fromJson(String jsonString, Class<T> clazz) {
|
125 |
+
if (StringUtils.isEmpty(jsonString)) {
|
126 |
+
return null;
|
127 |
+
}
|
128 |
+
try {
|
129 |
+
return this.readValue(jsonString, clazz);
|
130 |
+
} catch (IOException e) {
|
131 |
+
logger.warn("parse json string error:" + jsonString, e);
|
132 |
+
return null;
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
/**
|
137 |
+
* 反序列化复杂Collection如List<Bean>, 先使用函數createCollectionType构造类型,然后调用本函数.
|
138 |
+
* @see #createCollectionType(Class, Class...)
|
139 |
+
*/
|
140 |
+
@SuppressWarnings("unchecked")
|
141 |
+
public <T> T fromJson(String jsonString, JavaType javaType) {
|
142 |
+
if (StringUtils.isEmpty(jsonString)) {
|
143 |
+
return null;
|
144 |
+
}
|
145 |
+
try {
|
146 |
+
return (T) this.readValue(jsonString, javaType);
|
147 |
+
} catch (IOException e) {
|
148 |
+
logger.warn("parse json string error:" + jsonString, e);
|
149 |
+
return null;
|
150 |
+
}
|
151 |
+
}
|
152 |
+
|
153 |
+
/**
|
154 |
+
* 構造泛型的Collection Type如:
|
155 |
+
* ArrayList<MyBean>, 则调用constructCollectionType(ArrayList.class,MyBean.class)
|
156 |
+
* HashMap<String,MyBean>, 则调用(HashMap.class,String.class, MyBean.class)
|
157 |
+
*/
|
158 |
+
public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
|
159 |
+
return this.getTypeFactory().constructParametricType(collectionClass, elementClasses);
|
160 |
+
}
|
161 |
+
|
162 |
+
/**
|
163 |
+
* 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
|
164 |
+
*/
|
165 |
+
@SuppressWarnings("unchecked")
|
166 |
+
public <T> T update(String jsonString, T object) {
|
167 |
+
try {
|
168 |
+
return (T) this.readerForUpdating(object).readValue(jsonString);
|
169 |
+
} catch (JsonProcessingException e) {
|
170 |
+
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
|
171 |
+
} catch (IOException e) {
|
172 |
+
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
|
173 |
+
}
|
174 |
+
return null;
|
175 |
+
}
|
176 |
+
|
177 |
+
/**
|
178 |
+
* 輸出JSONP格式數據.
|
179 |
+
*/
|
180 |
+
public String toJsonP(String functionName, Object object) {
|
181 |
+
return toJson(new JSONPObject(functionName, object));
|
182 |
+
}
|
183 |
+
|
184 |
+
/**
|
185 |
+
* 設定是否使用Enum的toString函數來讀寫Enum,
|
186 |
+
* 為False時時使用Enum的name()函數來讀寫Enum, 默認為False.
|
187 |
+
* 注意本函數一定要在Mapper創建後, 所有的讀寫動作之前調用.
|
188 |
+
*/
|
189 |
+
public JsonMapper enableEnumUseToString() {
|
190 |
+
this.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
|
191 |
+
this.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
|
192 |
+
return this;
|
193 |
+
}
|
194 |
+
|
195 |
+
/**
|
196 |
+
* 支持使用Jaxb的Annotation,使得POJO上的annotation不用与Jackson耦合。
|
197 |
+
* 默认会先查找jaxb的annotation,如果找不到再找jackson的。
|
198 |
+
*/
|
199 |
+
public JsonMapper enableJaxbAnnotation() {
|
200 |
+
JaxbAnnotationModule module = new JaxbAnnotationModule();
|
201 |
+
this.registerModule(module);
|
202 |
+
return this;
|
203 |
+
}
|
204 |
+
|
205 |
+
/**
|
206 |
+
* 允许单引号
|
207 |
+
* 允许不带引号的字段名称
|
208 |
+
*/
|
209 |
+
public JsonMapper enableSimple() {
|
210 |
+
this.configure(Feature.ALLOW_SINGLE_QUOTES, true);
|
211 |
+
this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
212 |
+
return this;
|
213 |
+
}
|
214 |
+
|
215 |
+
/**
|
216 |
+
* 取出Mapper做进一步的设置或使用其他序列化API.
|
217 |
+
*/
|
218 |
+
public ObjectMapper getMapper() {
|
219 |
+
return this;
|
220 |
+
}
|
221 |
+
|
222 |
+
/**
|
223 |
+
* 对象转换为JSON字符串
|
224 |
+
* @param object
|
225 |
+
* @return
|
226 |
+
*/
|
227 |
+
public static String toJsonString(Object object){
|
228 |
+
return JsonMapper.getInstance().toJson(object);
|
229 |
+
}
|
230 |
+
|
231 |
+
/**
|
232 |
+
* JSON字符串转换为对象
|
233 |
+
* @param jsonString
|
234 |
+
* @param clazz
|
235 |
+
* @return
|
236 |
+
*/
|
237 |
+
public static Object fromJsonString(String jsonString, Class<?> clazz){
|
238 |
+
return JsonMapper.getInstance().fromJson(jsonString, clazz);
|
239 |
+
}
|
240 |
+
|
241 |
+
/**
|
242 |
+
* 测试
|
243 |
+
*/
|
244 |
+
public static void main(String[] args) {
|
245 |
+
List<Map<String, Object>> list = Lists.newArrayList();
|
246 |
+
Map<String, Object> map = Maps.newHashMap();
|
247 |
+
map.put("id", 1);
|
248 |
+
map.put("pId", -1);
|
249 |
+
map.put("name", "根节点");
|
250 |
+
list.add(map);
|
251 |
+
map = Maps.newHashMap();
|
252 |
+
map.put("id", 2);
|
253 |
+
map.put("pId", 1);
|
254 |
+
map.put("name", "你好");
|
255 |
+
map.put("open", true);
|
256 |
+
list.add(map);
|
257 |
+
String json = JsonMapper.getInstance().toJson(list);
|
258 |
+
System.out.println(json);
|
259 |
+
}
|
260 |
+
|
261 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/adapters/MapAdapter.java
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.mapper.adapters;
|
2 |
+
|
3 |
+
import java.util.HashMap;
|
4 |
+
import java.util.Map;
|
5 |
+
|
6 |
+
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
7 |
+
|
8 |
+
public class MapAdapter extends XmlAdapter<MapConvertor, Map<String, Object>> {
|
9 |
+
|
10 |
+
@Override
|
11 |
+
public MapConvertor marshal(Map<String, Object> map) throws Exception {
|
12 |
+
MapConvertor convertor = new MapConvertor();
|
13 |
+
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
14 |
+
MapConvertor.MapEntry e = new MapConvertor.MapEntry(entry);
|
15 |
+
convertor.addEntry(e);
|
16 |
+
}
|
17 |
+
return convertor;
|
18 |
+
}
|
19 |
+
|
20 |
+
@Override
|
21 |
+
public Map<String, Object> unmarshal(MapConvertor map) throws Exception {
|
22 |
+
Map<String, Object> result = new HashMap<String, Object>();
|
23 |
+
for (MapConvertor.MapEntry e : map.getEntries()) {
|
24 |
+
result.put(e.getKey(), e.getValue());
|
25 |
+
}
|
26 |
+
return result;
|
27 |
+
}
|
28 |
+
|
29 |
+
}
|
30 |
+
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/mapper/adapters/MapConvertor.java
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.mapper.adapters;
|
2 |
+
|
3 |
+
import java.util.ArrayList;
|
4 |
+
import java.util.List;
|
5 |
+
import java.util.Map;
|
6 |
+
|
7 |
+
import javax.xml.bind.annotation.XmlAccessType;
|
8 |
+
import javax.xml.bind.annotation.XmlAccessorType;
|
9 |
+
import javax.xml.bind.annotation.XmlType;
|
10 |
+
|
11 |
+
@XmlType(name = "MapConvertor")
|
12 |
+
@XmlAccessorType(XmlAccessType.FIELD)
|
13 |
+
public class MapConvertor {
|
14 |
+
|
15 |
+
private List<MapEntry> entries = new ArrayList<MapEntry>();
|
16 |
+
|
17 |
+
public void addEntry(MapEntry entry) {
|
18 |
+
entries.add(entry);
|
19 |
+
}
|
20 |
+
|
21 |
+
public List<MapEntry> getEntries() {
|
22 |
+
return entries;
|
23 |
+
}
|
24 |
+
|
25 |
+
public static class MapEntry {
|
26 |
+
|
27 |
+
private String key;
|
28 |
+
|
29 |
+
private Object value;
|
30 |
+
|
31 |
+
public MapEntry() {
|
32 |
+
super();
|
33 |
+
}
|
34 |
+
|
35 |
+
public MapEntry(Map.Entry<String, Object> entry) {
|
36 |
+
super();
|
37 |
+
this.key = entry.getKey();
|
38 |
+
this.value = entry.getValue();
|
39 |
+
}
|
40 |
+
|
41 |
+
public MapEntry(String key, Object value) {
|
42 |
+
super();
|
43 |
+
this.key = key;
|
44 |
+
this.value = value;
|
45 |
+
}
|
46 |
+
|
47 |
+
public String getKey() {
|
48 |
+
return key;
|
49 |
+
}
|
50 |
+
|
51 |
+
public void setKey(String key) {
|
52 |
+
this.key = key;
|
53 |
+
}
|
54 |
+
|
55 |
+
public Object getValue() {
|
56 |
+
return value;
|
57 |
+
}
|
58 |
+
|
59 |
+
public void setValue(Object value) {
|
60 |
+
this.value = value;
|
61 |
+
}
|
62 |
+
}
|
63 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/ActEntity.java
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.io.Serializable;
|
7 |
+
|
8 |
+
import com.fasterxml.jackson.annotation.JsonIgnore;
|
9 |
+
import com.thinkgem.jeesite.modules.act.entity.Act;
|
10 |
+
|
11 |
+
/**
|
12 |
+
* Activiti Entity类
|
13 |
+
* @author ThinkGem
|
14 |
+
* @version 2013-05-28
|
15 |
+
*/
|
16 |
+
public abstract class ActEntity<T> extends DataEntity<T> implements Serializable {
|
17 |
+
|
18 |
+
private static final long serialVersionUID = 1L;
|
19 |
+
|
20 |
+
protected Act act; // 流程任务对象
|
21 |
+
|
22 |
+
public ActEntity() {
|
23 |
+
super();
|
24 |
+
}
|
25 |
+
|
26 |
+
public ActEntity(String id) {
|
27 |
+
super(id);
|
28 |
+
}
|
29 |
+
|
30 |
+
@JsonIgnore
|
31 |
+
public Act getAct() {
|
32 |
+
if (act == null){
|
33 |
+
act = new Act();
|
34 |
+
}
|
35 |
+
return act;
|
36 |
+
}
|
37 |
+
|
38 |
+
public void setAct(Act act) {
|
39 |
+
this.act = act;
|
40 |
+
}
|
41 |
+
|
42 |
+
/**
|
43 |
+
* 获取流程实例ID
|
44 |
+
* @return
|
45 |
+
*/
|
46 |
+
public String getProcInsId() {
|
47 |
+
return this.getAct().getProcInsId();
|
48 |
+
}
|
49 |
+
|
50 |
+
/**
|
51 |
+
* 设置流程实例ID
|
52 |
+
* @param procInsId
|
53 |
+
*/
|
54 |
+
public void setProcInsId(String procInsId) {
|
55 |
+
this.getAct().setProcInsId(procInsId);
|
56 |
+
}
|
57 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/BaseDao.java
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
/**
|
7 |
+
* DAO支持类实现
|
8 |
+
* @author ThinkGem
|
9 |
+
* @version 2014-05-16
|
10 |
+
*/
|
11 |
+
public interface BaseDao {
|
12 |
+
|
13 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/BaseEntity.java
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.io.Serializable;
|
7 |
+
import java.util.Map;
|
8 |
+
|
9 |
+
import javax.xml.bind.annotation.XmlTransient;
|
10 |
+
|
11 |
+
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
12 |
+
|
13 |
+
import com.fasterxml.jackson.annotation.JsonIgnore;
|
14 |
+
import com.google.common.collect.Maps;
|
15 |
+
import com.thinkgem.jeesite.common.config.Global;
|
16 |
+
import com.thinkgem.jeesite.common.supcan.annotation.treelist.SupTreeList;
|
17 |
+
import com.thinkgem.jeesite.common.supcan.annotation.treelist.cols.SupCol;
|
18 |
+
import com.thinkgem.jeesite.common.utils.StringUtils;
|
19 |
+
import com.thinkgem.jeesite.modules.sys.entity.User;
|
20 |
+
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
|
21 |
+
|
22 |
+
/**
|
23 |
+
* Entity支持类
|
24 |
+
* @author ThinkGem
|
25 |
+
* @version 2014-05-16
|
26 |
+
*/
|
27 |
+
@SupTreeList
|
28 |
+
public abstract class BaseEntity<T> implements Serializable {
|
29 |
+
|
30 |
+
private static final long serialVersionUID = 1L;
|
31 |
+
|
32 |
+
/**
|
33 |
+
* 实体编号(唯一标识)
|
34 |
+
*/
|
35 |
+
protected String id;
|
36 |
+
|
37 |
+
/**
|
38 |
+
* 当前用户
|
39 |
+
*/
|
40 |
+
protected User currentUser;
|
41 |
+
|
42 |
+
/**
|
43 |
+
* 当前实体分页对象
|
44 |
+
*/
|
45 |
+
protected Page<T> page;
|
46 |
+
|
47 |
+
/**
|
48 |
+
* 自定义SQL(SQL标识,SQL内容)
|
49 |
+
*/
|
50 |
+
protected Map<String, String> sqlMap;
|
51 |
+
|
52 |
+
/**
|
53 |
+
* 是否是新记录(默认:false),调用setIsNewRecord()设置新记录,使用自定义ID。
|
54 |
+
* 设置为true后强制执行插入语句,ID不会自动生成,需从手动传入。
|
55 |
+
*/
|
56 |
+
protected boolean isNewRecord = false;
|
57 |
+
|
58 |
+
public BaseEntity() {
|
59 |
+
|
60 |
+
}
|
61 |
+
|
62 |
+
public BaseEntity(String id) {
|
63 |
+
this();
|
64 |
+
this.id = id;
|
65 |
+
}
|
66 |
+
|
67 |
+
@SupCol(isUnique="true", isHide="true")
|
68 |
+
public String getId() {
|
69 |
+
return id;
|
70 |
+
}
|
71 |
+
|
72 |
+
public void setId(String id) {
|
73 |
+
this.id = id;
|
74 |
+
}
|
75 |
+
|
76 |
+
@JsonIgnore
|
77 |
+
@XmlTransient
|
78 |
+
public User getCurrentUser() {
|
79 |
+
if(currentUser == null){
|
80 |
+
currentUser = UserUtils.getUser();
|
81 |
+
}
|
82 |
+
return currentUser;
|
83 |
+
}
|
84 |
+
|
85 |
+
public void setCurrentUser(User currentUser) {
|
86 |
+
this.currentUser = currentUser;
|
87 |
+
}
|
88 |
+
|
89 |
+
@JsonIgnore
|
90 |
+
@XmlTransient
|
91 |
+
public Page<T> getPage() {
|
92 |
+
if (page == null){
|
93 |
+
page = new Page<T>();
|
94 |
+
}
|
95 |
+
return page;
|
96 |
+
}
|
97 |
+
|
98 |
+
public Page<T> setPage(Page<T> page) {
|
99 |
+
this.page = page;
|
100 |
+
return page;
|
101 |
+
}
|
102 |
+
|
103 |
+
@JsonIgnore
|
104 |
+
@XmlTransient
|
105 |
+
public Map<String, String> getSqlMap() {
|
106 |
+
if (sqlMap == null){
|
107 |
+
sqlMap = Maps.newHashMap();
|
108 |
+
}
|
109 |
+
return sqlMap;
|
110 |
+
}
|
111 |
+
|
112 |
+
public void setSqlMap(Map<String, String> sqlMap) {
|
113 |
+
this.sqlMap = sqlMap;
|
114 |
+
}
|
115 |
+
|
116 |
+
/**
|
117 |
+
* 插入之前执行方法,子类实现
|
118 |
+
*/
|
119 |
+
public abstract void preInsert();
|
120 |
+
|
121 |
+
/**
|
122 |
+
* 更新之前执行方法,子类实现
|
123 |
+
*/
|
124 |
+
public abstract void preUpdate();
|
125 |
+
|
126 |
+
/**
|
127 |
+
* 是否是新记录(默认:false),调用setIsNewRecord()设置新记录,使用自定义ID。
|
128 |
+
* 设置为true后强制执行插入语句,ID不会自动生成,需从手动传入。
|
129 |
+
* @return
|
130 |
+
*/
|
131 |
+
public boolean getIsNewRecord() {
|
132 |
+
return isNewRecord || StringUtils.isBlank(getId());
|
133 |
+
}
|
134 |
+
|
135 |
+
/**
|
136 |
+
* 是否是新记录(默认:false),调用setIsNewRecord()设置新记录,使用自定义ID。
|
137 |
+
* 设置为true后强制执行插入语句,ID不会自动生成,需从手动传入。
|
138 |
+
*/
|
139 |
+
public void setIsNewRecord(boolean isNewRecord) {
|
140 |
+
this.isNewRecord = isNewRecord;
|
141 |
+
}
|
142 |
+
|
143 |
+
/**
|
144 |
+
* 全局变量对象
|
145 |
+
*/
|
146 |
+
@JsonIgnore
|
147 |
+
public Global getGlobal() {
|
148 |
+
return Global.getInstance();
|
149 |
+
}
|
150 |
+
|
151 |
+
/**
|
152 |
+
* 获取数据库名称
|
153 |
+
*/
|
154 |
+
@JsonIgnore
|
155 |
+
public String getDbName(){
|
156 |
+
return Global.getConfig("jdbc.type");
|
157 |
+
}
|
158 |
+
|
159 |
+
@Override
|
160 |
+
public boolean equals(Object obj) {
|
161 |
+
if (null == obj) {
|
162 |
+
return false;
|
163 |
+
}
|
164 |
+
if (this == obj) {
|
165 |
+
return true;
|
166 |
+
}
|
167 |
+
if (!getClass().equals(obj.getClass())) {
|
168 |
+
return false;
|
169 |
+
}
|
170 |
+
BaseEntity<?> that = (BaseEntity<?>) obj;
|
171 |
+
return null == this.getId() ? false : this.getId().equals(that.getId());
|
172 |
+
}
|
173 |
+
|
174 |
+
@Override
|
175 |
+
public String toString() {
|
176 |
+
return ReflectionToStringBuilder.toString(this);
|
177 |
+
}
|
178 |
+
|
179 |
+
/**
|
180 |
+
* 删除标记(0:正常;1:删除;2:审核;)
|
181 |
+
*/
|
182 |
+
public static final String DEL_FLAG_NORMAL = "0";
|
183 |
+
public static final String DEL_FLAG_DELETE = "1";
|
184 |
+
public static final String DEL_FLAG_AUDIT = "2";
|
185 |
+
|
186 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/CrudDao.java
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.util.List;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* DAO支持类实现
|
10 |
+
* @author ThinkGem
|
11 |
+
* @version 2014-05-16
|
12 |
+
* @param <T>
|
13 |
+
*/
|
14 |
+
public interface CrudDao<T> extends BaseDao {
|
15 |
+
|
16 |
+
/**
|
17 |
+
* 获取单条数据
|
18 |
+
* @param id
|
19 |
+
* @return
|
20 |
+
*/
|
21 |
+
public T get(String id);
|
22 |
+
|
23 |
+
/**
|
24 |
+
* 获取单条数据
|
25 |
+
* @param entity
|
26 |
+
* @return
|
27 |
+
*/
|
28 |
+
public T get(T entity);
|
29 |
+
|
30 |
+
/**
|
31 |
+
* 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<T>());
|
32 |
+
* @param entity
|
33 |
+
* @return
|
34 |
+
*/
|
35 |
+
public List<T> findList(T entity);
|
36 |
+
|
37 |
+
/**
|
38 |
+
* 查询所有数据列表
|
39 |
+
* @param entity
|
40 |
+
* @return
|
41 |
+
*/
|
42 |
+
public List<T> findAllList(T entity);
|
43 |
+
|
44 |
+
/**
|
45 |
+
* 查询所有数据列表
|
46 |
+
* @see public List<T> findAllList(T entity)
|
47 |
+
* @return
|
48 |
+
*/
|
49 |
+
@Deprecated
|
50 |
+
public List<T> findAllList();
|
51 |
+
|
52 |
+
/**
|
53 |
+
* 插入数据
|
54 |
+
* @param entity
|
55 |
+
* @return
|
56 |
+
*/
|
57 |
+
public int insert(T entity);
|
58 |
+
|
59 |
+
/**
|
60 |
+
* 更新数据
|
61 |
+
* @param entity
|
62 |
+
* @return
|
63 |
+
*/
|
64 |
+
public int update(T entity);
|
65 |
+
|
66 |
+
/**
|
67 |
+
* 删除数据(一般为逻辑删除,更新del_flag字段为1)
|
68 |
+
* @param id
|
69 |
+
* @see public int delete(T entity)
|
70 |
+
* @return
|
71 |
+
*/
|
72 |
+
@Deprecated
|
73 |
+
public int delete(String id);
|
74 |
+
|
75 |
+
/**
|
76 |
+
* 删除数据(一般为逻辑删除,更新del_flag字段为1)
|
77 |
+
* @param entity
|
78 |
+
* @return
|
79 |
+
*/
|
80 |
+
public int delete(T entity);
|
81 |
+
|
82 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/DataEntity.java
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.util.Date;
|
7 |
+
|
8 |
+
import org.apache.commons.lang3.StringUtils;
|
9 |
+
import org.hibernate.validator.constraints.Length;
|
10 |
+
|
11 |
+
import com.fasterxml.jackson.annotation.JsonFormat;
|
12 |
+
import com.fasterxml.jackson.annotation.JsonIgnore;
|
13 |
+
import com.thinkgem.jeesite.common.utils.IdGen;
|
14 |
+
import com.thinkgem.jeesite.modules.sys.entity.User;
|
15 |
+
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
|
16 |
+
|
17 |
+
/**
|
18 |
+
* 数据Entity类
|
19 |
+
* @author ThinkGem
|
20 |
+
* @version 2014-05-16
|
21 |
+
*/
|
22 |
+
public abstract class DataEntity<T> extends BaseEntity<T> {
|
23 |
+
|
24 |
+
private static final long serialVersionUID = 1L;
|
25 |
+
|
26 |
+
protected String remarks; // 备注
|
27 |
+
protected User createBy; // 创建者
|
28 |
+
protected Date createDate; // 创建日期
|
29 |
+
protected User updateBy; // 更新者
|
30 |
+
protected Date updateDate; // 更新日期
|
31 |
+
protected String delFlag; // 删除标记(0:正常;1:删除;2:审核)
|
32 |
+
|
33 |
+
public DataEntity() {
|
34 |
+
super();
|
35 |
+
this.delFlag = DEL_FLAG_NORMAL;
|
36 |
+
}
|
37 |
+
|
38 |
+
public DataEntity(String id) {
|
39 |
+
super(id);
|
40 |
+
}
|
41 |
+
|
42 |
+
/**
|
43 |
+
* 插入之前执行方法,需要手动调用
|
44 |
+
*/
|
45 |
+
@Override
|
46 |
+
public void preInsert(){
|
47 |
+
// 不限制ID为UUID,调用setIsNewRecord()使用自定义ID
|
48 |
+
if (!this.isNewRecord){
|
49 |
+
setId(IdGen.uuid());
|
50 |
+
}
|
51 |
+
User user = UserUtils.getUser();
|
52 |
+
if (StringUtils.isNotBlank(user.getId())){
|
53 |
+
this.updateBy = user;
|
54 |
+
this.createBy = user;
|
55 |
+
}
|
56 |
+
this.updateDate = new Date();
|
57 |
+
this.createDate = this.updateDate;
|
58 |
+
}
|
59 |
+
|
60 |
+
/**
|
61 |
+
* 更新之前执行方法,需要手动调用
|
62 |
+
*/
|
63 |
+
@Override
|
64 |
+
public void preUpdate(){
|
65 |
+
User user = UserUtils.getUser();
|
66 |
+
if (StringUtils.isNotBlank(user.getId())){
|
67 |
+
this.updateBy = user;
|
68 |
+
}
|
69 |
+
this.updateDate = new Date();
|
70 |
+
}
|
71 |
+
|
72 |
+
@Length(min=0, max=255)
|
73 |
+
public String getRemarks() {
|
74 |
+
return remarks;
|
75 |
+
}
|
76 |
+
|
77 |
+
public void setRemarks(String remarks) {
|
78 |
+
this.remarks = remarks;
|
79 |
+
}
|
80 |
+
|
81 |
+
@JsonIgnore
|
82 |
+
public User getCreateBy() {
|
83 |
+
return createBy;
|
84 |
+
}
|
85 |
+
|
86 |
+
public void setCreateBy(User createBy) {
|
87 |
+
this.createBy = createBy;
|
88 |
+
}
|
89 |
+
|
90 |
+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
91 |
+
public Date getCreateDate() {
|
92 |
+
return createDate;
|
93 |
+
}
|
94 |
+
|
95 |
+
public void setCreateDate(Date createDate) {
|
96 |
+
this.createDate = createDate;
|
97 |
+
}
|
98 |
+
|
99 |
+
@JsonIgnore
|
100 |
+
public User getUpdateBy() {
|
101 |
+
return updateBy;
|
102 |
+
}
|
103 |
+
|
104 |
+
public void setUpdateBy(User updateBy) {
|
105 |
+
this.updateBy = updateBy;
|
106 |
+
}
|
107 |
+
|
108 |
+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
109 |
+
public Date getUpdateDate() {
|
110 |
+
return updateDate;
|
111 |
+
}
|
112 |
+
|
113 |
+
public void setUpdateDate(Date updateDate) {
|
114 |
+
this.updateDate = updateDate;
|
115 |
+
}
|
116 |
+
|
117 |
+
@JsonIgnore
|
118 |
+
@Length(min=1, max=1)
|
119 |
+
public String getDelFlag() {
|
120 |
+
return delFlag;
|
121 |
+
}
|
122 |
+
|
123 |
+
public void setDelFlag(String delFlag) {
|
124 |
+
this.delFlag = delFlag;
|
125 |
+
}
|
126 |
+
|
127 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/MapperLoader.java
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package com.thinkgem.jeesite.common.persistence;
|
2 |
+
|
3 |
+
import java.io.IOException;
|
4 |
+
import java.lang.reflect.Field;
|
5 |
+
import java.util.HashMap;
|
6 |
+
import java.util.Map;
|
7 |
+
import java.util.Set;
|
8 |
+
import java.util.concurrent.Executors;
|
9 |
+
import java.util.concurrent.ScheduledExecutorService;
|
10 |
+
import java.util.concurrent.TimeUnit;
|
11 |
+
|
12 |
+
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
|
13 |
+
import org.apache.ibatis.executor.ErrorContext;
|
14 |
+
import org.apache.ibatis.session.Configuration;
|
15 |
+
import org.apache.ibatis.session.SqlSessionFactory;
|
16 |
+
import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
17 |
+
import org.springframework.beans.BeansException;
|
18 |
+
import org.springframework.beans.factory.DisposableBean;
|
19 |
+
import org.springframework.beans.factory.InitializingBean;
|
20 |
+
import org.springframework.context.ApplicationContext;
|
21 |
+
import org.springframework.context.ApplicationContextAware;
|
22 |
+
import org.springframework.context.ConfigurableApplicationContext;
|
23 |
+
import org.springframework.core.NestedIOException;
|
24 |
+
import org.springframework.core.io.Resource;
|
25 |
+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
26 |
+
import org.springframework.core.io.support.ResourcePatternResolver;
|
27 |
+
import org.springframework.util.ClassUtils;
|
28 |
+
import org.springframework.util.StringUtils;
|
29 |
+
|
30 |
+
/**
|
31 |
+
* Mybatis的mapper文件中的sql语句被修改后, 只能重启服务器才能被加载, 非常耗时,所以就写了一个自动加载的类,
|
32 |
+
* 配置后检查xml文件更改,如果发生变化,重新加载xml里面的内容.
|
33 |
+
*/
|
34 |
+
//@Service
|
35 |
+
//@Lazy(false)
|
36 |
+
public class MapperLoader implements DisposableBean, InitializingBean, ApplicationContextAware {
|
37 |
+
|
38 |
+
private ConfigurableApplicationContext context = null;
|
39 |
+
private transient String basePackage = null;
|
40 |
+
private HashMap<String, String> fileMapping = new HashMap<String, String>();
|
41 |
+
private Scanner scanner = null;
|
42 |
+
private ScheduledExecutorService service = null;
|
43 |
+
|
44 |
+
@Override
|
45 |
+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
46 |
+
this.context = (ConfigurableApplicationContext) applicationContext;
|
47 |
+
|
48 |
+
}
|
49 |
+
|
50 |
+
@Override
|
51 |
+
public void afterPropertiesSet() throws Exception {
|
52 |
+
try {
|
53 |
+
service = Executors.newScheduledThreadPool(1);
|
54 |
+
|
55 |
+
// 获取xml所在包
|
56 |
+
MapperScannerConfigurer config = context.getBean(MapperScannerConfigurer.class);
|
57 |
+
Field field = config.getClass().getDeclaredField("basePackage");
|
58 |
+
field.setAccessible(true);
|
59 |
+
basePackage = (String) field.get(config);
|
60 |
+
|
61 |
+
// 触发文件监听事件
|
62 |
+
scanner = new Scanner();
|
63 |
+
scanner.scan();
|
64 |
+
|
65 |
+
service.scheduleAtFixedRate(new Task(), 5, 5, TimeUnit.SECONDS);
|
66 |
+
|
67 |
+
} catch (Exception e1) {
|
68 |
+
e1.printStackTrace();
|
69 |
+
}
|
70 |
+
|
71 |
+
}
|
72 |
+
|
73 |
+
class Task implements Runnable {
|
74 |
+
@Override
|
75 |
+
public void run() {
|
76 |
+
try {
|
77 |
+
if (scanner.isChanged()) {
|
78 |
+
System.out.println("*Mapper.xml文件改变,重新加载.");
|
79 |
+
scanner.reloadXML();
|
80 |
+
System.out.println("加载完毕.");
|
81 |
+
}
|
82 |
+
} catch (Exception e) {
|
83 |
+
e.printStackTrace();
|
84 |
+
}
|
85 |
+
}
|
86 |
+
|
87 |
+
}
|
88 |
+
|
89 |
+
@SuppressWarnings({ "rawtypes" })
|
90 |
+
class Scanner {
|
91 |
+
|
92 |
+
private String[] basePackages;
|
93 |
+
private static final String XML_RESOURCE_PATTERN = "**/*.xml";
|
94 |
+
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
95 |
+
|
96 |
+
public Scanner() {
|
97 |
+
basePackages = StringUtils.tokenizeToStringArray(MapperLoader.this.basePackage,
|
98 |
+
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
|
99 |
+
}
|
100 |
+
|
101 |
+
public Resource[] getResource(String basePackage, String pattern) throws IOException {
|
102 |
+
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
|
103 |
+
+ ClassUtils.convertClassNameToResourcePath(context.getEnvironment().resolveRequiredPlaceholders(
|
104 |
+
basePackage)) + "/" + pattern;
|
105 |
+
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
|
106 |
+
return resources;
|
107 |
+
}
|
108 |
+
|
109 |
+
public void reloadXML() throws Exception {
|
110 |
+
SqlSessionFactory factory = context.getBean(SqlSessionFactory.class);
|
111 |
+
Configuration configuration = factory.getConfiguration();
|
112 |
+
// 移除加载项
|
113 |
+
removeConfig(configuration);
|
114 |
+
// 重新扫描加载
|
115 |
+
for (String basePackage : basePackages) {
|
116 |
+
Resource[] resources = getResource(basePackage, XML_RESOURCE_PATTERN);
|
117 |
+
if (resources != null) {
|
118 |
+
for (int i = 0; i < resources.length; i++) {
|
119 |
+
if (resources[i] == null) {
|
120 |
+
continue;
|
121 |
+
}
|
122 |
+
try {
|
123 |
+
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(resources[i].getInputStream(),
|
124 |
+
configuration, resources[i].toString(), configuration.getSqlFragments());
|
125 |
+
xmlMapperBuilder.parse();
|
126 |
+
} catch (Exception e) {
|
127 |
+
throw new NestedIOException("Failed to parse mapping resource: '" + resources[i] + "'", e);
|
128 |
+
} finally {
|
129 |
+
ErrorContext.instance().reset();
|
130 |
+
}
|
131 |
+
}
|
132 |
+
}
|
133 |
+
}
|
134 |
+
|
135 |
+
}
|
136 |
+
|
137 |
+
private void removeConfig(Configuration configuration) throws Exception {
|
138 |
+
Class<?> classConfig = configuration.getClass();
|
139 |
+
clearMap(classConfig, configuration, "mappedStatements");
|
140 |
+
clearMap(classConfig, configuration, "caches");
|
141 |
+
clearMap(classConfig, configuration, "resultMaps");
|
142 |
+
clearMap(classConfig, configuration, "parameterMaps");
|
143 |
+
clearMap(classConfig, configuration, "keyGenerators");
|
144 |
+
clearMap(classConfig, configuration, "sqlFragments");
|
145 |
+
|
146 |
+
clearSet(classConfig, configuration, "loadedResources");
|
147 |
+
|
148 |
+
}
|
149 |
+
|
150 |
+
private void clearMap(Class<?> classConfig, Configuration configuration, String fieldName) throws Exception {
|
151 |
+
Field field = classConfig.getDeclaredField(fieldName);
|
152 |
+
field.setAccessible(true);
|
153 |
+
Map mapConfig = (Map) field.get(configuration);
|
154 |
+
mapConfig.clear();
|
155 |
+
}
|
156 |
+
|
157 |
+
private void clearSet(Class<?> classConfig, Configuration configuration, String fieldName) throws Exception {
|
158 |
+
Field field = classConfig.getDeclaredField(fieldName);
|
159 |
+
field.setAccessible(true);
|
160 |
+
Set setConfig = (Set) field.get(configuration);
|
161 |
+
setConfig.clear();
|
162 |
+
}
|
163 |
+
|
164 |
+
public void scan() throws IOException {
|
165 |
+
if (!fileMapping.isEmpty()) {
|
166 |
+
return;
|
167 |
+
}
|
168 |
+
for (String basePackage : basePackages) {
|
169 |
+
Resource[] resources = getResource(basePackage, XML_RESOURCE_PATTERN);
|
170 |
+
if (resources != null) {
|
171 |
+
for (int i = 0; i < resources.length; i++) {
|
172 |
+
String multi_key = getValue(resources[i]);
|
173 |
+
fileMapping.put(resources[i].getFilename(), multi_key);
|
174 |
+
}
|
175 |
+
}
|
176 |
+
}
|
177 |
+
}
|
178 |
+
|
179 |
+
private String getValue(Resource resource) throws IOException {
|
180 |
+
String contentLength = String.valueOf((resource.contentLength()));
|
181 |
+
String lastModified = String.valueOf((resource.lastModified()));
|
182 |
+
return new StringBuilder(contentLength).append(lastModified).toString();
|
183 |
+
}
|
184 |
+
|
185 |
+
public boolean isChanged() throws IOException {
|
186 |
+
boolean isChanged = false;
|
187 |
+
for (String basePackage : basePackages) {
|
188 |
+
Resource[] resources = getResource(basePackage, XML_RESOURCE_PATTERN);
|
189 |
+
if (resources != null) {
|
190 |
+
for (int i = 0; i < resources.length; i++) {
|
191 |
+
String name = resources[i].getFilename();
|
192 |
+
String value = fileMapping.get(name);
|
193 |
+
String multi_key = getValue(resources[i]);
|
194 |
+
if (!multi_key.equals(value)) {
|
195 |
+
isChanged = true;
|
196 |
+
fileMapping.put(name, multi_key);
|
197 |
+
}
|
198 |
+
}
|
199 |
+
}
|
200 |
+
}
|
201 |
+
return isChanged;
|
202 |
+
}
|
203 |
+
}
|
204 |
+
|
205 |
+
@Override
|
206 |
+
public void destroy() throws Exception {
|
207 |
+
if (service != null) {
|
208 |
+
service.shutdownNow();
|
209 |
+
}
|
210 |
+
}
|
211 |
+
|
212 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/Page.java
ADDED
@@ -0,0 +1,574 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.util.ArrayList;
|
7 |
+
import java.util.List;
|
8 |
+
import java.util.regex.Pattern;
|
9 |
+
|
10 |
+
import javax.servlet.http.HttpServletRequest;
|
11 |
+
import javax.servlet.http.HttpServletResponse;
|
12 |
+
|
13 |
+
import org.apache.commons.lang3.StringUtils;
|
14 |
+
|
15 |
+
import com.fasterxml.jackson.annotation.JsonIgnore;
|
16 |
+
import com.thinkgem.jeesite.common.config.Global;
|
17 |
+
import com.thinkgem.jeesite.common.utils.CookieUtils;
|
18 |
+
|
19 |
+
/**
|
20 |
+
* 分页类
|
21 |
+
* @author ThinkGem
|
22 |
+
* @version 2013-7-2
|
23 |
+
* @param <T>
|
24 |
+
*/
|
25 |
+
public class Page<T> {
|
26 |
+
|
27 |
+
private int pageNo = 1; // 当前页码
|
28 |
+
private int pageSize = Integer.valueOf(Global.getConfig("page.pageSize")); // 页面大小,设置为“-1”表示不进行分页(分页无效)
|
29 |
+
|
30 |
+
private long count;// 总记录数,设置为“-1”表示不查询总数
|
31 |
+
|
32 |
+
private int first;// 首页索引
|
33 |
+
private int last;// 尾页索引
|
34 |
+
private int prev;// 上一页索引
|
35 |
+
private int next;// 下一页索引
|
36 |
+
|
37 |
+
private boolean firstPage;//是否是第一页
|
38 |
+
private boolean lastPage;//是否是最后一页
|
39 |
+
|
40 |
+
private int length = 8;// 显示页面长度
|
41 |
+
private int slider = 1;// 前后显示页面长度
|
42 |
+
|
43 |
+
private List<T> list = new ArrayList<T>();
|
44 |
+
|
45 |
+
private String orderBy = ""; // 标准查询有效, 实例: updatedate desc, name asc
|
46 |
+
|
47 |
+
private String funcName = "page"; // 设置点击页码调用的js函数名称,默认为page,在一页有多个分页对象时使用。
|
48 |
+
|
49 |
+
private String funcParam = ""; // 函数的附加参数,第三个参数值。
|
50 |
+
|
51 |
+
private String message = ""; // 设置提示消息,显示在“共n条”之后
|
52 |
+
|
53 |
+
public Page() {
|
54 |
+
this.pageSize = -1;
|
55 |
+
}
|
56 |
+
|
57 |
+
/**
|
58 |
+
* 构造方法
|
59 |
+
* @param request 传递 repage 参数,来记住页码
|
60 |
+
* @param response 用于设置 Cookie,记住页码
|
61 |
+
*/
|
62 |
+
public Page(HttpServletRequest request, HttpServletResponse response){
|
63 |
+
this(request, response, -2);
|
64 |
+
}
|
65 |
+
|
66 |
+
/**
|
67 |
+
* 构造方法
|
68 |
+
* @param request 传递 repage 参数,来记住页码
|
69 |
+
* @param response 用于设置 Cookie,记住页码
|
70 |
+
* @param defaultPageSize 默认分页大小,如果传递 -1 则为不分页,返回所有数据
|
71 |
+
*/
|
72 |
+
public Page(HttpServletRequest request, HttpServletResponse response, int defaultPageSize){
|
73 |
+
// 设置页码参数(传递repage参数,来记住页码)
|
74 |
+
String no = request.getParameter("pageNo");
|
75 |
+
if (StringUtils.isNumeric(no)){
|
76 |
+
CookieUtils.setCookie(response, "pageNo", no);
|
77 |
+
this.setPageNo(Integer.parseInt(no));
|
78 |
+
}else if (request.getParameter("repage")!=null){
|
79 |
+
no = CookieUtils.getCookie(request, "pageNo");
|
80 |
+
if (StringUtils.isNumeric(no)){
|
81 |
+
this.setPageNo(Integer.parseInt(no));
|
82 |
+
}
|
83 |
+
}
|
84 |
+
// 设置页面大小参数(传递repage参数,来记住页码大小)
|
85 |
+
String size = request.getParameter("pageSize");
|
86 |
+
if (StringUtils.isNumeric(size)){
|
87 |
+
CookieUtils.setCookie(response, "pageSize", size);
|
88 |
+
this.setPageSize(Integer.parseInt(size));
|
89 |
+
}else if (request.getParameter("repage")!=null){
|
90 |
+
size = CookieUtils.getCookie(request, "pageSize");
|
91 |
+
if (StringUtils.isNumeric(size)){
|
92 |
+
this.setPageSize(Integer.parseInt(size));
|
93 |
+
}
|
94 |
+
}else if (defaultPageSize != -2){
|
95 |
+
this.pageSize = defaultPageSize;
|
96 |
+
}
|
97 |
+
// 设置页面分页函数
|
98 |
+
String funcName = request.getParameter("funcName");
|
99 |
+
if (StringUtils.isNotBlank(funcName)){
|
100 |
+
CookieUtils.setCookie(response, "funcName", funcName);
|
101 |
+
this.setFuncName(funcName);
|
102 |
+
}else if (request.getParameter("repage")!=null){
|
103 |
+
funcName = CookieUtils.getCookie(request, "funcName");
|
104 |
+
if (StringUtils.isNotBlank(funcName)){
|
105 |
+
this.setFuncName(funcName);
|
106 |
+
}
|
107 |
+
}
|
108 |
+
// 设置排序参数
|
109 |
+
String orderBy = request.getParameter("orderBy");
|
110 |
+
if (StringUtils.isNotBlank(orderBy)){
|
111 |
+
this.setOrderBy(orderBy);
|
112 |
+
}
|
113 |
+
}
|
114 |
+
|
115 |
+
/**
|
116 |
+
* 构造方法
|
117 |
+
* @param pageNo 当前页码
|
118 |
+
* @param pageSize 分页大小
|
119 |
+
*/
|
120 |
+
public Page(int pageNo, int pageSize) {
|
121 |
+
this(pageNo, pageSize, 0);
|
122 |
+
}
|
123 |
+
|
124 |
+
/**
|
125 |
+
* 构造方法
|
126 |
+
* @param pageNo 当前页码
|
127 |
+
* @param pageSize 分页大小
|
128 |
+
* @param count 数据条数
|
129 |
+
*/
|
130 |
+
public Page(int pageNo, int pageSize, long count) {
|
131 |
+
this(pageNo, pageSize, count, new ArrayList<T>());
|
132 |
+
}
|
133 |
+
|
134 |
+
/**
|
135 |
+
* 构造方法
|
136 |
+
* @param pageNo 当前页码
|
137 |
+
* @param pageSize 分页大小
|
138 |
+
* @param count 数据条数
|
139 |
+
* @param list 本页数据对象列表
|
140 |
+
*/
|
141 |
+
public Page(int pageNo, int pageSize, long count, List<T> list) {
|
142 |
+
this.setCount(count);
|
143 |
+
this.setPageNo(pageNo);
|
144 |
+
this.pageSize = pageSize;
|
145 |
+
this.list = list;
|
146 |
+
}
|
147 |
+
|
148 |
+
/**
|
149 |
+
* 初始化参数
|
150 |
+
*/
|
151 |
+
public void initialize(){
|
152 |
+
|
153 |
+
//1
|
154 |
+
this.first = 1;
|
155 |
+
|
156 |
+
this.last = (int)(count / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1);
|
157 |
+
|
158 |
+
if (this.count % this.pageSize != 0 || this.last == 0) {
|
159 |
+
this.last++;
|
160 |
+
}
|
161 |
+
|
162 |
+
if (this.last < this.first) {
|
163 |
+
this.last = this.first;
|
164 |
+
}
|
165 |
+
|
166 |
+
if (this.pageNo <= 1) {
|
167 |
+
this.pageNo = this.first;
|
168 |
+
this.firstPage=true;
|
169 |
+
}
|
170 |
+
|
171 |
+
if (this.pageNo >= this.last) {
|
172 |
+
this.pageNo = this.last;
|
173 |
+
this.lastPage=true;
|
174 |
+
}
|
175 |
+
|
176 |
+
if (this.pageNo < this.last - 1) {
|
177 |
+
this.next = this.pageNo + 1;
|
178 |
+
} else {
|
179 |
+
this.next = this.last;
|
180 |
+
}
|
181 |
+
|
182 |
+
if (this.pageNo > 1) {
|
183 |
+
this.prev = this.pageNo - 1;
|
184 |
+
} else {
|
185 |
+
this.prev = this.first;
|
186 |
+
}
|
187 |
+
|
188 |
+
//2
|
189 |
+
if (this.pageNo < this.first) {// 如果当前页小于首页
|
190 |
+
this.pageNo = this.first;
|
191 |
+
}
|
192 |
+
|
193 |
+
if (this.pageNo > this.last) {// 如果当前页大于尾页
|
194 |
+
this.pageNo = this.last;
|
195 |
+
}
|
196 |
+
|
197 |
+
}
|
198 |
+
|
199 |
+
/**
|
200 |
+
* 默认输出当前分页标签
|
201 |
+
* <div class="page">${page}</div>
|
202 |
+
*/
|
203 |
+
@Override
|
204 |
+
public String toString() {
|
205 |
+
|
206 |
+
StringBuilder sb = new StringBuilder();
|
207 |
+
|
208 |
+
if (pageNo == first) {// 如果是首页
|
209 |
+
sb.append("<li class=\"disabled\"><a href=\"javascript:\">« 上一页</a></li>\n");
|
210 |
+
} else {
|
211 |
+
sb.append("<li><a href=\"javascript:\" onclick=\""+funcName+"("+prev+","+pageSize+",'"+funcParam+"');\">« 上一页</a></li>\n");
|
212 |
+
}
|
213 |
+
|
214 |
+
int begin = pageNo - (length / 2);
|
215 |
+
|
216 |
+
if (begin < first) {
|
217 |
+
begin = first;
|
218 |
+
}
|
219 |
+
|
220 |
+
int end = begin + length - 1;
|
221 |
+
|
222 |
+
if (end >= last) {
|
223 |
+
end = last;
|
224 |
+
begin = end - length + 1;
|
225 |
+
if (begin < first) {
|
226 |
+
begin = first;
|
227 |
+
}
|
228 |
+
}
|
229 |
+
|
230 |
+
if (begin > first) {
|
231 |
+
int i = 0;
|
232 |
+
for (i = first; i < first + slider && i < begin; i++) {
|
233 |
+
sb.append("<li><a href=\"javascript:\" onclick=\""+funcName+"("+i+","+pageSize+",'"+funcParam+"');\">"
|
234 |
+
+ (i + 1 - first) + "</a></li>\n");
|
235 |
+
}
|
236 |
+
if (i < begin) {
|
237 |
+
sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
|
238 |
+
}
|
239 |
+
}
|
240 |
+
|
241 |
+
for (int i = begin; i <= end; i++) {
|
242 |
+
if (i == pageNo) {
|
243 |
+
sb.append("<li class=\"active\"><a href=\"javascript:\">" + (i + 1 - first)
|
244 |
+
+ "</a></li>\n");
|
245 |
+
} else {
|
246 |
+
sb.append("<li><a href=\"javascript:\" onclick=\""+funcName+"("+i+","+pageSize+",'"+funcParam+"');\">"
|
247 |
+
+ (i + 1 - first) + "</a></li>\n");
|
248 |
+
}
|
249 |
+
}
|
250 |
+
|
251 |
+
if (last - end > slider) {
|
252 |
+
sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
|
253 |
+
end = last - slider;
|
254 |
+
}
|
255 |
+
|
256 |
+
for (int i = end + 1; i <= last; i++) {
|
257 |
+
sb.append("<li><a href=\"javascript:\" onclick=\""+funcName+"("+i+","+pageSize+",'"+funcParam+"');\">"
|
258 |
+
+ (i + 1 - first) + "</a></li>\n");
|
259 |
+
}
|
260 |
+
|
261 |
+
if (pageNo == last) {
|
262 |
+
sb.append("<li class=\"disabled\"><a href=\"javascript:\">下一页 »</a></li>\n");
|
263 |
+
} else {
|
264 |
+
sb.append("<li><a href=\"javascript:\" onclick=\""+funcName+"("+next+","+pageSize+",'"+funcParam+"');\">"
|
265 |
+
+ "下一页 »</a></li>\n");
|
266 |
+
}
|
267 |
+
|
268 |
+
sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">当前 ");
|
269 |
+
sb.append("<input type=\"text\" value=\""+pageNo+"\" onkeypress=\"var e=window.event||event;var c=e.keyCode||e.which;if(c==13)");
|
270 |
+
sb.append(funcName+"(this.value,"+pageSize+",'"+funcParam+"');\" onclick=\"this.select();\"/> / ");
|
271 |
+
sb.append("<input type=\"text\" value=\""+pageSize+"\" onkeypress=\"var e=window.event||event;var c=e.keyCode||e.which;if(c==13)");
|
272 |
+
sb.append(funcName+"("+pageNo+",this.value,'"+funcParam+"');\" onclick=\"this.select();\"/> 条,");
|
273 |
+
sb.append("共 " + count + " 条"+(message!=null?message:"")+"</a></li>\n");
|
274 |
+
|
275 |
+
sb.insert(0,"<ul>\n").append("</ul>\n");
|
276 |
+
|
277 |
+
sb.append("<div style=\"clear:both;\"></div>");
|
278 |
+
|
279 |
+
// sb.insert(0,"<div class=\"page\">\n").append("</div>\n");
|
280 |
+
|
281 |
+
return sb.toString();
|
282 |
+
}
|
283 |
+
|
284 |
+
/**
|
285 |
+
* 获取分页HTML代码
|
286 |
+
* @return
|
287 |
+
*/
|
288 |
+
public String getHtml(){
|
289 |
+
return toString();
|
290 |
+
}
|
291 |
+
|
292 |
+
// public static void main(String[] args) {
|
293 |
+
// Page<String> p = new Page<String>(3, 3);
|
294 |
+
// System.out.println(p);
|
295 |
+
// System.out.println("首页:"+p.getFirst());
|
296 |
+
// System.out.println("尾页:"+p.getLast());
|
297 |
+
// System.out.println("上页:"+p.getPrev());
|
298 |
+
// System.out.println("下页:"+p.getNext());
|
299 |
+
// }
|
300 |
+
|
301 |
+
/**
|
302 |
+
* 获取设置总数
|
303 |
+
* @return
|
304 |
+
*/
|
305 |
+
public long getCount() {
|
306 |
+
return count;
|
307 |
+
}
|
308 |
+
|
309 |
+
/**
|
310 |
+
* 设置数据总数
|
311 |
+
* @param count
|
312 |
+
*/
|
313 |
+
public void setCount(long count) {
|
314 |
+
this.count = count;
|
315 |
+
if (pageSize >= count){
|
316 |
+
pageNo = 1;
|
317 |
+
}
|
318 |
+
}
|
319 |
+
|
320 |
+
/**
|
321 |
+
* 获取当前页码
|
322 |
+
* @return
|
323 |
+
*/
|
324 |
+
public int getPageNo() {
|
325 |
+
return pageNo;
|
326 |
+
}
|
327 |
+
|
328 |
+
/**
|
329 |
+
* 设置当前页码
|
330 |
+
* @param pageNo
|
331 |
+
*/
|
332 |
+
public void setPageNo(int pageNo) {
|
333 |
+
this.pageNo = pageNo;
|
334 |
+
}
|
335 |
+
|
336 |
+
/**
|
337 |
+
* 获取页面大小
|
338 |
+
* @return
|
339 |
+
*/
|
340 |
+
public int getPageSize() {
|
341 |
+
return pageSize;
|
342 |
+
}
|
343 |
+
|
344 |
+
/**
|
345 |
+
* 设置页面大小(最大500)
|
346 |
+
* @param pageSize
|
347 |
+
*/
|
348 |
+
public void setPageSize(int pageSize) {
|
349 |
+
this.pageSize = pageSize <= 0 ? 10 : pageSize;// > 500 ? 500 : pageSize;
|
350 |
+
}
|
351 |
+
|
352 |
+
/**
|
353 |
+
* 首页索引
|
354 |
+
* @return
|
355 |
+
*/
|
356 |
+
@JsonIgnore
|
357 |
+
public int getFirst() {
|
358 |
+
return first;
|
359 |
+
}
|
360 |
+
|
361 |
+
/**
|
362 |
+
* 尾页索引
|
363 |
+
* @return
|
364 |
+
*/
|
365 |
+
@JsonIgnore
|
366 |
+
public int getLast() {
|
367 |
+
return last;
|
368 |
+
}
|
369 |
+
|
370 |
+
/**
|
371 |
+
* 获取页面总数
|
372 |
+
* @return getLast();
|
373 |
+
*/
|
374 |
+
@JsonIgnore
|
375 |
+
public int getTotalPage() {
|
376 |
+
return getLast();
|
377 |
+
}
|
378 |
+
|
379 |
+
/**
|
380 |
+
* 是否为第一页
|
381 |
+
* @return
|
382 |
+
*/
|
383 |
+
@JsonIgnore
|
384 |
+
public boolean isFirstPage() {
|
385 |
+
return firstPage;
|
386 |
+
}
|
387 |
+
|
388 |
+
/**
|
389 |
+
* 是否为最后一页
|
390 |
+
* @return
|
391 |
+
*/
|
392 |
+
@JsonIgnore
|
393 |
+
public boolean isLastPage() {
|
394 |
+
return lastPage;
|
395 |
+
}
|
396 |
+
|
397 |
+
/**
|
398 |
+
* 上一页索引值
|
399 |
+
* @return
|
400 |
+
*/
|
401 |
+
@JsonIgnore
|
402 |
+
public int getPrev() {
|
403 |
+
if (isFirstPage()) {
|
404 |
+
return pageNo;
|
405 |
+
} else {
|
406 |
+
return pageNo - 1;
|
407 |
+
}
|
408 |
+
}
|
409 |
+
|
410 |
+
/**
|
411 |
+
* 下一页索引值
|
412 |
+
* @return
|
413 |
+
*/
|
414 |
+
@JsonIgnore
|
415 |
+
public int getNext() {
|
416 |
+
if (isLastPage()) {
|
417 |
+
return pageNo;
|
418 |
+
} else {
|
419 |
+
return pageNo + 1;
|
420 |
+
}
|
421 |
+
}
|
422 |
+
|
423 |
+
/**
|
424 |
+
* 获取本页数据对象列表
|
425 |
+
* @return List<T>
|
426 |
+
*/
|
427 |
+
public List<T> getList() {
|
428 |
+
return list;
|
429 |
+
}
|
430 |
+
|
431 |
+
/**
|
432 |
+
* 设置本页数据对象列表
|
433 |
+
* @param list
|
434 |
+
*/
|
435 |
+
public Page<T> setList(List<T> list) {
|
436 |
+
this.list = list;
|
437 |
+
initialize();
|
438 |
+
return this;
|
439 |
+
}
|
440 |
+
|
441 |
+
/**
|
442 |
+
* 获取查询排序字符串
|
443 |
+
* @return
|
444 |
+
*/
|
445 |
+
@JsonIgnore
|
446 |
+
public String getOrderBy() {
|
447 |
+
// SQL过滤,防止注入
|
448 |
+
String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
|
449 |
+
+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
|
450 |
+
Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
|
451 |
+
if (sqlPattern.matcher(orderBy).find()) {
|
452 |
+
return "";
|
453 |
+
}
|
454 |
+
return orderBy;
|
455 |
+
}
|
456 |
+
|
457 |
+
/**
|
458 |
+
* 设置查询排序,标准查询有效, 实例: updatedate desc, name asc
|
459 |
+
*/
|
460 |
+
public void setOrderBy(String orderBy) {
|
461 |
+
this.orderBy = orderBy;
|
462 |
+
}
|
463 |
+
|
464 |
+
/**
|
465 |
+
* 获取点击页码调用的js函数名称
|
466 |
+
* function ${page.funcName}(pageNo){location="${ctx}/list-${category.id}${urlSuffix}?pageNo="+i;}
|
467 |
+
* @return
|
468 |
+
*/
|
469 |
+
@JsonIgnore
|
470 |
+
public String getFuncName() {
|
471 |
+
return funcName;
|
472 |
+
}
|
473 |
+
|
474 |
+
/**
|
475 |
+
* 设置点击页码调用的js函数名称,默认为page,在一页有多个分页对象时使用。
|
476 |
+
* @param funcName 默认为page
|
477 |
+
*/
|
478 |
+
public void setFuncName(String funcName) {
|
479 |
+
this.funcName = funcName;
|
480 |
+
}
|
481 |
+
|
482 |
+
/**
|
483 |
+
* 获取分页函数的附加参数
|
484 |
+
* @return
|
485 |
+
*/
|
486 |
+
@JsonIgnore
|
487 |
+
public String getFuncParam() {
|
488 |
+
return funcParam;
|
489 |
+
}
|
490 |
+
|
491 |
+
/**
|
492 |
+
* 设置分页函数的附加参数
|
493 |
+
* @return
|
494 |
+
*/
|
495 |
+
public void setFuncParam(String funcParam) {
|
496 |
+
this.funcParam = funcParam;
|
497 |
+
}
|
498 |
+
|
499 |
+
/**
|
500 |
+
* 设置提示消息,显示在“共n条”之后
|
501 |
+
* @param message
|
502 |
+
*/
|
503 |
+
public void setMessage(String message) {
|
504 |
+
this.message = message;
|
505 |
+
}
|
506 |
+
|
507 |
+
/**
|
508 |
+
* 分页是否有效
|
509 |
+
* @return this.pageSize==-1
|
510 |
+
*/
|
511 |
+
@JsonIgnore
|
512 |
+
public boolean isDisabled() {
|
513 |
+
return this.pageSize==-1;
|
514 |
+
}
|
515 |
+
|
516 |
+
/**
|
517 |
+
* 是否进行总数统计
|
518 |
+
* @return this.count==-1
|
519 |
+
*/
|
520 |
+
@JsonIgnore
|
521 |
+
public boolean isNotCount() {
|
522 |
+
return this.count==-1;
|
523 |
+
}
|
524 |
+
|
525 |
+
/**
|
526 |
+
* 获取 Hibernate FirstResult
|
527 |
+
*/
|
528 |
+
public int getFirstResult(){
|
529 |
+
int firstResult = (getPageNo() - 1) * getPageSize();
|
530 |
+
if (firstResult >= getCount()) {
|
531 |
+
firstResult = 0;
|
532 |
+
}
|
533 |
+
return firstResult;
|
534 |
+
}
|
535 |
+
/**
|
536 |
+
* 获取 Hibernate MaxResults
|
537 |
+
*/
|
538 |
+
public int getMaxResults(){
|
539 |
+
return getPageSize();
|
540 |
+
}
|
541 |
+
|
542 |
+
// /**
|
543 |
+
// * 获取 Spring data JPA 分页对象
|
544 |
+
// */
|
545 |
+
// public Pageable getSpringPage(){
|
546 |
+
// List<Order> orders = new ArrayList<Order>();
|
547 |
+
// if (orderBy!=null){
|
548 |
+
// for (String order : StringUtils.split(orderBy, ",")){
|
549 |
+
// String[] o = StringUtils.split(order, " ");
|
550 |
+
// if (o.length==1){
|
551 |
+
// orders.add(new Order(Direction.ASC, o[0]));
|
552 |
+
// }else if (o.length==2){
|
553 |
+
// if ("DESC".equals(o[1].toUpperCase())){
|
554 |
+
// orders.add(new Order(Direction.DESC, o[0]));
|
555 |
+
// }else{
|
556 |
+
// orders.add(new Order(Direction.ASC, o[0]));
|
557 |
+
// }
|
558 |
+
// }
|
559 |
+
// }
|
560 |
+
// }
|
561 |
+
// return new PageRequest(this.pageNo - 1, this.pageSize, new Sort(orders));
|
562 |
+
// }
|
563 |
+
//
|
564 |
+
// /**
|
565 |
+
// * 设置 Spring data JPA 分页对象,转换为本系统分页对象
|
566 |
+
// */
|
567 |
+
// public void setSpringPage(org.springframework.data.domain.Page<T> page){
|
568 |
+
// this.pageNo = page.getNumber();
|
569 |
+
// this.pageSize = page.getSize();
|
570 |
+
// this.count = page.getTotalElements();
|
571 |
+
// this.list = page.getContent();
|
572 |
+
// }
|
573 |
+
|
574 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/Parameter.java
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.util.HashMap;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* 查询参数类
|
10 |
+
* @author ThinkGem
|
11 |
+
* @version 2013-8-23
|
12 |
+
*/
|
13 |
+
public class Parameter extends HashMap<String, Object> {
|
14 |
+
|
15 |
+
private static final long serialVersionUID = 1L;
|
16 |
+
|
17 |
+
/**
|
18 |
+
* 构造类,例:new Parameter(id, parentIds)
|
19 |
+
* @param values 参数值
|
20 |
+
*/
|
21 |
+
public Parameter(Object... values) {
|
22 |
+
if (values != null){
|
23 |
+
for (int i=0; i<values.length; i++){
|
24 |
+
put("p"+(i+1), values[i]);
|
25 |
+
}
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
/**
|
30 |
+
* 构造类,例:new Parameter(new Object[][]{{"id", id}, {"parentIds", parentIds}})
|
31 |
+
* @param parameters 参数二维数组
|
32 |
+
*/
|
33 |
+
public Parameter(Object[][] parameters) {
|
34 |
+
if (parameters != null){
|
35 |
+
for (Object[] os : parameters){
|
36 |
+
if (os.length == 2){
|
37 |
+
put((String)os[0], os[1]);
|
38 |
+
}
|
39 |
+
}
|
40 |
+
}
|
41 |
+
}
|
42 |
+
|
43 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/TreeDao.java
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import java.util.List;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* DAO支持类实现
|
10 |
+
* @author ThinkGem
|
11 |
+
* @version 2014-05-16
|
12 |
+
* @param <T>
|
13 |
+
*/
|
14 |
+
public interface TreeDao<T extends TreeEntity<T>> extends CrudDao<T> {
|
15 |
+
|
16 |
+
/**
|
17 |
+
* 找到所有子节点
|
18 |
+
* @param entity
|
19 |
+
* @return
|
20 |
+
*/
|
21 |
+
public List<T> findByParentIdsLike(T entity);
|
22 |
+
|
23 |
+
/**
|
24 |
+
* 更新所有父节点字段
|
25 |
+
* @param entity
|
26 |
+
* @return
|
27 |
+
*/
|
28 |
+
public int updateParentIds(T entity);
|
29 |
+
|
30 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/TreeEntity.java
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence;
|
5 |
+
|
6 |
+
import javax.validation.constraints.NotNull;
|
7 |
+
|
8 |
+
import org.hibernate.validator.constraints.Length;
|
9 |
+
|
10 |
+
import com.fasterxml.jackson.annotation.JsonBackReference;
|
11 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
12 |
+
import com.thinkgem.jeesite.common.utils.StringUtils;
|
13 |
+
|
14 |
+
/**
|
15 |
+
* 数据Entity类
|
16 |
+
* @author ThinkGem
|
17 |
+
* @version 2014-05-16
|
18 |
+
*/
|
19 |
+
public abstract class TreeEntity<T> extends DataEntity<T> {
|
20 |
+
|
21 |
+
private static final long serialVersionUID = 1L;
|
22 |
+
|
23 |
+
protected T parent; // 父级编号
|
24 |
+
protected String parentIds; // 所有父级编号
|
25 |
+
protected String name; // 机构名称
|
26 |
+
protected Integer sort; // 排序
|
27 |
+
|
28 |
+
public TreeEntity() {
|
29 |
+
super();
|
30 |
+
this.sort = 30;
|
31 |
+
}
|
32 |
+
|
33 |
+
public TreeEntity(String id) {
|
34 |
+
super(id);
|
35 |
+
}
|
36 |
+
|
37 |
+
/**
|
38 |
+
* 父对象,只能通过子类实现,父类实现mybatis无法读取
|
39 |
+
* @return
|
40 |
+
*/
|
41 |
+
@JsonBackReference
|
42 |
+
@NotNull
|
43 |
+
public abstract T getParent();
|
44 |
+
|
45 |
+
/**
|
46 |
+
* 父对象,只能通过子类实现,父类实现mybatis无法读取
|
47 |
+
* @return
|
48 |
+
*/
|
49 |
+
public abstract void setParent(T parent);
|
50 |
+
|
51 |
+
@Length(min=1, max=2000)
|
52 |
+
public String getParentIds() {
|
53 |
+
return parentIds;
|
54 |
+
}
|
55 |
+
|
56 |
+
public void setParentIds(String parentIds) {
|
57 |
+
this.parentIds = parentIds;
|
58 |
+
}
|
59 |
+
|
60 |
+
@Length(min=1, max=100)
|
61 |
+
public String getName() {
|
62 |
+
return name;
|
63 |
+
}
|
64 |
+
|
65 |
+
public void setName(String name) {
|
66 |
+
this.name = name;
|
67 |
+
}
|
68 |
+
|
69 |
+
public Integer getSort() {
|
70 |
+
return sort;
|
71 |
+
}
|
72 |
+
|
73 |
+
public void setSort(Integer sort) {
|
74 |
+
this.sort = sort;
|
75 |
+
}
|
76 |
+
|
77 |
+
public String getParentId() {
|
78 |
+
String id = null;
|
79 |
+
if (parent != null){
|
80 |
+
id = (String)Reflections.getFieldValue(parent, "id");
|
81 |
+
}
|
82 |
+
return StringUtils.isNotBlank(id) ? id : "0";
|
83 |
+
}
|
84 |
+
|
85 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/annotation/MyBatisDao.java
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.annotation;
|
5 |
+
|
6 |
+
import java.lang.annotation.Documented;
|
7 |
+
import java.lang.annotation.ElementType;
|
8 |
+
import java.lang.annotation.Retention;
|
9 |
+
import java.lang.annotation.RetentionPolicy;
|
10 |
+
import java.lang.annotation.Target;
|
11 |
+
|
12 |
+
import org.springframework.stereotype.Component;
|
13 |
+
|
14 |
+
/**
|
15 |
+
* 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。
|
16 |
+
* @author thinkgem
|
17 |
+
* @version 2013-8-28
|
18 |
+
*/
|
19 |
+
@Retention(RetentionPolicy.RUNTIME)
|
20 |
+
@Target(ElementType.TYPE)
|
21 |
+
@Documented
|
22 |
+
@Component
|
23 |
+
public @interface MyBatisDao {
|
24 |
+
|
25 |
+
/**
|
26 |
+
* The value may indicate a suggestion for a logical component name,
|
27 |
+
* to be turned into a Spring bean in case of an autodetected component.
|
28 |
+
* @return the suggested component name, if any
|
29 |
+
*/
|
30 |
+
String value() default "";
|
31 |
+
|
32 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/Dialect.java
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect;
|
5 |
+
|
6 |
+
/**
|
7 |
+
* 类似hibernate的Dialect,但只精简出分页部分
|
8 |
+
*
|
9 |
+
* @author poplar.yfyang
|
10 |
+
* @version 1.0 2011-11-18 下午12:31
|
11 |
+
* @since JDK 1.5
|
12 |
+
*/
|
13 |
+
public interface Dialect {
|
14 |
+
|
15 |
+
/**
|
16 |
+
* 数据库本身是否支持分页当前的分页查询方式
|
17 |
+
* 如果数据库不支持的话,则不进行数据库分页
|
18 |
+
*
|
19 |
+
* @return true:支持当前的分页查询方式
|
20 |
+
*/
|
21 |
+
public boolean supportsLimit();
|
22 |
+
|
23 |
+
/**
|
24 |
+
* 将sql转换为分页SQL,分别调用分页sql
|
25 |
+
*
|
26 |
+
* @param sql SQL语句
|
27 |
+
* @param offset 开始条数
|
28 |
+
* @param limit 每页显示多少纪录条数
|
29 |
+
* @return 分页查询的sql
|
30 |
+
*/
|
31 |
+
public String getLimitString(String sql, int offset, int limit);
|
32 |
+
|
33 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/DB2Dialect.java
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* DB2的分页数据库方言实现
|
10 |
+
*
|
11 |
+
* @author poplar.yfyang
|
12 |
+
* @version 1.0 2010-10-10 下午12:31
|
13 |
+
* @since JDK 1.5
|
14 |
+
*/
|
15 |
+
public class DB2Dialect implements Dialect {
|
16 |
+
@Override
|
17 |
+
public boolean supportsLimit() {
|
18 |
+
return true;
|
19 |
+
}
|
20 |
+
|
21 |
+
private static String getRowNumber(String sql) {
|
22 |
+
StringBuilder rownumber = new StringBuilder(50)
|
23 |
+
.append("rownumber() over(");
|
24 |
+
|
25 |
+
int orderByIndex = sql.toLowerCase().indexOf("order by");
|
26 |
+
|
27 |
+
if (orderByIndex > 0 && !hasDistinct(sql)) {
|
28 |
+
rownumber.append(sql.substring(orderByIndex));
|
29 |
+
}
|
30 |
+
|
31 |
+
rownumber.append(") as rownumber_,");
|
32 |
+
|
33 |
+
return rownumber.toString();
|
34 |
+
}
|
35 |
+
|
36 |
+
private static boolean hasDistinct(String sql) {
|
37 |
+
return sql.toLowerCase().contains("select distinct");
|
38 |
+
}
|
39 |
+
|
40 |
+
@Override
|
41 |
+
public String getLimitString(String sql, int offset, int limit) {
|
42 |
+
return getLimitString(sql, offset, Integer.toString(offset), Integer.toString(limit));
|
43 |
+
}
|
44 |
+
|
45 |
+
/**
|
46 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
47 |
+
* <pre>
|
48 |
+
* 如mysql
|
49 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
50 |
+
* select * from user limit :offset,:limit
|
51 |
+
* </pre>
|
52 |
+
*
|
53 |
+
* @param sql 实际SQL语句
|
54 |
+
* @param offset 分页开始纪录条数
|
55 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
56 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
57 |
+
* @return 包含占位符的分页sql
|
58 |
+
*/
|
59 |
+
public String getLimitString(String sql, int offset, String offsetPlaceholder, String limitPlaceholder) {
|
60 |
+
int startOfSelect = sql.toLowerCase().indexOf("select");
|
61 |
+
|
62 |
+
StringBuilder pagingSelect = new StringBuilder(sql.length() + 100)
|
63 |
+
.append(sql.substring(0, startOfSelect)) //add the comment
|
64 |
+
.append("select * from ( select ") //nest the main query in an outer select
|
65 |
+
.append(getRowNumber(sql)); //add the rownnumber bit into the outer query select list
|
66 |
+
|
67 |
+
if (hasDistinct(sql)) {
|
68 |
+
pagingSelect.append(" row_.* from ( ") //add another (inner) nested select
|
69 |
+
.append(sql.substring(startOfSelect)) //add the main query
|
70 |
+
.append(" ) as row_"); //close off the inner nested select
|
71 |
+
} else {
|
72 |
+
pagingSelect.append(sql.substring(startOfSelect + 6)); //add the main query
|
73 |
+
}
|
74 |
+
|
75 |
+
pagingSelect.append(" ) as temp_ where rownumber_ ");
|
76 |
+
|
77 |
+
//add the restriction to the outer select
|
78 |
+
if (offset > 0) {
|
79 |
+
// int end = offset + limit;
|
80 |
+
String endString = offsetPlaceholder + "+" + limitPlaceholder;
|
81 |
+
pagingSelect.append("between ").append(offsetPlaceholder)
|
82 |
+
.append("+1 and ").append(endString);
|
83 |
+
} else {
|
84 |
+
pagingSelect.append("<= ").append(limitPlaceholder);
|
85 |
+
}
|
86 |
+
|
87 |
+
return pagingSelect.toString();
|
88 |
+
}
|
89 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/DerbyDialect.java
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* @author poplar.yfyang
|
10 |
+
* @version 1.0 2010-10-10 下午12:31
|
11 |
+
* @since JDK 1.5
|
12 |
+
*/
|
13 |
+
public class DerbyDialect implements Dialect {
|
14 |
+
@Override
|
15 |
+
public boolean supportsLimit() {
|
16 |
+
return false;
|
17 |
+
}
|
18 |
+
|
19 |
+
@Override
|
20 |
+
public String getLimitString(String sql, int offset, int limit) {
|
21 |
+
// return getLimitString(sql,offset,Integer.toString(offset),limit,Integer.toString(limit));
|
22 |
+
throw new UnsupportedOperationException("paged queries not supported");
|
23 |
+
}
|
24 |
+
|
25 |
+
/**
|
26 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
27 |
+
* <pre>
|
28 |
+
* 如mysql
|
29 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
30 |
+
* select * from user limit :offset,:limit
|
31 |
+
* </pre>
|
32 |
+
*
|
33 |
+
* @param sql 实际SQL语句
|
34 |
+
* @param offset 分页开始纪录条数
|
35 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
36 |
+
* @param limit 分页每页显示纪录条数
|
37 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
38 |
+
* @return 包含占位符的分页sql
|
39 |
+
*/
|
40 |
+
public String getLimitString(String sql, int offset,String offsetPlaceholder, int limit, String limitPlaceholder) {
|
41 |
+
throw new UnsupportedOperationException( "paged queries not supported" );
|
42 |
+
}
|
43 |
+
|
44 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/H2Dialect.java
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* A dialect compatible with the H2 database.
|
10 |
+
*
|
11 |
+
* @author poplar.yfyang
|
12 |
+
* @version 1.0 2010-10-10 下午12:31
|
13 |
+
* @since JDK 1.5
|
14 |
+
*/
|
15 |
+
public class H2Dialect implements Dialect {
|
16 |
+
|
17 |
+
public boolean supportsLimit() {
|
18 |
+
return true;
|
19 |
+
}
|
20 |
+
|
21 |
+
/**
|
22 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
23 |
+
* <pre>
|
24 |
+
* 如mysql
|
25 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
26 |
+
* select * from user limit :offset,:limit
|
27 |
+
* </pre>
|
28 |
+
*
|
29 |
+
* @param sql 实际SQL语句
|
30 |
+
* @param offset 分页开始纪录条数
|
31 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
32 |
+
* @param limit 分页每页显示纪录条数
|
33 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
34 |
+
* @return 包含占位符的分页sql
|
35 |
+
*/
|
36 |
+
private String getLimitString(String sql, int offset, String offsetPlaceholder, int limit, String limitPlaceholder) {
|
37 |
+
return sql + ((offset > 0) ? " limit " + limitPlaceholder + " offset "
|
38 |
+
+ offsetPlaceholder : " limit " + limitPlaceholder);
|
39 |
+
}
|
40 |
+
|
41 |
+
@Override
|
42 |
+
public String getLimitString(String sql, int offset, int limit) {
|
43 |
+
return getLimitString(sql, offset, Integer.toString(offset), limit, Integer.toString(limit));
|
44 |
+
}
|
45 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/HSQLDialect.java
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* Dialect for HSQLDB
|
10 |
+
*
|
11 |
+
* @author poplar.yfyang
|
12 |
+
* @version 1.0 2010-10-10 下午12:31
|
13 |
+
* @since JDK 1.5
|
14 |
+
*/
|
15 |
+
public class HSQLDialect implements Dialect {
|
16 |
+
@Override
|
17 |
+
public boolean supportsLimit() {
|
18 |
+
return true;
|
19 |
+
}
|
20 |
+
|
21 |
+
@Override
|
22 |
+
public String getLimitString(String sql, int offset, int limit) {
|
23 |
+
return getLimitString(sql, offset, Integer.toString(offset),
|
24 |
+
Integer.toString(limit));
|
25 |
+
}
|
26 |
+
|
27 |
+
/**
|
28 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
29 |
+
* <pre>
|
30 |
+
* 如mysql
|
31 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
32 |
+
* select * from user limit :offset,:limit
|
33 |
+
* </pre>
|
34 |
+
*
|
35 |
+
* @param sql 实际SQL语句
|
36 |
+
* @param offset 分页开始纪录条数
|
37 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
38 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
39 |
+
* @return 包含占位符的分页sql
|
40 |
+
*/
|
41 |
+
public String getLimitString(String sql, int offset, String offsetPlaceholder, String limitPlaceholder) {
|
42 |
+
boolean hasOffset = offset > 0;
|
43 |
+
return
|
44 |
+
new StringBuffer(sql.length() + 10)
|
45 |
+
.append(sql)
|
46 |
+
.insert(sql.toLowerCase().indexOf("select") + 6, hasOffset ? " limit " + offsetPlaceholder + " " + limitPlaceholder : " top " + limitPlaceholder)
|
47 |
+
.toString();
|
48 |
+
}
|
49 |
+
|
50 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/MySQLDialect.java
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* Mysql方言的实现
|
10 |
+
*
|
11 |
+
* @author poplar.yfyang
|
12 |
+
* @version 1.0 2010-10-10 下午12:31
|
13 |
+
* @since JDK 1.5
|
14 |
+
*/
|
15 |
+
public class MySQLDialect implements Dialect {
|
16 |
+
|
17 |
+
|
18 |
+
@Override
|
19 |
+
public String getLimitString(String sql, int offset, int limit) {
|
20 |
+
return getLimitString(sql, offset, Integer.toString(offset),
|
21 |
+
Integer.toString(limit));
|
22 |
+
}
|
23 |
+
|
24 |
+
public boolean supportsLimit() {
|
25 |
+
return true;
|
26 |
+
}
|
27 |
+
|
28 |
+
/**
|
29 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
30 |
+
* <pre>
|
31 |
+
* 如mysql
|
32 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
33 |
+
* select * from user limit :offset,:limit
|
34 |
+
* </pre>
|
35 |
+
*
|
36 |
+
* @param sql 实际SQL语句
|
37 |
+
* @param offset 分页开始纪录条数
|
38 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
39 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
40 |
+
* @return 包含占位符的分页sql
|
41 |
+
*/
|
42 |
+
public String getLimitString(String sql, int offset, String offsetPlaceholder, String limitPlaceholder) {
|
43 |
+
StringBuilder stringBuilder = new StringBuilder(sql);
|
44 |
+
stringBuilder.append(" limit ");
|
45 |
+
if (offset > 0) {
|
46 |
+
stringBuilder.append(offsetPlaceholder).append(",").append(limitPlaceholder);
|
47 |
+
} else {
|
48 |
+
stringBuilder.append(limitPlaceholder);
|
49 |
+
}
|
50 |
+
return stringBuilder.toString();
|
51 |
+
}
|
52 |
+
|
53 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/OracleDialect.java
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* Oracle的方言实现
|
10 |
+
* @author poplar.yfyang
|
11 |
+
* @version 1.0 2010-10-10 下午12:31
|
12 |
+
* @since JDK 1.5
|
13 |
+
*/
|
14 |
+
public class OracleDialect implements Dialect {
|
15 |
+
@Override
|
16 |
+
public boolean supportsLimit() {
|
17 |
+
return true;
|
18 |
+
}
|
19 |
+
|
20 |
+
@Override
|
21 |
+
public String getLimitString(String sql, int offset, int limit) {
|
22 |
+
return getLimitString(sql, offset, Integer.toString(offset), Integer.toString(limit));
|
23 |
+
}
|
24 |
+
|
25 |
+
/**
|
26 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
27 |
+
* <pre>
|
28 |
+
* 如mysql
|
29 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
30 |
+
* select * from user limit :offset,:limit
|
31 |
+
* </pre>
|
32 |
+
*
|
33 |
+
* @param sql 实际SQL语句
|
34 |
+
* @param offset 分页开始纪录条数
|
35 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
36 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
37 |
+
* @return 包含占位符的分页sql
|
38 |
+
*/
|
39 |
+
public String getLimitString(String sql, int offset, String offsetPlaceholder, String limitPlaceholder) {
|
40 |
+
sql = sql.trim();
|
41 |
+
boolean isForUpdate = false;
|
42 |
+
if (sql.toLowerCase().endsWith(" for update")) {
|
43 |
+
sql = sql.substring(0, sql.length() - 11);
|
44 |
+
isForUpdate = true;
|
45 |
+
}
|
46 |
+
StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
|
47 |
+
|
48 |
+
if (offset > 0) {
|
49 |
+
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
|
50 |
+
} else {
|
51 |
+
pagingSelect.append("select * from ( ");
|
52 |
+
}
|
53 |
+
pagingSelect.append(sql);
|
54 |
+
if (offset > 0) {
|
55 |
+
String endString = offsetPlaceholder + "+" + limitPlaceholder;
|
56 |
+
pagingSelect.append(" ) row_ where rownum <= "+endString+") where rownum_ > ").append(offsetPlaceholder);
|
57 |
+
} else {
|
58 |
+
pagingSelect.append(" ) where rownum <= "+limitPlaceholder);
|
59 |
+
}
|
60 |
+
|
61 |
+
if (isForUpdate) {
|
62 |
+
pagingSelect.append(" for update");
|
63 |
+
}
|
64 |
+
|
65 |
+
return pagingSelect.toString();
|
66 |
+
}
|
67 |
+
|
68 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/PostgreSQLDialect.java
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* Postgre Sql的方言实现
|
10 |
+
* @author poplar.yfyang
|
11 |
+
* @version 1.0 2010-10-10 下午12:31
|
12 |
+
* @since JDK 1.5
|
13 |
+
*/
|
14 |
+
public class PostgreSQLDialect implements Dialect {
|
15 |
+
|
16 |
+
public boolean supportsLimit() {
|
17 |
+
return true;
|
18 |
+
}
|
19 |
+
|
20 |
+
@Override
|
21 |
+
public String getLimitString(String sql, int offset, int limit) {
|
22 |
+
return getLimitString(sql, offset, Integer.toString(offset),
|
23 |
+
Integer.toString(limit));
|
24 |
+
}
|
25 |
+
|
26 |
+
/**
|
27 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
28 |
+
* <pre>
|
29 |
+
* 如mysql
|
30 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
31 |
+
* select * from user limit :offset,:limit
|
32 |
+
* </pre>
|
33 |
+
*
|
34 |
+
* @param sql 实际SQL语句
|
35 |
+
* @param offset 分页开始纪录条数
|
36 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
37 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
38 |
+
* @return 包含占位符的分页sql
|
39 |
+
*/
|
40 |
+
public String getLimitString(String sql, int offset,
|
41 |
+
String offsetPlaceholder, String limitPlaceholder) {
|
42 |
+
StringBuilder pageSql = new StringBuilder().append(sql);
|
43 |
+
pageSql = offset <= 0
|
44 |
+
? pageSql.append(" limit ").append(limitPlaceholder) :
|
45 |
+
pageSql.append(" limit ").append(limitPlaceholder).append(" offset ").append(offsetPlaceholder);
|
46 |
+
return pageSql.toString();
|
47 |
+
}
|
48 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServer2005Dialect.java
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import org.apache.commons.lang3.StringUtils;
|
7 |
+
|
8 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
9 |
+
|
10 |
+
/**
|
11 |
+
* Sql 2005的方言实现
|
12 |
+
* @author poplar.yfyang
|
13 |
+
* @version 1.0 2010-10-10 下午12:31
|
14 |
+
* @since JDK 1.5
|
15 |
+
*/
|
16 |
+
public class SQLServer2005Dialect implements Dialect {
|
17 |
+
|
18 |
+
@Override
|
19 |
+
public boolean supportsLimit() {
|
20 |
+
return true;
|
21 |
+
}
|
22 |
+
|
23 |
+
@Override
|
24 |
+
public String getLimitString(String sql, int offset, int limit) {
|
25 |
+
return getLimitString(sql, offset,
|
26 |
+
limit, Integer.toString(limit));
|
27 |
+
}
|
28 |
+
|
29 |
+
/**
|
30 |
+
* Add a LIMIT clause to the given SQL SELECT
|
31 |
+
* <p/>
|
32 |
+
* The LIMIT SQL will look like:
|
33 |
+
* <p/>
|
34 |
+
* WITH query AS
|
35 |
+
* (SELECT TOP 100 percent ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __row_number__, * from table_name)
|
36 |
+
* SELECT *
|
37 |
+
* FROM query
|
38 |
+
* WHERE __row_number__ BETWEEN :offset and :lastRows
|
39 |
+
* ORDER BY __row_number__
|
40 |
+
*
|
41 |
+
* @param querySqlString The SQL statement to base the limit query off of.
|
42 |
+
* @param offset Offset of the first row to be returned by the query (zero-based)
|
43 |
+
* @param limit Maximum number of rows to be returned by the query
|
44 |
+
* @param limitPlaceholder limitPlaceholder
|
45 |
+
* @return A new SQL statement with the LIMIT clause applied.
|
46 |
+
*/
|
47 |
+
private String getLimitString(String querySqlString, int offset, int limit, String limitPlaceholder) {
|
48 |
+
StringBuilder pagingBuilder = new StringBuilder();
|
49 |
+
String orderby = getOrderByPart(querySqlString);
|
50 |
+
String distinctStr = "";
|
51 |
+
|
52 |
+
String loweredString = querySqlString.toLowerCase();
|
53 |
+
String sqlPartString = querySqlString;
|
54 |
+
if (loweredString.trim().startsWith("select")) {
|
55 |
+
int index = 6;
|
56 |
+
if (loweredString.startsWith("select distinct")) {
|
57 |
+
distinctStr = "DISTINCT ";
|
58 |
+
index = 15;
|
59 |
+
}
|
60 |
+
sqlPartString = sqlPartString.substring(index);
|
61 |
+
}
|
62 |
+
pagingBuilder.append(sqlPartString);
|
63 |
+
|
64 |
+
// if no ORDER BY is specified use fake ORDER BY field to avoid errors
|
65 |
+
if (StringUtils.isEmpty(orderby)) {
|
66 |
+
orderby = "ORDER BY CURRENT_TIMESTAMP";
|
67 |
+
}
|
68 |
+
|
69 |
+
StringBuilder result = new StringBuilder();
|
70 |
+
result.append("WITH query AS (SELECT ")
|
71 |
+
.append(distinctStr)
|
72 |
+
.append("TOP 100 PERCENT ")
|
73 |
+
.append(" ROW_NUMBER() OVER (")
|
74 |
+
.append(orderby)
|
75 |
+
.append(") as __row_number__, ")
|
76 |
+
.append(pagingBuilder)
|
77 |
+
.append(") SELECT * FROM query WHERE __row_number__ BETWEEN ")
|
78 |
+
.append(offset + 1).append(" AND ").append(offset + limit)
|
79 |
+
.append(" ORDER BY __row_number__");
|
80 |
+
|
81 |
+
return result.toString();
|
82 |
+
}
|
83 |
+
|
84 |
+
static String getOrderByPart(String sql) {
|
85 |
+
String loweredString = sql.toLowerCase();
|
86 |
+
int orderByIndex = loweredString.indexOf("order by");
|
87 |
+
if (orderByIndex != -1) {
|
88 |
+
// if we find a new "order by" then we need to ignore
|
89 |
+
// the previous one since it was probably used for a subquery
|
90 |
+
return sql.substring(orderByIndex);
|
91 |
+
} else {
|
92 |
+
return "";
|
93 |
+
}
|
94 |
+
}
|
95 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SQLServerDialect.java
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* MSSQLServer 数据库实现分页方言
|
10 |
+
*
|
11 |
+
* @author poplar.yfyang
|
12 |
+
* @version 1.0 2010-10-10 下午12:31
|
13 |
+
* @since JDK 1.5
|
14 |
+
*/
|
15 |
+
public class SQLServerDialect implements Dialect {
|
16 |
+
|
17 |
+
public boolean supportsLimit() {
|
18 |
+
return true;
|
19 |
+
}
|
20 |
+
|
21 |
+
static int getAfterSelectInsertPoint(String sql) {
|
22 |
+
int selectIndex = sql.toLowerCase().indexOf("select");
|
23 |
+
final int selectDistinctIndex = sql.toLowerCase().indexOf("select distinct");
|
24 |
+
return selectIndex + (selectDistinctIndex == selectIndex ? 15 : 6);
|
25 |
+
}
|
26 |
+
|
27 |
+
public String getLimitString(String sql, int offset, int limit) {
|
28 |
+
return getLimit(sql, offset, limit);
|
29 |
+
}
|
30 |
+
|
31 |
+
/**
|
32 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
33 |
+
* <pre>
|
34 |
+
* 如mysql
|
35 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
36 |
+
* select * from user limit :offset,:limit
|
37 |
+
* </pre>
|
38 |
+
*
|
39 |
+
* @param sql 实际SQL语句
|
40 |
+
* @param offset 分页开始纪录条数
|
41 |
+
* @param limit 分页每页显示纪录条数
|
42 |
+
* @return 包含占位符的分页sql
|
43 |
+
*/
|
44 |
+
public String getLimit(String sql, int offset, int limit) {
|
45 |
+
if (offset > 0) {
|
46 |
+
throw new UnsupportedOperationException("sql server has no offset");
|
47 |
+
}
|
48 |
+
return new StringBuffer(sql.length() + 8)
|
49 |
+
.append(sql)
|
50 |
+
.insert(getAfterSelectInsertPoint(sql), " top " + limit)
|
51 |
+
.toString();
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/dialect/db/SybaseDialect.java
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.dialect.db;
|
5 |
+
|
6 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
7 |
+
|
8 |
+
/**
|
9 |
+
* Sybase数据库分页方言实现。
|
10 |
+
* 还未实现
|
11 |
+
*
|
12 |
+
* @author poplar.yfyang
|
13 |
+
* @version 1.0 2010-10-10 下午12:31
|
14 |
+
* @since JDK 1.5
|
15 |
+
*/
|
16 |
+
public class SybaseDialect implements Dialect {
|
17 |
+
|
18 |
+
public boolean supportsLimit() {
|
19 |
+
return false;
|
20 |
+
}
|
21 |
+
|
22 |
+
|
23 |
+
@Override
|
24 |
+
public String getLimitString(String sql, int offset, int limit) {
|
25 |
+
return null;
|
26 |
+
}
|
27 |
+
|
28 |
+
/**
|
29 |
+
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
|
30 |
+
* <pre>
|
31 |
+
* 如mysql
|
32 |
+
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
|
33 |
+
* select * from user limit :offset,:limit
|
34 |
+
* </pre>
|
35 |
+
*
|
36 |
+
* @param sql 实际SQL语句
|
37 |
+
* @param offset 分页开始纪录条数
|
38 |
+
* @param offsetPlaceholder 分页开始纪录条数-占位符号
|
39 |
+
* @param limit 分页每页显示纪录条数
|
40 |
+
* @param limitPlaceholder 分页纪录条数占位符号
|
41 |
+
* @return 包含占位符的分页sql
|
42 |
+
*/
|
43 |
+
public String getLimitString(String sql, int offset, String offsetPlaceholder, int limit, String limitPlaceholder) {
|
44 |
+
throw new UnsupportedOperationException("paged queries not supported");
|
45 |
+
}
|
46 |
+
|
47 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/BaseInterceptor.java
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.interceptor;
|
5 |
+
|
6 |
+
import org.apache.ibatis.logging.Log;
|
7 |
+
import org.apache.ibatis.logging.LogFactory;
|
8 |
+
import org.apache.ibatis.plugin.Interceptor;
|
9 |
+
|
10 |
+
import com.thinkgem.jeesite.common.config.Global;
|
11 |
+
import com.thinkgem.jeesite.common.persistence.Page;
|
12 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
13 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.DB2Dialect;
|
14 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.DerbyDialect;
|
15 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.H2Dialect;
|
16 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.HSQLDialect;
|
17 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.MySQLDialect;
|
18 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.OracleDialect;
|
19 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.PostgreSQLDialect;
|
20 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.SQLServer2005Dialect;
|
21 |
+
import com.thinkgem.jeesite.common.persistence.dialect.db.SybaseDialect;
|
22 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
23 |
+
|
24 |
+
import java.io.Serializable;
|
25 |
+
import java.util.Properties;
|
26 |
+
|
27 |
+
/**
|
28 |
+
* Mybatis分页拦截器基类
|
29 |
+
* @author poplar.yfyang / thinkgem
|
30 |
+
* @version 2013-8-28
|
31 |
+
*/
|
32 |
+
public abstract class BaseInterceptor implements Interceptor, Serializable {
|
33 |
+
|
34 |
+
private static final long serialVersionUID = 1L;
|
35 |
+
|
36 |
+
protected static final String PAGE = "page";
|
37 |
+
|
38 |
+
protected static final String DELEGATE = "delegate";
|
39 |
+
|
40 |
+
protected static final String MAPPED_STATEMENT = "mappedStatement";
|
41 |
+
|
42 |
+
protected Log log = LogFactory.getLog(this.getClass());
|
43 |
+
|
44 |
+
protected Dialect DIALECT;
|
45 |
+
|
46 |
+
// /**
|
47 |
+
// * 拦截的ID,在mapper中的id,可以匹配正则
|
48 |
+
// */
|
49 |
+
// protected String _SQL_PATTERN = "";
|
50 |
+
|
51 |
+
/**
|
52 |
+
* 对参数进行转换和检查
|
53 |
+
* @param parameterObject 参数对象
|
54 |
+
* @param page 分页对象
|
55 |
+
* @return 分页对象
|
56 |
+
* @throws NoSuchFieldException 无法找到参数
|
57 |
+
*/
|
58 |
+
@SuppressWarnings("unchecked")
|
59 |
+
protected static Page<Object> convertParameter(Object parameterObject, Page<Object> page) {
|
60 |
+
try{
|
61 |
+
if (parameterObject instanceof Page) {
|
62 |
+
return (Page<Object>) parameterObject;
|
63 |
+
} else {
|
64 |
+
return (Page<Object>)Reflections.getFieldValue(parameterObject, PAGE);
|
65 |
+
}
|
66 |
+
}catch (Exception e) {
|
67 |
+
return null;
|
68 |
+
}
|
69 |
+
}
|
70 |
+
|
71 |
+
/**
|
72 |
+
* 设置属性,支持自定义方言类和制定数据库的方式
|
73 |
+
* <code>dialectClass</code>,自定义方言类。可以不配置这项
|
74 |
+
* <ode>dbms</ode> 数据库类型,插件支持的数据库
|
75 |
+
* <code>sqlPattern</code> 需要拦截的SQL ID
|
76 |
+
* @param p 属性
|
77 |
+
*/
|
78 |
+
protected void initProperties(Properties p) {
|
79 |
+
Dialect dialect = null;
|
80 |
+
String dbType = Global.getConfig("jdbc.type");
|
81 |
+
if ("db2".equals(dbType)){
|
82 |
+
dialect = new DB2Dialect();
|
83 |
+
}else if("derby".equals(dbType)){
|
84 |
+
dialect = new DerbyDialect();
|
85 |
+
}else if("h2".equals(dbType)){
|
86 |
+
dialect = new H2Dialect();
|
87 |
+
}else if("hsql".equals(dbType)){
|
88 |
+
dialect = new HSQLDialect();
|
89 |
+
}else if("mysql".equals(dbType)){
|
90 |
+
dialect = new MySQLDialect();
|
91 |
+
}else if("oracle".equals(dbType)){
|
92 |
+
dialect = new OracleDialect();
|
93 |
+
}else if("postgre".equals(dbType)){
|
94 |
+
dialect = new PostgreSQLDialect();
|
95 |
+
}else if("mssql".equals(dbType) || "sqlserver".equals(dbType)){
|
96 |
+
dialect = new SQLServer2005Dialect();
|
97 |
+
}else if("sybase".equals(dbType)){
|
98 |
+
dialect = new SybaseDialect();
|
99 |
+
}
|
100 |
+
if (dialect == null) {
|
101 |
+
throw new RuntimeException("mybatis dialect error.");
|
102 |
+
}
|
103 |
+
DIALECT = dialect;
|
104 |
+
// _SQL_PATTERN = p.getProperty("sqlPattern");
|
105 |
+
// _SQL_PATTERN = Global.getConfig("mybatis.pagePattern");
|
106 |
+
// if (StringUtils.isEmpty(_SQL_PATTERN)) {
|
107 |
+
// throw new RuntimeException("sqlPattern property is not found!");
|
108 |
+
// }
|
109 |
+
}
|
110 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/PaginationInterceptor.java
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.interceptor;
|
5 |
+
|
6 |
+
import org.apache.ibatis.executor.Executor;
|
7 |
+
import org.apache.ibatis.mapping.BoundSql;
|
8 |
+
import org.apache.ibatis.mapping.MappedStatement;
|
9 |
+
import org.apache.ibatis.mapping.SqlSource;
|
10 |
+
import org.apache.ibatis.plugin.Intercepts;
|
11 |
+
import org.apache.ibatis.plugin.Invocation;
|
12 |
+
import org.apache.ibatis.plugin.Plugin;
|
13 |
+
import org.apache.ibatis.plugin.Signature;
|
14 |
+
import org.apache.ibatis.reflection.MetaObject;
|
15 |
+
import org.apache.ibatis.session.ResultHandler;
|
16 |
+
import org.apache.ibatis.session.RowBounds;
|
17 |
+
|
18 |
+
import com.thinkgem.jeesite.common.persistence.Page;
|
19 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
20 |
+
import com.thinkgem.jeesite.common.utils.StringUtils;
|
21 |
+
|
22 |
+
import java.util.Properties;
|
23 |
+
|
24 |
+
/**
|
25 |
+
* 数据库分页插件,只拦截查询语句.
|
26 |
+
* @author poplar.yfyang / thinkgem
|
27 |
+
* @version 2013-8-28
|
28 |
+
*/
|
29 |
+
@Intercepts({@Signature(type = Executor.class, method = "query",
|
30 |
+
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
|
31 |
+
public class PaginationInterceptor extends BaseInterceptor {
|
32 |
+
|
33 |
+
private static final long serialVersionUID = 1L;
|
34 |
+
|
35 |
+
@Override
|
36 |
+
public Object intercept(Invocation invocation) throws Throwable {
|
37 |
+
|
38 |
+
final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
|
39 |
+
|
40 |
+
// //拦截需要分页的SQL
|
41 |
+
//// if (mappedStatement.getId().matches(_SQL_PATTERN)) {
|
42 |
+
// if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
|
43 |
+
Object parameter = invocation.getArgs()[1];
|
44 |
+
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
|
45 |
+
Object parameterObject = boundSql.getParameterObject();
|
46 |
+
|
47 |
+
//获取分页参数对象
|
48 |
+
Page<Object> page = null;
|
49 |
+
if (parameterObject != null) {
|
50 |
+
page = convertParameter(parameterObject, page);
|
51 |
+
}
|
52 |
+
|
53 |
+
//如果设置了分页对象,则进行分页
|
54 |
+
if (page != null && page.getPageSize() != -1) {
|
55 |
+
|
56 |
+
if (StringUtils.isBlank(boundSql.getSql())){
|
57 |
+
return null;
|
58 |
+
}
|
59 |
+
String originalSql = boundSql.getSql().trim();
|
60 |
+
|
61 |
+
//得到总记录数
|
62 |
+
page.setCount(SQLHelper.getCount(originalSql, null, mappedStatement, parameterObject, boundSql, log));
|
63 |
+
|
64 |
+
//分页查询 本地化对象 修改数据库注意修改实现
|
65 |
+
String pageSql = SQLHelper.generatePageSql(originalSql, page, DIALECT);
|
66 |
+
// if (log.isDebugEnabled()) {
|
67 |
+
// log.debug("PAGE SQL:" + StringUtils.replace(pageSql, "\n", ""));
|
68 |
+
// }
|
69 |
+
invocation.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
|
70 |
+
BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql, boundSql.getParameterMappings(), boundSql.getParameterObject());
|
71 |
+
//解决MyBatis 分页foreach 参数失效 start
|
72 |
+
if (Reflections.getFieldValue(boundSql, "metaParameters") != null) {
|
73 |
+
MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");
|
74 |
+
Reflections.setFieldValue(newBoundSql, "metaParameters", mo);
|
75 |
+
}
|
76 |
+
//解决MyBatis 分页foreach 参数失效 end
|
77 |
+
MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
|
78 |
+
|
79 |
+
invocation.getArgs()[0] = newMs;
|
80 |
+
}
|
81 |
+
// }
|
82 |
+
return invocation.proceed();
|
83 |
+
}
|
84 |
+
|
85 |
+
|
86 |
+
@Override
|
87 |
+
public Object plugin(Object target) {
|
88 |
+
return Plugin.wrap(target, this);
|
89 |
+
}
|
90 |
+
|
91 |
+
@Override
|
92 |
+
public void setProperties(Properties properties) {
|
93 |
+
super.initProperties(properties);
|
94 |
+
}
|
95 |
+
|
96 |
+
private MappedStatement copyFromMappedStatement(MappedStatement ms,
|
97 |
+
SqlSource newSqlSource) {
|
98 |
+
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(),
|
99 |
+
ms.getId(), newSqlSource, ms.getSqlCommandType());
|
100 |
+
builder.resource(ms.getResource());
|
101 |
+
builder.fetchSize(ms.getFetchSize());
|
102 |
+
builder.statementType(ms.getStatementType());
|
103 |
+
builder.keyGenerator(ms.getKeyGenerator());
|
104 |
+
if (ms.getKeyProperties() != null) {
|
105 |
+
for (String keyProperty : ms.getKeyProperties()) {
|
106 |
+
builder.keyProperty(keyProperty);
|
107 |
+
}
|
108 |
+
}
|
109 |
+
builder.timeout(ms.getTimeout());
|
110 |
+
builder.parameterMap(ms.getParameterMap());
|
111 |
+
builder.resultMaps(ms.getResultMaps());
|
112 |
+
builder.cache(ms.getCache());
|
113 |
+
builder.useCache(ms.isUseCache());
|
114 |
+
return builder.build();
|
115 |
+
}
|
116 |
+
|
117 |
+
public static class BoundSqlSqlSource implements SqlSource {
|
118 |
+
BoundSql boundSql;
|
119 |
+
|
120 |
+
public BoundSqlSqlSource(BoundSql boundSql) {
|
121 |
+
this.boundSql = boundSql;
|
122 |
+
}
|
123 |
+
|
124 |
+
public BoundSql getBoundSql(Object parameterObject) {
|
125 |
+
return boundSql;
|
126 |
+
}
|
127 |
+
}
|
128 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/PreparePaginationInterceptor.java
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.interceptor;
|
5 |
+
|
6 |
+
import org.apache.ibatis.executor.statement.BaseStatementHandler;
|
7 |
+
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
|
8 |
+
import org.apache.ibatis.executor.statement.StatementHandler;
|
9 |
+
import org.apache.ibatis.mapping.BoundSql;
|
10 |
+
import org.apache.ibatis.mapping.MappedStatement;
|
11 |
+
import org.apache.ibatis.plugin.Intercepts;
|
12 |
+
import org.apache.ibatis.plugin.Invocation;
|
13 |
+
import org.apache.ibatis.plugin.Plugin;
|
14 |
+
import org.apache.ibatis.plugin.Signature;
|
15 |
+
|
16 |
+
import com.thinkgem.jeesite.common.persistence.Page;
|
17 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
18 |
+
|
19 |
+
import java.sql.Connection;
|
20 |
+
import java.util.Properties;
|
21 |
+
|
22 |
+
/**
|
23 |
+
* Mybatis数据库分页插件,拦截StatementHandler的prepare方法
|
24 |
+
* @author poplar.yfyang / thinkgem
|
25 |
+
* @version 2013-8-28
|
26 |
+
*/
|
27 |
+
@Intercepts({
|
28 |
+
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})
|
29 |
+
})
|
30 |
+
public class PreparePaginationInterceptor extends BaseInterceptor {
|
31 |
+
|
32 |
+
private static final long serialVersionUID = 1L;
|
33 |
+
|
34 |
+
public PreparePaginationInterceptor() {
|
35 |
+
super();
|
36 |
+
}
|
37 |
+
|
38 |
+
@Override
|
39 |
+
public Object intercept(Invocation ivk) throws Throwable {
|
40 |
+
if (ivk.getTarget().getClass().isAssignableFrom(RoutingStatementHandler.class)) {
|
41 |
+
final RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
|
42 |
+
final BaseStatementHandler delegate = (BaseStatementHandler) Reflections.getFieldValue(statementHandler, DELEGATE);
|
43 |
+
final MappedStatement mappedStatement = (MappedStatement) Reflections.getFieldValue(delegate, MAPPED_STATEMENT);
|
44 |
+
|
45 |
+
// //拦截需要分页的SQL
|
46 |
+
//// if (mappedStatement.getId().matches(_SQL_PATTERN)) {
|
47 |
+
// if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
|
48 |
+
BoundSql boundSql = delegate.getBoundSql();
|
49 |
+
//分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
|
50 |
+
Object parameterObject = boundSql.getParameterObject();
|
51 |
+
if (parameterObject == null) {
|
52 |
+
log.error("参数未实例化");
|
53 |
+
throw new NullPointerException("parameterObject尚未实例化!");
|
54 |
+
} else {
|
55 |
+
final Connection connection = (Connection) ivk.getArgs()[0];
|
56 |
+
final String sql = boundSql.getSql();
|
57 |
+
//记录统计
|
58 |
+
final int count = SQLHelper.getCount(sql, connection, mappedStatement, parameterObject, boundSql, log);
|
59 |
+
Page<Object> page = null;
|
60 |
+
page = convertParameter(parameterObject, page);
|
61 |
+
page.setCount(count);
|
62 |
+
String pagingSql = SQLHelper.generatePageSql(sql, page, DIALECT);
|
63 |
+
if (log.isDebugEnabled()) {
|
64 |
+
log.debug("PAGE SQL:" + pagingSql);
|
65 |
+
}
|
66 |
+
//将分页sql语句反射回BoundSql.
|
67 |
+
Reflections.setFieldValue(boundSql, "sql", pagingSql);
|
68 |
+
}
|
69 |
+
|
70 |
+
if (boundSql.getSql() == null || "".equals(boundSql.getSql())){
|
71 |
+
return null;
|
72 |
+
}
|
73 |
+
|
74 |
+
}
|
75 |
+
// }
|
76 |
+
return ivk.proceed();
|
77 |
+
}
|
78 |
+
|
79 |
+
|
80 |
+
@Override
|
81 |
+
public Object plugin(Object o) {
|
82 |
+
return Plugin.wrap(o, this);
|
83 |
+
}
|
84 |
+
|
85 |
+
@Override
|
86 |
+
public void setProperties(Properties properties) {
|
87 |
+
initProperties(properties);
|
88 |
+
}
|
89 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/interceptor/SQLHelper.java
ADDED
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.interceptor;
|
5 |
+
|
6 |
+
import org.apache.ibatis.executor.ErrorContext;
|
7 |
+
import org.apache.ibatis.executor.ExecutorException;
|
8 |
+
import org.apache.ibatis.logging.Log;
|
9 |
+
import org.apache.ibatis.mapping.BoundSql;
|
10 |
+
import org.apache.ibatis.mapping.MappedStatement;
|
11 |
+
import org.apache.ibatis.mapping.ParameterMapping;
|
12 |
+
import org.apache.ibatis.mapping.ParameterMode;
|
13 |
+
import org.apache.ibatis.reflection.MetaObject;
|
14 |
+
import org.apache.ibatis.reflection.property.PropertyTokenizer;
|
15 |
+
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
|
16 |
+
import org.apache.ibatis.session.Configuration;
|
17 |
+
import org.apache.ibatis.type.TypeHandler;
|
18 |
+
import org.apache.ibatis.type.TypeHandlerRegistry;
|
19 |
+
|
20 |
+
import com.thinkgem.jeesite.common.config.Global;
|
21 |
+
import com.thinkgem.jeesite.common.persistence.Page;
|
22 |
+
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
|
23 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
24 |
+
import com.thinkgem.jeesite.common.utils.StringUtils;
|
25 |
+
|
26 |
+
import java.sql.Connection;
|
27 |
+
import java.sql.PreparedStatement;
|
28 |
+
import java.sql.ResultSet;
|
29 |
+
import java.sql.SQLException;
|
30 |
+
import java.util.List;
|
31 |
+
import java.util.regex.Matcher;
|
32 |
+
import java.util.regex.Pattern;
|
33 |
+
|
34 |
+
/**
|
35 |
+
* SQL工具类
|
36 |
+
* @author poplar.yfyang / thinkgem
|
37 |
+
* @version 2013-8-28
|
38 |
+
*/
|
39 |
+
public class SQLHelper {
|
40 |
+
|
41 |
+
/**
|
42 |
+
* 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler
|
43 |
+
*
|
44 |
+
* @param ps 表示预编译的 SQL 语句的对象。
|
45 |
+
* @param mappedStatement MappedStatement
|
46 |
+
* @param boundSql SQL
|
47 |
+
* @param parameterObject 参数对象
|
48 |
+
* @throws java.sql.SQLException 数据库异常
|
49 |
+
*/
|
50 |
+
@SuppressWarnings("unchecked")
|
51 |
+
public static void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException {
|
52 |
+
ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
|
53 |
+
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
|
54 |
+
if (parameterMappings != null) {
|
55 |
+
Configuration configuration = mappedStatement.getConfiguration();
|
56 |
+
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
|
57 |
+
MetaObject metaObject = parameterObject == null ? null :
|
58 |
+
configuration.newMetaObject(parameterObject);
|
59 |
+
for (int i = 0; i < parameterMappings.size(); i++) {
|
60 |
+
ParameterMapping parameterMapping = parameterMappings.get(i);
|
61 |
+
if (parameterMapping.getMode() != ParameterMode.OUT) {
|
62 |
+
Object value;
|
63 |
+
String propertyName = parameterMapping.getProperty();
|
64 |
+
PropertyTokenizer prop = new PropertyTokenizer(propertyName);
|
65 |
+
if (parameterObject == null) {
|
66 |
+
value = null;
|
67 |
+
} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
|
68 |
+
value = parameterObject;
|
69 |
+
} else if (boundSql.hasAdditionalParameter(propertyName)) {
|
70 |
+
value = boundSql.getAdditionalParameter(propertyName);
|
71 |
+
} else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) {
|
72 |
+
value = boundSql.getAdditionalParameter(prop.getName());
|
73 |
+
if (value != null) {
|
74 |
+
value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
|
75 |
+
}
|
76 |
+
} else {
|
77 |
+
value = metaObject == null ? null : metaObject.getValue(propertyName);
|
78 |
+
}
|
79 |
+
@SuppressWarnings("rawtypes")
|
80 |
+
TypeHandler typeHandler = parameterMapping.getTypeHandler();
|
81 |
+
if (typeHandler == null) {
|
82 |
+
throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
|
83 |
+
}
|
84 |
+
typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType());
|
85 |
+
}
|
86 |
+
}
|
87 |
+
}
|
88 |
+
}
|
89 |
+
|
90 |
+
|
91 |
+
/**
|
92 |
+
* 查询总纪录数
|
93 |
+
* @param sql SQL语句
|
94 |
+
* @param connection 数据库连接
|
95 |
+
* @param mappedStatement mapped
|
96 |
+
* @param parameterObject 参数
|
97 |
+
* @param boundSql boundSql
|
98 |
+
* @return 总记录数
|
99 |
+
* @throws SQLException sql查询错误
|
100 |
+
*/
|
101 |
+
public static int getCount(final String sql, final Connection connection,
|
102 |
+
final MappedStatement mappedStatement, final Object parameterObject,
|
103 |
+
final BoundSql boundSql, Log log) throws SQLException {
|
104 |
+
String dbName = Global.getConfig("jdbc.type");
|
105 |
+
final String countSql;
|
106 |
+
if("oracle".equals(dbName)){
|
107 |
+
countSql = "select count(1) from (" + sql + ") tmp_count";
|
108 |
+
}else{
|
109 |
+
countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";
|
110 |
+
// countSql = "select count(1) " + removeSelect(removeOrders(sql));
|
111 |
+
}
|
112 |
+
Connection conn = connection;
|
113 |
+
PreparedStatement ps = null;
|
114 |
+
ResultSet rs = null;
|
115 |
+
try {
|
116 |
+
if (log.isDebugEnabled()) {
|
117 |
+
log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[]{"\n","\t"}, new String[]{" "," "}));
|
118 |
+
}
|
119 |
+
if (conn == null){
|
120 |
+
conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
|
121 |
+
}
|
122 |
+
ps = conn.prepareStatement(countSql);
|
123 |
+
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
|
124 |
+
boundSql.getParameterMappings(), parameterObject);
|
125 |
+
//解决MyBatis 分页foreach 参数失效 start
|
126 |
+
if (Reflections.getFieldValue(boundSql, "metaParameters") != null) {
|
127 |
+
MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");
|
128 |
+
Reflections.setFieldValue(countBS, "metaParameters", mo);
|
129 |
+
}
|
130 |
+
//解决MyBatis 分页foreach 参数失效 end
|
131 |
+
SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);
|
132 |
+
rs = ps.executeQuery();
|
133 |
+
int count = 0;
|
134 |
+
if (rs.next()) {
|
135 |
+
count = rs.getInt(1);
|
136 |
+
}
|
137 |
+
return count;
|
138 |
+
} finally {
|
139 |
+
if (rs != null) {
|
140 |
+
rs.close();
|
141 |
+
}
|
142 |
+
if (ps != null) {
|
143 |
+
ps.close();
|
144 |
+
}
|
145 |
+
if (conn != null) {
|
146 |
+
conn.close();
|
147 |
+
}
|
148 |
+
}
|
149 |
+
}
|
150 |
+
|
151 |
+
|
152 |
+
/**
|
153 |
+
* 根据数据库方言,生成特定的分页sql
|
154 |
+
* @param sql Mapper中的Sql语句
|
155 |
+
* @param page 分页对象
|
156 |
+
* @param dialect 方言类型
|
157 |
+
* @return 分页SQL
|
158 |
+
*/
|
159 |
+
public static String generatePageSql(String sql, Page<Object> page, Dialect dialect) {
|
160 |
+
if (dialect.supportsLimit()) {
|
161 |
+
return dialect.getLimitString(sql, page.getFirstResult(), page.getMaxResults());
|
162 |
+
} else {
|
163 |
+
return sql;
|
164 |
+
}
|
165 |
+
}
|
166 |
+
|
167 |
+
/**
|
168 |
+
* 去除qlString的select子句。
|
169 |
+
* @param hql
|
170 |
+
* @return
|
171 |
+
*/
|
172 |
+
@SuppressWarnings("unused")
|
173 |
+
private static String removeSelect(String qlString){
|
174 |
+
int beginPos = qlString.toLowerCase().indexOf("from");
|
175 |
+
return qlString.substring(beginPos);
|
176 |
+
}
|
177 |
+
|
178 |
+
/**
|
179 |
+
* 去除hql的orderBy子句。
|
180 |
+
* @param hql
|
181 |
+
* @return
|
182 |
+
*/
|
183 |
+
@SuppressWarnings("unused")
|
184 |
+
private static String removeOrders(String qlString) {
|
185 |
+
Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
|
186 |
+
Matcher m = p.matcher(qlString);
|
187 |
+
StringBuffer sb = new StringBuffer();
|
188 |
+
while (m.find()) {
|
189 |
+
m.appendReplacement(sb, "");
|
190 |
+
}
|
191 |
+
m.appendTail(sb);
|
192 |
+
return sb.toString();
|
193 |
+
}
|
194 |
+
|
195 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PageConfiguration.java
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.proxy;
|
5 |
+
|
6 |
+
import org.apache.ibatis.binding.MapperRegistry;
|
7 |
+
import org.apache.ibatis.session.Configuration;
|
8 |
+
import org.apache.ibatis.session.SqlSession;
|
9 |
+
|
10 |
+
/**
|
11 |
+
* <p>
|
12 |
+
* 自定义Mybatis的配置,扩展.
|
13 |
+
* </p>
|
14 |
+
*
|
15 |
+
* @author poplar.yfyang
|
16 |
+
* @version 1.0 2012-05-13 上午10:06
|
17 |
+
* @since JDK 1.5
|
18 |
+
*/
|
19 |
+
public class PageConfiguration extends Configuration {
|
20 |
+
|
21 |
+
protected MapperRegistry mapperRegistry = new PaginationMapperRegistry(this);
|
22 |
+
|
23 |
+
@Override
|
24 |
+
public <T> void addMapper(Class<T> type) {
|
25 |
+
mapperRegistry.addMapper(type);
|
26 |
+
}
|
27 |
+
|
28 |
+
@Override
|
29 |
+
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
|
30 |
+
return mapperRegistry.getMapper(type, sqlSession);
|
31 |
+
}
|
32 |
+
|
33 |
+
@Override
|
34 |
+
public boolean hasMapper(Class<?> type) {
|
35 |
+
return mapperRegistry.hasMapper(type);
|
36 |
+
}
|
37 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PaginationMapperMethod.java
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.proxy;
|
5 |
+
|
6 |
+
import org.apache.ibatis.annotations.Param;
|
7 |
+
import org.apache.ibatis.binding.BindingException;
|
8 |
+
import org.apache.ibatis.mapping.MappedStatement;
|
9 |
+
import org.apache.ibatis.mapping.SqlCommandType;
|
10 |
+
import org.apache.ibatis.session.Configuration;
|
11 |
+
import org.apache.ibatis.session.RowBounds;
|
12 |
+
import org.apache.ibatis.session.SqlSession;
|
13 |
+
|
14 |
+
import com.thinkgem.jeesite.common.persistence.Page;
|
15 |
+
|
16 |
+
import java.lang.reflect.Method;
|
17 |
+
import java.util.ArrayList;
|
18 |
+
import java.util.HashMap;
|
19 |
+
import java.util.List;
|
20 |
+
import java.util.Map;
|
21 |
+
|
22 |
+
/**
|
23 |
+
* <p>
|
24 |
+
* 执行代理类,扩展Mybatis的方式来让其Mapper接口来支持.
|
25 |
+
* </p>
|
26 |
+
*
|
27 |
+
* @author poplar.yfyang
|
28 |
+
* @version 1.0 2012-05-13 上午10:09
|
29 |
+
* @since JDK 1.5
|
30 |
+
*/
|
31 |
+
public class PaginationMapperMethod {
|
32 |
+
|
33 |
+
private final SqlSession sqlSession;
|
34 |
+
private final Configuration config;
|
35 |
+
|
36 |
+
private SqlCommandType type;
|
37 |
+
private String commandName;
|
38 |
+
private String commandCountName;
|
39 |
+
|
40 |
+
private final Class<?> declaringInterface;
|
41 |
+
private final Method method;
|
42 |
+
|
43 |
+
private Integer rowBoundsIndex;
|
44 |
+
private Integer paginationIndex;
|
45 |
+
|
46 |
+
private final List<String> paramNames;
|
47 |
+
private final List<Integer> paramPositions;
|
48 |
+
|
49 |
+
private boolean hasNamedParameters;
|
50 |
+
|
51 |
+
public PaginationMapperMethod(Class<?> declaringInterface, Method method,
|
52 |
+
SqlSession sqlSession) {
|
53 |
+
paramNames = new ArrayList<String>();
|
54 |
+
paramPositions = new ArrayList<Integer>();
|
55 |
+
this.sqlSession = sqlSession;
|
56 |
+
this.method = method;
|
57 |
+
this.config = sqlSession.getConfiguration();
|
58 |
+
this.declaringInterface = declaringInterface;
|
59 |
+
this.hasNamedParameters = false;
|
60 |
+
setupFields();
|
61 |
+
setupMethodSignature();
|
62 |
+
setupCommandType();
|
63 |
+
validateStatement();
|
64 |
+
}
|
65 |
+
|
66 |
+
/**
|
67 |
+
* 代理执行方法。
|
68 |
+
*
|
69 |
+
* @param args 参数信息
|
70 |
+
* @return 执行结果
|
71 |
+
*/
|
72 |
+
@SuppressWarnings("unchecked")
|
73 |
+
public Object execute(Object[] args) {
|
74 |
+
final Object param = getParam(args);
|
75 |
+
Page<Object> page;
|
76 |
+
RowBounds rowBounds;
|
77 |
+
if (paginationIndex != null) {
|
78 |
+
page = (Page<Object>) args[paginationIndex];
|
79 |
+
rowBounds = new RowBounds(page.getFirstResult(), page.getMaxResults());
|
80 |
+
} else if (rowBoundsIndex != null) {
|
81 |
+
rowBounds = (RowBounds) args[rowBoundsIndex];
|
82 |
+
page = new Page<Object>();
|
83 |
+
} else {
|
84 |
+
throw new BindingException("Invalid bound statement (not found rowBounds or pagination in paramenters)");
|
85 |
+
}
|
86 |
+
page.setCount(executeForCount(param));
|
87 |
+
page.setList(executeForList(param, rowBounds));
|
88 |
+
return page;
|
89 |
+
}
|
90 |
+
|
91 |
+
/**
|
92 |
+
* 执行总数的方法,调用方法执行计算总数,取得总结果
|
93 |
+
*
|
94 |
+
* @param param 参数信息
|
95 |
+
* @return 查询的总记录数
|
96 |
+
*/
|
97 |
+
private long executeForCount(Object param) {
|
98 |
+
Number result = (Number) sqlSession.selectOne(commandCountName, param);
|
99 |
+
return result.longValue();
|
100 |
+
}
|
101 |
+
|
102 |
+
/**
|
103 |
+
* 取得分页的执行结果,返回的是纪录信息
|
104 |
+
*
|
105 |
+
* @param param 参数
|
106 |
+
* @param rowBounds row
|
107 |
+
* @return 纪录列表
|
108 |
+
*/
|
109 |
+
private List<Object> executeForList(Object param, RowBounds rowBounds) {
|
110 |
+
return sqlSession.selectList(commandName, param, rowBounds);
|
111 |
+
}
|
112 |
+
|
113 |
+
/**
|
114 |
+
* 取得当前执行的参数信息
|
115 |
+
*
|
116 |
+
* @param args 参数
|
117 |
+
* @return 参数信息
|
118 |
+
*/
|
119 |
+
private Object getParam(Object[] args) {
|
120 |
+
final int paramCount = paramPositions.size();
|
121 |
+
if (args == null || paramCount == 0) {
|
122 |
+
return null;
|
123 |
+
} else if (!hasNamedParameters && paramCount == 1) {
|
124 |
+
return args[paramPositions.get(0)];
|
125 |
+
} else {
|
126 |
+
Map<String, Object> param = new HashMap<String, Object>();
|
127 |
+
for (int i = 0; i < paramCount; i++) {
|
128 |
+
param.put(paramNames.get(i), args[paramPositions.get(i)]);
|
129 |
+
}
|
130 |
+
return param;
|
131 |
+
}
|
132 |
+
}
|
133 |
+
|
134 |
+
private void setupMethodSignature() {
|
135 |
+
final Class<?>[] argTypes = method.getParameterTypes();
|
136 |
+
for (int i = 0; i < argTypes.length; i++) {
|
137 |
+
if (Page.class.isAssignableFrom(argTypes[i])) {
|
138 |
+
paginationIndex = i;
|
139 |
+
} else if (RowBounds.class.isAssignableFrom(argTypes[i])) {
|
140 |
+
rowBoundsIndex = i;
|
141 |
+
} else {
|
142 |
+
String paramName = String.valueOf(paramPositions.size());
|
143 |
+
paramName = getParamNameFromAnnotation(i, paramName);
|
144 |
+
paramNames.add(paramName);
|
145 |
+
paramPositions.add(i);
|
146 |
+
}
|
147 |
+
}
|
148 |
+
}
|
149 |
+
|
150 |
+
private String getParamNameFromAnnotation(int i, String paramName) {
|
151 |
+
Object[] annotations = method.getParameterAnnotations()[i];
|
152 |
+
for (Object annotation : annotations) {
|
153 |
+
if (annotation instanceof Param) {
|
154 |
+
hasNamedParameters = true;
|
155 |
+
paramName = ((Param) annotation).value();
|
156 |
+
}
|
157 |
+
}
|
158 |
+
return paramName;
|
159 |
+
}
|
160 |
+
|
161 |
+
/**
|
162 |
+
* 设置当前的查询总记录数的ID
|
163 |
+
*/
|
164 |
+
private void setupFields() {
|
165 |
+
commandName = declaringInterface.getName() + "." + method.getName();
|
166 |
+
commandCountName = commandName + "Count"; // 命名约定
|
167 |
+
}
|
168 |
+
|
169 |
+
/**
|
170 |
+
* 设置当前的参数的类型信息
|
171 |
+
*/
|
172 |
+
private void setupCommandType() {
|
173 |
+
MappedStatement ms = config.getMappedStatement(commandName);
|
174 |
+
type = ms.getSqlCommandType();
|
175 |
+
if (type != SqlCommandType.SELECT) {
|
176 |
+
throw new BindingException("Unsupport execution method for: " + commandName);
|
177 |
+
}
|
178 |
+
}
|
179 |
+
|
180 |
+
/**
|
181 |
+
* 验证Statement
|
182 |
+
*/
|
183 |
+
private void validateStatement() {
|
184 |
+
if (!config.hasStatement(commandName)) {
|
185 |
+
throw new BindingException("Invalid bound statement (not found): " + commandName);
|
186 |
+
}
|
187 |
+
if (!config.hasStatement(commandCountName)) {
|
188 |
+
throw new BindingException("Invalid bound statement (not found): " + commandCountName);
|
189 |
+
}
|
190 |
+
}
|
191 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PaginationMapperProxy.java
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.proxy;
|
5 |
+
|
6 |
+
import org.apache.ibatis.binding.BindingException;
|
7 |
+
import org.apache.ibatis.binding.MapperMethod;
|
8 |
+
import org.apache.ibatis.session.SqlSession;
|
9 |
+
|
10 |
+
import com.thinkgem.jeesite.common.persistence.Page;
|
11 |
+
import com.thinkgem.jeesite.common.utils.Reflections;
|
12 |
+
|
13 |
+
import java.lang.reflect.InvocationHandler;
|
14 |
+
import java.lang.reflect.Method;
|
15 |
+
import java.lang.reflect.Proxy;
|
16 |
+
import java.util.HashSet;
|
17 |
+
import java.util.Set;
|
18 |
+
|
19 |
+
/**
|
20 |
+
* <p>
|
21 |
+
* .
|
22 |
+
* </p>
|
23 |
+
*
|
24 |
+
* @author poplar.yfyang
|
25 |
+
* @version 1.0 2012-05-13 上午10:07
|
26 |
+
* @since JDK 1.5
|
27 |
+
*/
|
28 |
+
public class PaginationMapperProxy implements InvocationHandler {
|
29 |
+
|
30 |
+
|
31 |
+
private static final Set<String> OBJECT_METHODS = new HashSet<String>() {
|
32 |
+
private static final long serialVersionUID = -1782950882770203583L;
|
33 |
+
{
|
34 |
+
add("toString");
|
35 |
+
add("getClass");
|
36 |
+
add("hashCode");
|
37 |
+
add("equals");
|
38 |
+
add("wait");
|
39 |
+
add("notify");
|
40 |
+
add("notifyAll");
|
41 |
+
}
|
42 |
+
};
|
43 |
+
|
44 |
+
private boolean isObjectMethod(Method method) {
|
45 |
+
return OBJECT_METHODS.contains(method.getName());
|
46 |
+
}
|
47 |
+
|
48 |
+
private final SqlSession sqlSession;
|
49 |
+
|
50 |
+
private PaginationMapperProxy(final SqlSession sqlSession) {
|
51 |
+
this.sqlSession = sqlSession;
|
52 |
+
}
|
53 |
+
|
54 |
+
@Override
|
55 |
+
public Object invoke(Object proxy, Method method, Object[] args)
|
56 |
+
throws Throwable {
|
57 |
+
if (isObjectMethod(method)) {
|
58 |
+
return null;
|
59 |
+
}
|
60 |
+
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
|
61 |
+
if (Page.class.isAssignableFrom(method.getReturnType())) {
|
62 |
+
// 分页处理
|
63 |
+
return new PaginationMapperMethod(declaringInterface, method, sqlSession).execute(args);
|
64 |
+
}
|
65 |
+
// 原处理方式
|
66 |
+
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession.getConfiguration());
|
67 |
+
final Object result = mapperMethod.execute(sqlSession, args);
|
68 |
+
if (result == null && method.getReturnType().isPrimitive()) {
|
69 |
+
throw new BindingException(
|
70 |
+
"Mapper method '"
|
71 |
+
+ method.getName()
|
72 |
+
+ "' ("
|
73 |
+
+ method.getDeclaringClass()
|
74 |
+
+ ") attempted to return null from a method with a primitive return type ("
|
75 |
+
+ method.getReturnType() + ").");
|
76 |
+
}
|
77 |
+
return result;
|
78 |
+
}
|
79 |
+
|
80 |
+
private Class<?> findDeclaringInterface(Object proxy, Method method) {
|
81 |
+
Class<?> declaringInterface = null;
|
82 |
+
for (Class<?> mapperFaces : proxy.getClass().getInterfaces()) {
|
83 |
+
Method m = Reflections.getAccessibleMethod(mapperFaces,
|
84 |
+
method.getName(),
|
85 |
+
method.getParameterTypes());
|
86 |
+
if (m != null) {
|
87 |
+
declaringInterface = mapperFaces;
|
88 |
+
}
|
89 |
+
}
|
90 |
+
if (declaringInterface == null) {
|
91 |
+
throw new BindingException(
|
92 |
+
"Could not find interface with the given method " + method);
|
93 |
+
}
|
94 |
+
return declaringInterface;
|
95 |
+
}
|
96 |
+
|
97 |
+
@SuppressWarnings("unchecked")
|
98 |
+
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
|
99 |
+
ClassLoader classLoader = mapperInterface.getClassLoader();
|
100 |
+
Class<?>[] interfaces = new Class[]{mapperInterface};
|
101 |
+
PaginationMapperProxy proxy = new PaginationMapperProxy(sqlSession);
|
102 |
+
return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
|
103 |
+
}
|
104 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/persistence/proxy/PaginationMapperRegistry.java
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.persistence.proxy;
|
5 |
+
|
6 |
+
import org.apache.ibatis.binding.BindingException;
|
7 |
+
import org.apache.ibatis.binding.MapperRegistry;
|
8 |
+
import org.apache.ibatis.session.Configuration;
|
9 |
+
import org.apache.ibatis.session.SqlSession;
|
10 |
+
|
11 |
+
/**
|
12 |
+
* <p>
|
13 |
+
* .
|
14 |
+
* </p>
|
15 |
+
*
|
16 |
+
* @author poplar.yfyang
|
17 |
+
* @version 1.0 2012-05-13 上午10:06
|
18 |
+
* @since JDK 1.5
|
19 |
+
*/
|
20 |
+
public class PaginationMapperRegistry extends MapperRegistry {
|
21 |
+
public PaginationMapperRegistry(Configuration config) {
|
22 |
+
super(config);
|
23 |
+
}
|
24 |
+
|
25 |
+
@Override
|
26 |
+
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
|
27 |
+
if (!hasMapper(type)) {
|
28 |
+
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
|
29 |
+
}
|
30 |
+
try {
|
31 |
+
return PaginationMapperProxy.newMapperProxy(type, sqlSession);
|
32 |
+
} catch (Exception e) {
|
33 |
+
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
|
34 |
+
}
|
35 |
+
}
|
36 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/security/Cryptos.java
ADDED
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright (c) 2005-2012 springside.org.cn
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.security;
|
5 |
+
|
6 |
+
import java.io.UnsupportedEncodingException;
|
7 |
+
import java.security.GeneralSecurityException;
|
8 |
+
import java.security.SecureRandom;
|
9 |
+
import java.util.Arrays;
|
10 |
+
|
11 |
+
import javax.crypto.Cipher;
|
12 |
+
import javax.crypto.KeyGenerator;
|
13 |
+
import javax.crypto.Mac;
|
14 |
+
import javax.crypto.SecretKey;
|
15 |
+
import javax.crypto.spec.IvParameterSpec;
|
16 |
+
import javax.crypto.spec.SecretKeySpec;
|
17 |
+
|
18 |
+
import com.thinkgem.jeesite.common.utils.Encodes;
|
19 |
+
import com.thinkgem.jeesite.common.utils.Exceptions;
|
20 |
+
|
21 |
+
/**
|
22 |
+
* 支持HMAC-SHA1消息签名 及 DES/AES对称加密的工具类.
|
23 |
+
*
|
24 |
+
* 支持Hex与Base64两种编码方式.
|
25 |
+
*
|
26 |
+
* @author calvin
|
27 |
+
*/
|
28 |
+
public class Cryptos {
|
29 |
+
|
30 |
+
private static final String AES = "AES";
|
31 |
+
private static final String AES_CBC = "AES/CBC/PKCS5Padding";
|
32 |
+
private static final String HMACSHA1 = "HmacSHA1";
|
33 |
+
|
34 |
+
private static final String DEFAULT_URL_ENCODING = "UTF-8";
|
35 |
+
private static final int DEFAULT_HMACSHA1_KEYSIZE = 160; //RFC2401
|
36 |
+
private static final int DEFAULT_AES_KEYSIZE = 128;
|
37 |
+
private static final int DEFAULT_IVSIZE = 16;
|
38 |
+
|
39 |
+
private static final byte[] DEFAULT_KEY = new byte[]{-97,88,-94,9,70,-76,126,25,0,3,-20,113,108,28,69,125};
|
40 |
+
|
41 |
+
private static SecureRandom random = new SecureRandom();
|
42 |
+
|
43 |
+
//-- HMAC-SHA1 funciton --//
|
44 |
+
/**
|
45 |
+
* 使用HMAC-SHA1进行消息签名, 返回字节数组,长度为20字节.
|
46 |
+
*
|
47 |
+
* @param input 原始输入字符数组
|
48 |
+
* @param key HMAC-SHA1密钥
|
49 |
+
*/
|
50 |
+
public static byte[] hmacSha1(byte[] input, byte[] key) {
|
51 |
+
try {
|
52 |
+
SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
|
53 |
+
Mac mac = Mac.getInstance(HMACSHA1);
|
54 |
+
mac.init(secretKey);
|
55 |
+
return mac.doFinal(input);
|
56 |
+
} catch (GeneralSecurityException e) {
|
57 |
+
throw Exceptions.unchecked(e);
|
58 |
+
}
|
59 |
+
}
|
60 |
+
|
61 |
+
/**
|
62 |
+
* 校验HMAC-SHA1签名是否正确.
|
63 |
+
*
|
64 |
+
* @param expected 已存在的签名
|
65 |
+
* @param input 原始输入字符串
|
66 |
+
* @param key 密钥
|
67 |
+
*/
|
68 |
+
public static boolean isMacValid(byte[] expected, byte[] input, byte[] key) {
|
69 |
+
byte[] actual = hmacSha1(input, key);
|
70 |
+
return Arrays.equals(expected, actual);
|
71 |
+
}
|
72 |
+
|
73 |
+
/**
|
74 |
+
* 生成HMAC-SHA1密钥,返回字节数组,长度为160位(20字节).
|
75 |
+
* HMAC-SHA1算法对密钥无特殊要求, RFC2401建议最少长度为160位(20字节).
|
76 |
+
*/
|
77 |
+
public static byte[] generateHmacSha1Key() {
|
78 |
+
try {
|
79 |
+
KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1);
|
80 |
+
keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
|
81 |
+
SecretKey secretKey = keyGenerator.generateKey();
|
82 |
+
return secretKey.getEncoded();
|
83 |
+
} catch (GeneralSecurityException e) {
|
84 |
+
throw Exceptions.unchecked(e);
|
85 |
+
}
|
86 |
+
}
|
87 |
+
|
88 |
+
//-- AES funciton --//
|
89 |
+
|
90 |
+
/**
|
91 |
+
* 使用AES加密原始字符串.
|
92 |
+
*
|
93 |
+
* @param input 原始输入字符数组
|
94 |
+
*/
|
95 |
+
public static String aesEncrypt(String input) {
|
96 |
+
try {
|
97 |
+
return Encodes.encodeHex(aesEncrypt(input.getBytes(DEFAULT_URL_ENCODING), DEFAULT_KEY));
|
98 |
+
} catch (UnsupportedEncodingException e) {
|
99 |
+
return "";
|
100 |
+
}
|
101 |
+
}
|
102 |
+
|
103 |
+
/**
|
104 |
+
* 使用AES加密原始字符串.
|
105 |
+
*
|
106 |
+
* @param input 原始输入字符数组
|
107 |
+
* @param key 符合AES要求的密钥
|
108 |
+
*/
|
109 |
+
public static String aesEncrypt(String input, String key) {
|
110 |
+
try {
|
111 |
+
return Encodes.encodeHex(aesEncrypt(input.getBytes(DEFAULT_URL_ENCODING), Encodes.decodeHex(key)));
|
112 |
+
} catch (UnsupportedEncodingException e) {
|
113 |
+
return "";
|
114 |
+
}
|
115 |
+
}
|
116 |
+
|
117 |
+
/**
|
118 |
+
* 使用AES加密原始字符串.
|
119 |
+
*
|
120 |
+
* @param input 原始输入字符数组
|
121 |
+
* @param key 符合AES要求的密钥
|
122 |
+
*/
|
123 |
+
public static byte[] aesEncrypt(byte[] input, byte[] key) {
|
124 |
+
return aes(input, key, Cipher.ENCRYPT_MODE);
|
125 |
+
}
|
126 |
+
|
127 |
+
/**
|
128 |
+
* 使用AES加密原始字符串.
|
129 |
+
*
|
130 |
+
* @param input 原始输入字符数组
|
131 |
+
* @param key 符合AES要求的密钥
|
132 |
+
* @param iv 初始向量
|
133 |
+
*/
|
134 |
+
public static byte[] aesEncrypt(byte[] input, byte[] key, byte[] iv) {
|
135 |
+
return aes(input, key, iv, Cipher.ENCRYPT_MODE);
|
136 |
+
}
|
137 |
+
|
138 |
+
/**
|
139 |
+
* 使用AES解密字符串, 返回原始字符串.
|
140 |
+
*
|
141 |
+
* @param input Hex编码的加密字符串
|
142 |
+
*/
|
143 |
+
public static String aesDecrypt(String input) {
|
144 |
+
try {
|
145 |
+
return new String(aesDecrypt(Encodes.decodeHex(input), DEFAULT_KEY), DEFAULT_URL_ENCODING);
|
146 |
+
} catch (UnsupportedEncodingException e) {
|
147 |
+
return "";
|
148 |
+
}
|
149 |
+
}
|
150 |
+
|
151 |
+
/**
|
152 |
+
* 使用AES解密字符串, 返回原始字符串.
|
153 |
+
*
|
154 |
+
* @param input Hex编码的加密字符串
|
155 |
+
* @param key 符合AES要求的密钥
|
156 |
+
*/
|
157 |
+
public static String aesDecrypt(String input, String key) {
|
158 |
+
try {
|
159 |
+
return new String(aesDecrypt(Encodes.decodeHex(input), Encodes.decodeHex(key)), DEFAULT_URL_ENCODING);
|
160 |
+
} catch (UnsupportedEncodingException e) {
|
161 |
+
return "";
|
162 |
+
}
|
163 |
+
}
|
164 |
+
|
165 |
+
/**
|
166 |
+
* 使用AES解密字符串, 返回原始字符串.
|
167 |
+
*
|
168 |
+
* @param input Hex编码的加密字符串
|
169 |
+
* @param key 符合AES要求的密钥
|
170 |
+
*/
|
171 |
+
public static byte[] aesDecrypt(byte[] input, byte[] key) {
|
172 |
+
return aes(input, key, Cipher.DECRYPT_MODE);
|
173 |
+
}
|
174 |
+
|
175 |
+
/**
|
176 |
+
* 使用AES解密字符串, 返回原始字符串.
|
177 |
+
*
|
178 |
+
* @param input Hex编码的加密字符串
|
179 |
+
* @param key 符合AES要求的密钥
|
180 |
+
* @param iv 初始向量
|
181 |
+
*/
|
182 |
+
public static byte[] aesDecrypt(byte[] input, byte[] key, byte[] iv) {
|
183 |
+
return aes(input, key, iv, Cipher.DECRYPT_MODE);
|
184 |
+
}
|
185 |
+
|
186 |
+
/**
|
187 |
+
* 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
|
188 |
+
*
|
189 |
+
* @param input 原始字节数组
|
190 |
+
* @param key 符合AES要求的密钥
|
191 |
+
* @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
|
192 |
+
*/
|
193 |
+
private static byte[] aes(byte[] input, byte[] key, int mode) {
|
194 |
+
try {
|
195 |
+
SecretKey secretKey = new SecretKeySpec(key, AES);
|
196 |
+
Cipher cipher = Cipher.getInstance(AES);
|
197 |
+
cipher.init(mode, secretKey);
|
198 |
+
return cipher.doFinal(input);
|
199 |
+
} catch (GeneralSecurityException e) {
|
200 |
+
throw Exceptions.unchecked(e);
|
201 |
+
}
|
202 |
+
}
|
203 |
+
|
204 |
+
/**
|
205 |
+
* 使用AES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
|
206 |
+
*
|
207 |
+
* @param input 原始字节数组
|
208 |
+
* @param key 符合AES要求的密钥
|
209 |
+
* @param iv 初始向量
|
210 |
+
* @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
|
211 |
+
*/
|
212 |
+
private static byte[] aes(byte[] input, byte[] key, byte[] iv, int mode) {
|
213 |
+
try {
|
214 |
+
SecretKey secretKey = new SecretKeySpec(key, AES);
|
215 |
+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
216 |
+
Cipher cipher = Cipher.getInstance(AES_CBC);
|
217 |
+
cipher.init(mode, secretKey, ivSpec);
|
218 |
+
return cipher.doFinal(input);
|
219 |
+
} catch (GeneralSecurityException e) {
|
220 |
+
throw Exceptions.unchecked(e);
|
221 |
+
}
|
222 |
+
}
|
223 |
+
|
224 |
+
/**
|
225 |
+
* 生成AES密钥,返回字节数组, 默认长度为128位(16字节).
|
226 |
+
*/
|
227 |
+
public static String generateAesKeyString() {
|
228 |
+
return Encodes.encodeHex(generateAesKey(DEFAULT_AES_KEYSIZE));
|
229 |
+
}
|
230 |
+
|
231 |
+
/**
|
232 |
+
* 生成AES密钥,返回字节数组, 默认长度为128位(16字节).
|
233 |
+
*/
|
234 |
+
public static byte[] generateAesKey() {
|
235 |
+
return generateAesKey(DEFAULT_AES_KEYSIZE);
|
236 |
+
}
|
237 |
+
|
238 |
+
/**
|
239 |
+
* 生成AES密钥,可选长度为128,192,256位.
|
240 |
+
*/
|
241 |
+
public static byte[] generateAesKey(int keysize) {
|
242 |
+
try {
|
243 |
+
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
|
244 |
+
keyGenerator.init(keysize);
|
245 |
+
SecretKey secretKey = keyGenerator.generateKey();
|
246 |
+
return secretKey.getEncoded();
|
247 |
+
} catch (GeneralSecurityException e) {
|
248 |
+
throw Exceptions.unchecked(e);
|
249 |
+
}
|
250 |
+
}
|
251 |
+
|
252 |
+
/**
|
253 |
+
* 生成随机向量,默认大小为cipher.getBlockSize(), 16字节.
|
254 |
+
*/
|
255 |
+
public static byte[] generateIV() {
|
256 |
+
byte[] bytes = new byte[DEFAULT_IVSIZE];
|
257 |
+
random.nextBytes(bytes);
|
258 |
+
return bytes;
|
259 |
+
}
|
260 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/security/Digests.java
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright (c) 2005-2012 springside.org.cn
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.security;
|
5 |
+
|
6 |
+
import java.io.IOException;
|
7 |
+
import java.io.InputStream;
|
8 |
+
import java.security.GeneralSecurityException;
|
9 |
+
import java.security.MessageDigest;
|
10 |
+
import java.security.SecureRandom;
|
11 |
+
|
12 |
+
import org.apache.commons.lang3.Validate;
|
13 |
+
|
14 |
+
import com.thinkgem.jeesite.common.utils.Exceptions;
|
15 |
+
|
16 |
+
/**
|
17 |
+
* 支持SHA-1/MD5消息摘要的工具类.
|
18 |
+
*
|
19 |
+
* 返回ByteSource,可进一步被编码为Hex, Base64或UrlSafeBase64
|
20 |
+
*
|
21 |
+
* @author calvin
|
22 |
+
*/
|
23 |
+
public class Digests {
|
24 |
+
|
25 |
+
private static final String SHA1 = "SHA-1";
|
26 |
+
private static final String MD5 = "MD5";
|
27 |
+
|
28 |
+
private static SecureRandom random = new SecureRandom();
|
29 |
+
|
30 |
+
/**
|
31 |
+
* 对输入字符串进行md5散列.
|
32 |
+
*/
|
33 |
+
public static byte[] md5(byte[] input) {
|
34 |
+
return digest(input, MD5, null, 1);
|
35 |
+
}
|
36 |
+
public static byte[] md5(byte[] input, int iterations) {
|
37 |
+
return digest(input, MD5, null, iterations);
|
38 |
+
}
|
39 |
+
|
40 |
+
/**
|
41 |
+
* 对输入字符串进行sha1散列.
|
42 |
+
*/
|
43 |
+
public static byte[] sha1(byte[] input) {
|
44 |
+
return digest(input, SHA1, null, 1);
|
45 |
+
}
|
46 |
+
|
47 |
+
public static byte[] sha1(byte[] input, byte[] salt) {
|
48 |
+
return digest(input, SHA1, salt, 1);
|
49 |
+
}
|
50 |
+
|
51 |
+
public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
|
52 |
+
return digest(input, SHA1, salt, iterations);
|
53 |
+
}
|
54 |
+
|
55 |
+
/**
|
56 |
+
* 对字符串进行散列, 支持md5与sha1算法.
|
57 |
+
*/
|
58 |
+
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
|
59 |
+
try {
|
60 |
+
MessageDigest digest = MessageDigest.getInstance(algorithm);
|
61 |
+
|
62 |
+
if (salt != null) {
|
63 |
+
digest.update(salt);
|
64 |
+
}
|
65 |
+
|
66 |
+
byte[] result = digest.digest(input);
|
67 |
+
|
68 |
+
for (int i = 1; i < iterations; i++) {
|
69 |
+
digest.reset();
|
70 |
+
result = digest.digest(result);
|
71 |
+
}
|
72 |
+
return result;
|
73 |
+
} catch (GeneralSecurityException e) {
|
74 |
+
throw Exceptions.unchecked(e);
|
75 |
+
}
|
76 |
+
}
|
77 |
+
|
78 |
+
/**
|
79 |
+
* 生成随机的Byte[]作为salt.
|
80 |
+
*
|
81 |
+
* @param numBytes byte数组的大小
|
82 |
+
*/
|
83 |
+
public static byte[] generateSalt(int numBytes) {
|
84 |
+
Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);
|
85 |
+
|
86 |
+
byte[] bytes = new byte[numBytes];
|
87 |
+
random.nextBytes(bytes);
|
88 |
+
return bytes;
|
89 |
+
}
|
90 |
+
|
91 |
+
/**
|
92 |
+
* 对文件进行md5散列.
|
93 |
+
*/
|
94 |
+
public static byte[] md5(InputStream input) throws IOException {
|
95 |
+
return digest(input, MD5);
|
96 |
+
}
|
97 |
+
|
98 |
+
/**
|
99 |
+
* 对文件进行sha1散列.
|
100 |
+
*/
|
101 |
+
public static byte[] sha1(InputStream input) throws IOException {
|
102 |
+
return digest(input, SHA1);
|
103 |
+
}
|
104 |
+
|
105 |
+
private static byte[] digest(InputStream input, String algorithm) throws IOException {
|
106 |
+
try {
|
107 |
+
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
|
108 |
+
int bufferLength = 8 * 1024;
|
109 |
+
byte[] buffer = new byte[bufferLength];
|
110 |
+
int read = input.read(buffer, 0, bufferLength);
|
111 |
+
|
112 |
+
while (read > -1) {
|
113 |
+
messageDigest.update(buffer, 0, read);
|
114 |
+
read = input.read(buffer, 0, bufferLength);
|
115 |
+
}
|
116 |
+
|
117 |
+
return messageDigest.digest();
|
118 |
+
} catch (GeneralSecurityException e) {
|
119 |
+
throw Exceptions.unchecked(e);
|
120 |
+
}
|
121 |
+
}
|
122 |
+
|
123 |
+
}
|
test/1273091433/jeesite/src/main/java/com/thinkgem/jeesite/common/security/shiro/HasAnyPermissionsTag.java
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
|
3 |
+
*/
|
4 |
+
package com.thinkgem.jeesite.common.security.shiro;
|
5 |
+
|
6 |
+
import org.apache.shiro.subject.Subject;
|
7 |
+
import org.apache.shiro.web.tags.PermissionTag;
|
8 |
+
|
9 |
+
/**
|
10 |
+
* Shiro HasAnyPermissions Tag.
|
11 |
+
*
|
12 |
+
* @author calvin
|
13 |
+
*/
|
14 |
+
public class HasAnyPermissionsTag extends PermissionTag {
|
15 |
+
|
16 |
+
private static final long serialVersionUID = 1L;
|
17 |
+
private static final String PERMISSION_NAMES_DELIMETER = ",";
|
18 |
+
|
19 |
+
@Override
|
20 |
+
protected boolean showTagBody(String permissionNames) {
|
21 |
+
boolean hasAnyPermission = false;
|
22 |
+
|
23 |
+
Subject subject = getSubject();
|
24 |
+
|
25 |
+
if (subject != null) {
|
26 |
+
// Iterate through permissions and check to see if the user has one of the permissions
|
27 |
+
for (String permission : permissionNames.split(PERMISSION_NAMES_DELIMETER)) {
|
28 |
+
|
29 |
+
if (subject.isPermitted(permission.trim())) {
|
30 |
+
hasAnyPermission = true;
|
31 |
+
break;
|
32 |
+
}
|
33 |
+
|
34 |
+
}
|
35 |
+
}
|
36 |
+
|
37 |
+
return hasAnyPermission;
|
38 |
+
}
|
39 |
+
|
40 |
+
}
|