Created
September 21, 2015 01:53
-
-
Save safeJar/7070eec252d8d8a1264d to your computer and use it in GitHub Desktop.
mybatis daoimpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.intel.dao; | |
| import java.lang.reflect.ParameterizedType; | |
| import java.lang.reflect.Type; | |
| import java.util.List; | |
| import org.apache.commons.collections.CollectionUtils; | |
| import org.apache.ibatis.session.ExecutorType; | |
| import org.apache.ibatis.session.SqlSession; | |
| import org.apache.ibatis.session.SqlSessionFactory; | |
| import org.apache.log4j.Logger; | |
| import org.mybatis.spring.SqlSessionTemplate; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| public class BaseDaoImpl<T> implements BaseDao<T> { | |
| protected String clazzName; | |
| @Autowired | |
| protected SqlSessionTemplate sqlSessionTemplate; | |
| protected final Logger logger = Logger.getLogger(this.getClass()); | |
| private String getNamespace(Class<?> clazz) { | |
| return clazz.getSimpleName(); | |
| } | |
| public BaseDaoImpl() { | |
| Type type = this.getClass().getGenericSuperclass(); | |
| Class<?> clazz; | |
| if (type instanceof ParameterizedType) { | |
| clazz = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]; | |
| clazzName = getNamespace(clazz); | |
| } | |
| } | |
| private final String OP_INSERT = ".insert"; | |
| private final String OP_DELETE = ".delete"; | |
| private final String OP_UPDATE = ".update"; | |
| private final String OP_GET = ".get"; | |
| private final String OP_QUERY = ".query"; | |
| private final String OP_QUERYCOUNT = ".queryCount"; | |
| private static final int DEFAULT_FLUSH_SIZE = 200; | |
| @Override | |
| public void insertBatch(List<T> modelList) { | |
| insertBatch(modelList, DEFAULT_FLUSH_SIZE); | |
| } | |
| @Override | |
| public void insertBatch(List<T> list, int flushSize) { | |
| if (CollectionUtils.isNotEmpty(list)) { | |
| SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory(); | |
| SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); | |
| String statementName = clazzName + OP_INSERT; | |
| try { | |
| for (int i = 0; i < list.size(); i++) { | |
| sqlSession.insert(statementName, list.get(i)); | |
| if ((i - 1) % flushSize == 0) { | |
| sqlSession.commit(); | |
| } | |
| } | |
| sqlSession.commit(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| sqlSession.rollback(); | |
| return; | |
| } finally { | |
| sqlSession.close(); | |
| } | |
| } | |
| } | |
| @Override | |
| public void updateBatch(List<T> modelList) { | |
| updateBatch(modelList, DEFAULT_FLUSH_SIZE); | |
| } | |
| @Override | |
| public void updateBatch(List<T> list, int flushSize) { | |
| if (CollectionUtils.isNotEmpty(list)) { | |
| SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory(); | |
| SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); | |
| String statementName = clazzName + OP_UPDATE; | |
| try { | |
| for (int i = 0; i < list.size(); i++) { | |
| T model = null; | |
| try { | |
| model = list.get(i); | |
| } catch (Exception e) { | |
| continue; | |
| } | |
| sqlSession.update(statementName, model); | |
| if ((i - 1) % flushSize == 0) { | |
| sqlSession.commit(); | |
| } | |
| } | |
| sqlSession.commit(); | |
| } catch (Exception e) { | |
| sqlSession.rollback(); | |
| return; | |
| } finally { | |
| sqlSession.close(); | |
| } | |
| } | |
| } | |
| public void insert(T model) { | |
| String statementName = clazzName + OP_INSERT; | |
| sqlSessionTemplate.insert(statementName, model); | |
| } | |
| public void delete(int... ids) { | |
| String statementName = clazzName + OP_DELETE; | |
| sqlSessionTemplate.delete(statementName, ids); | |
| } | |
| public void update(T model) { | |
| String statementName = clazzName + OP_UPDATE; | |
| sqlSessionTemplate.update(statementName, model); | |
| } | |
| @SuppressWarnings("unchecked") | |
| public T get(int id) { | |
| String statementName = clazzName + OP_GET; | |
| return (T) sqlSessionTemplate.selectOne(statementName, id); | |
| } | |
| @SuppressWarnings("unchecked") | |
| public List<T> query(T model) { | |
| String statementName = clazzName + OP_QUERY; | |
| return (List<T>) sqlSessionTemplate.selectList(statementName, model); | |
| } | |
| @Override | |
| public int queryCount(T model) { | |
| String statementName = clazzName + OP_QUERYCOUNT; | |
| return (Integer) sqlSessionTemplate.selectOne(statementName, model); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment