DaoSupport.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.zhcs.dt.dao;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import org.apache.ibatis.session.ExecutorType;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.mybatis.spring.SqlSessionTemplate;
  8. import org.springframework.stereotype.Repository;
  9. import com.zhcs.dt.util.PageData;
  10. /**
  11. * @author FH Q313596790
  12. * 修改时间:2015、12、11
  13. */
  14. @Repository("daoSupport")
  15. public class DaoSupport implements DAO {
  16. @Resource(name = "sqlSessionTemplate")
  17. private SqlSessionTemplate sqlSessionTemplate;
  18. /**
  19. * 保存对象
  20. * @param str
  21. * @param obj
  22. * @return
  23. * @throws Exception
  24. */
  25. public Object save(String str, Object obj) throws Exception {
  26. return sqlSessionTemplate.insert(str, obj);
  27. }
  28. /**
  29. * 批量更新
  30. * @param str
  31. * @param obj
  32. * @return
  33. * @throws Exception
  34. */
  35. public Object batchSave(String str, List objs )throws Exception{
  36. return sqlSessionTemplate.insert(str, objs);
  37. }
  38. /**
  39. * 修改对象
  40. * @param str
  41. * @param obj
  42. * @return
  43. * @throws Exception
  44. */
  45. public Object update(String str, Object obj) throws Exception {
  46. return sqlSessionTemplate.update(str, obj);
  47. }
  48. /**
  49. * 修改对象
  50. * @param str
  51. * @param obj
  52. * @return
  53. * @throws Exception
  54. */
  55. public Object updateStatus(String str, Object obj) throws Exception {
  56. return sqlSessionTemplate.update(str, obj);
  57. }
  58. /**
  59. * 批量更新
  60. * @param str
  61. * @param obj
  62. * @return
  63. * @throws Exception
  64. */
  65. public void batchUpdate(String str, List objs )throws Exception{
  66. SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
  67. //批量执行器
  68. SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
  69. try{
  70. if(objs!=null){
  71. for(int i=0,size=objs.size();i<size;i++){
  72. sqlSession.update(str, objs.get(i));
  73. }
  74. sqlSession.flushStatements();
  75. sqlSession.commit();
  76. sqlSession.clearCache();
  77. }
  78. }finally{
  79. sqlSession.close();
  80. }
  81. }
  82. /**
  83. * 批量更新
  84. * @param str
  85. * @param obj
  86. * @return
  87. * @throws Exception
  88. */
  89. public Object batchDelete(String str, List objs )throws Exception{
  90. return sqlSessionTemplate.delete(str, objs);
  91. }
  92. /**
  93. * 删除对象
  94. * @param str
  95. * @param obj
  96. * @return
  97. * @throws Exception
  98. */
  99. public Object delete(String str, Object obj) throws Exception {
  100. return sqlSessionTemplate.delete(str, obj);
  101. }
  102. /**
  103. * 查找对象
  104. * @param str
  105. * @param obj
  106. * @return
  107. * @throws Exception
  108. */
  109. public Object findForObject(String str, Object obj) throws Exception {
  110. return sqlSessionTemplate.selectOne(str, obj);
  111. }
  112. /**
  113. * 查找对象
  114. * @param str
  115. * @param obj
  116. * @return
  117. * @throws Exception
  118. */
  119. public Object findForList(String str, Object obj) throws Exception {
  120. return sqlSessionTemplate.selectList(str, obj);
  121. }
  122. public Object findForMap(String str, Object obj, String key, String value) throws Exception {
  123. return sqlSessionTemplate.selectMap(str, obj, key);
  124. }
  125. }