package ch.asynk; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.exceptions.PersistenceException; public abstract class AbstractDAO { public final int DAO_ERROR = -1; protected Class 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; } @FunctionalInterface public interface CallBack { public TRet call(TMapper m); } protected int execInt(CallBack cb) { return execInt(cb, false); } protected int execInt(CallBack cb, boolean commit) { 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(CallBack cb) { TObject obj = null; SqlSession session = conn.openSqlSession(); try { obj = cb.call(session.getMapper(mapperClass)); } catch(PersistenceException e) { if (logErrors) conn.error(e); } finally { session.close(); } return obj; } protected List execObjects(CallBack, TMapper> cb) { List list = null; SqlSession session = conn.openSqlSession(); try { list = cb.call(session.getMapper(mapperClass)); } catch(PersistenceException e) { if (logErrors) conn.error(e); } finally { session.close(); } return list; } }