-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasedao.php
More file actions
137 lines (115 loc) · 4.33 KB
/
basedao.php
File metadata and controls
137 lines (115 loc) · 4.33 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
include_once("../include/common.php");
include_once("FileDownloader.php");
$content = <<<EOF
package com.taobao.ju.common.dal.dao;
import java.util.List;
import java.util.Map;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import com.taobao.tddl.client.sequence.Sequence;
import com.taobao.tddl.client.sequence.SequenceException;
/**
* User: duxing
* Email: duxing@taobao.com
* Date: 2013-1-22
*/
public class BaseDAO implements InitializingBean {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
protected SqlMapClientTemplate sqlMapClient;
protected Map<String,Sequence> sequenceTable;
protected JdbcTemplate jdbcTemplate;
@Override
public void afterPropertiesSet() throws Exception {
if(sqlMapClient==null||sequenceTable==null)
throw new Exception("BaseDAO initilize fail,check related spring's configuration file");
}
public void setSqlMapClient(SqlMapClientTemplate sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
public void setSequenceTable(Map<String, Sequence> sequenceTable) {
this.sequenceTable = sequenceTable;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//ID_KEY的内部enum类请各自应用放入自己的代码
protected Long getNextId(String idKey) throws DAOException {
if(idKey == null) throw new IllegalArgumentException("idKey can not be null");
Sequence sequence =sequenceTable.get(idKey+"_sequence");
if(sequence==null) throw new IllegalStateException(idKey+"'s sequence not found");
try {
return sequence.nextValue();
} catch (SequenceException e) {
throw new DAOException("[BaseDAO-getNextId]",e);
}
}
public int update(String statementName, Object parameterObject) throws DAOException {
try{
return sqlMapClient.update(statementName, parameterObject);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-update]",e);
}
}
public Object insert(String statementName, Object parameterObject) throws DAOException {
try{
return sqlMapClient.insert(statementName, parameterObject);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-insert]",e);
}
}
public int delete(String statementName, Object parameterObject) throws DAOException {
try{
return sqlMapClient.delete(statementName, parameterObject);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-delete]",e);
}
}
public Object queryForObject(String statementName, Object parameterObject) throws DAOException {
try{
return sqlMapClient.queryForObject(statementName, parameterObject);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-queryForObject]",e);
}
}
public List<?> queryForList(String statementName, Object parameterObject) throws DAOException {
try{
return sqlMapClient.queryForList(statementName, parameterObject);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-queryForList]",e);
}
}
/**
* 取List,包含分页
*
* @param statementName
* @param parameterObject
* @param pageNo
* 页次
* @param pageSize
* 每页记录数
* @return
* @throws DAOException
* @author zhengqing
*/
public List<?> queryForList(String statementName, Object parameterObject, int pageNo, int pageSize) throws DAOException {
try{
return sqlMapClient.queryForList(statementName, parameterObject, pageSize * (pageNo - 1), pageSize);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-queryForList]",e);
}
}
public Map<?, ?> queryForMap(String statementName, Object parameterObject, String keyProperty) throws DAOException {
try{
return sqlMapClient.queryForMap(statementName, parameterObject, keyProperty);
}catch (DataAccessException e){
throw new DAOException("[BaseDAO-queryForMap]",e);
}
}
}
EOF;
FileDownloader::download("BaseDAO.java",$content);