blob: 916d5114ea142ad320db787d760f4b0a262623cf (
plain)
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
|
package ch.asynk;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.exceptions.PersistenceException;
public abstract class AbstractDAO<TObject, TMapper>
{
public final int DAO_ERROR = -1;
protected Class<TMapper> mapperClass = null;
public static boolean logErrors = true;
private static SqlConnection conn = null;
static {
conn = new SqlConnection();
MapperRegistration.registerMappers(conn);
}
static public SqlConnection getSqlConnection() { return conn; }
protected abstract class CallBack<TRet, TMapper>
{
public abstract TRet call(TMapper m);
}
protected int execInt(boolean commit, CallBack<Integer, TMapper> cb)
{
int status = DAO_ERROR;
SqlSession session = conn.openSqlSession();
try {
status = cb.call(session.getMapper(mapperClass));
if (commit) session.commit();
}
catch(PersistenceException e) {
if (commit) session.rollback();
if (logErrors) conn.error(e);
}
finally { session.close(); }
return status;
}
protected TObject execObject(boolean commit, CallBack<TObject, TMapper> cb)
{
TObject obj = null;
SqlSession session = conn.openSqlSession();
try {
obj = cb.call(session.getMapper(mapperClass));
if (commit) session.commit();
}
catch(PersistenceException e) {
if (commit) session.rollback();
if (logErrors) conn.error(e);
}
finally { session.close(); }
return obj;
}
protected List<TObject> execObjects(boolean commit, CallBack<List<TObject>, TMapper> cb)
{
List<TObject> list = null;
SqlSession session = conn.openSqlSession();
try {
list = cb.call(session.getMapper(mapperClass));
if (commit) session.commit();
}
catch(PersistenceException e) {
if (commit) session.rollback();
if (logErrors) conn.error(e);
}
finally { session.close(); }
return list;
}
}
|