2b131583e71dd75131b2d3d41c65865b4d067a2e.svn-base
3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.common.persistence.interceptor;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.dialect.Dialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.DB2Dialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.DerbyDialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.H2Dialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.HSQLDialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.MySQLDialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.OracleDialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.PostgreSQLDialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.SQLServer2005Dialect;
import com.thinkgem.jeesite.common.persistence.dialect.db.SybaseDialect;
import com.thinkgem.jeesite.common.utils.Reflections;
import java.io.Serializable;
import java.util.Properties;
/**
* Mybatis分页拦截器基类
* @author poplar.yfyang / thinkgem
* @version 2013-8-28
*/
public abstract class BaseInterceptor implements Interceptor, Serializable {
private static final long serialVersionUID = 1L;
protected static final String PAGE = "page";
protected static final String DELEGATE = "delegate";
protected static final String MAPPED_STATEMENT = "mappedStatement";
protected Log log = LogFactory.getLog(this.getClass());
protected Dialect DIALECT;
// /**
// * 拦截的ID,在mapper中的id,可以匹配正则
// */
// protected String _SQL_PATTERN = "";
/**
* 对参数进行转换和检查
* @param parameterObject 参数对象
* @param page 分页对象
* @return 分页对象
* @throws NoSuchFieldException 无法找到参数
*/
@SuppressWarnings("unchecked")
protected static Page<Object> convertParameter(Object parameterObject, Page<Object> page) {
try{
if (parameterObject instanceof Page) {
return (Page<Object>) parameterObject;
} else {
return (Page<Object>)Reflections.getFieldValue(parameterObject, PAGE);
}
}catch (Exception e) {
return null;
}
}
/**
* 设置属性,支持自定义方言类和制定数据库的方式
* <code>dialectClass</code>,自定义方言类。可以不配置这项
* <ode>dbms</ode> 数据库类型,插件支持的数据库
* <code>sqlPattern</code> 需要拦截的SQL ID
* @param p 属性
*/
protected void initProperties(Properties p) {
Dialect dialect = null;
String dbType = Global.getConfig("jdbc.type");
if ("db2".equals(dbType)){
dialect = new DB2Dialect();
}else if("derby".equals(dbType)){
dialect = new DerbyDialect();
}else if("h2".equals(dbType)){
dialect = new H2Dialect();
}else if("hsql".equals(dbType)){
dialect = new HSQLDialect();
}else if("mysql".equals(dbType)){
dialect = new MySQLDialect();
}else if("oracle".equals(dbType)){
dialect = new OracleDialect();
}else if("postgre".equals(dbType)){
dialect = new PostgreSQLDialect();
}else if("mssql".equals(dbType) || "sqlserver".equals(dbType)){
dialect = new SQLServer2005Dialect();
}else if("sybase".equals(dbType)){
dialect = new SybaseDialect();
}
if (dialect == null) {
throw new RuntimeException("mybatis dialect error.");
}
DIALECT = dialect;
// _SQL_PATTERN = p.getProperty("sqlPattern");
// _SQL_PATTERN = Global.getConfig("mybatis.pagePattern");
// if (StringUtils.isEmpty(_SQL_PATTERN)) {
// throw new RuntimeException("sqlPattern property is not found!");
// }
}
}