Mybatis工具类
自己写的Mybatis工具类,不包括事务处理。
/**
* MybatisUtil.java
*
* @screen
* @author havery
*/
package com.newpp.core.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* MybatisUtil.
*
* @author havery
*/
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
private static final int UPDATE_FLG_INSERT = 1;
private static final int UPDATE_FLG_UPDATE = 2;
private static final int UPDATE_FLG_DELETE = 3;
private static int BATCH_COMMIT_COUNT = 5;
private MybatisUtil() {
}
private static SqlSessionFactory getSqlSessionFactory() throws IOException {
if (sqlSessionFactory == null) {
String resource = "mybatis-configuration.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
return sqlSessionFactory;
}
public static int insert(String sqlID, Object paramObj) throws IOException {
return doUpdate(sqlID, paramObj, UPDATE_FLG_INSERT);
}
public static int update(String sqlID, Object paramObj) throws IOException {
return doUpdate(sqlID, paramObj, UPDATE_FLG_UPDATE);
}
public static int delete(String sqlID, Object paramObj) throws IOException {
return doUpdate(sqlID, paramObj, UPDATE_FLG_DELETE);
}
/**
* @param sqlID
* @param paramObj
* @return
* @throws IOException
*/
private static int doUpdate(String sqlID, Object paramObj, int updateFlg)
throws IOException {
SqlSession session = getSqlSessionFactory().openSession();
try {
int cnt = 0;
switch (updateFlg) {
case UPDATE_FLG_INSERT:
cnt = session.insert(sqlID, paramObj);
break;
case UPDATE_FLG_UPDATE:
cnt = session.update(sqlID, paramObj);
break;
case UPDATE_FLG_DELETE:
cnt = session.delete(sqlID, paramObj);
break;
default:
break;
}
session.commit();
return cnt;
} finally {
session.close();
}
}
public static List<Object> findList(String sqlID, Object paramObj)
throws IOException {
SqlSession session = getSqlSessionFactory().openSession();
try {
List<Object> list = session.selectList(sqlID, paramObj);
return list;
} finally {
session.close();
}
}
public static List<Object> findListPaging(String sqlID, Object paramObj,
int beginIndex, int maxCount) throws IOException {
SqlSession session = getSqlSessionFactory().openSession();
try {
RowBounds rowBounds = new RowBounds(beginIndex, maxCount);
List<Object> list = session.selectList(sqlID, paramObj, rowBounds);
return list;
} finally {
session.close();
}
}
public static List<Object> findListPaging(String sqlID, Object paramObj,
int beginIndex, String maxCount) throws IOException {
int intMaxCount = 0;
try {
intMaxCount = Integer.parseInt(maxCount);
} catch (Exception e) {
e.printStackTrace();
}
SqlSession session = getSqlSessionFactory().openSession();
try {
RowBounds rowBounds = new RowBounds(beginIndex, intMaxCount);
List<Object> list = session.selectList(sqlID, paramObj, rowBounds);
return list;
} finally {
session.close();
}
}
public static Object findOne(String sqlID, Object paramObj)
throws IOException {
SqlSession session = getSqlSessionFactory().openSession();
try {
Object obj = session.selectOne(sqlID, paramObj);
return obj;
} finally {
session.close();
}
}
public static void findWithResultHandler(String sqlID, Object condition,
ResultHandler resultHandler) throws IOException {
SqlSession session = getSqlSessionFactory().openSession();
try {
session.select(sqlID, condition, resultHandler);
} finally {
session.close();
}
}
public static int count(String sqlID, Object paramObj) throws IOException {
int cnt = 0;
SqlSession session = getSqlSessionFactory().openSession();
try {
Integer obj = (Integer) session.selectOne(sqlID, paramObj);
if (obj != null)
cnt = obj.intValue();
return cnt;
} finally {
session.close();
}
}
// TODO
// @SuppressWarnings("rawtypes")
// public static void checkExclusive(String selectSqlID, Object paramObj,
// String updUserId, String updDate) throws DataExclusiveException,
// DataNotFoundException, IOException {
//
// List result = findList(selectSqlID, paramObj);
// if (result.size() == 0)
// throw new DataNotFoundException("E0113");
// if (result.size() > 1) {
// throw new DatabaseSystemException("E0138");
// } else {
// DbUtil.checkExclusive(updUserId, updDate,
// (EntityBase) result.get(0));
// return;
// }
// }
public static boolean insertWithBatch(String sqlID, List<Object> paramObj)
throws Exception {
SqlSession session = getSqlSessionFactory().openSession(
ExecutorType.BATCH, false);
int totalSize = paramObj.size();
try {
for (int i = 0; i < totalSize; i++) {
session.insert(sqlID, paramObj.get(i));
if ((i + 1) % BATCH_COMMIT_COUNT == 0 || i == totalSize - 1) {
session.commit();
session.clearCache();
}
}
} catch (Exception e1) {
//TODO
session.rollback();
throw e1;
} finally {
session.close();
}
return true;
}
}