DaoSupport2.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /**
  10. * 第2数据源
  11. * @author FH Q 31 359 6790
  12. * 修改时间:2016、04、29
  13. */
  14. @Repository("daoSupport2")
  15. public class DaoSupport2 implements DAO {
  16. //@Resource(name = "sqlSessionTemplate2") //去掉注释,打开第2数据源
  17. private SqlSessionTemplate sqlSessionTemplate2;
  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 sqlSessionTemplate2.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 sqlSessionTemplate2.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 sqlSessionTemplate2.update(str, obj);
  47. }
  48. /**
  49. * 批量更新
  50. * @param str
  51. * @param obj
  52. * @return
  53. * @throws Exception
  54. */
  55. public void batchUpdate(String str, List objs )throws Exception{
  56. SqlSessionFactory sqlSessionFactory = sqlSessionTemplate2.getSqlSessionFactory();
  57. //批量执行器
  58. SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
  59. try{
  60. if(objs!=null){
  61. for(int i=0,size=objs.size();i<size;i++){
  62. sqlSession.update(str, objs.get(i));
  63. }
  64. sqlSession.flushStatements();
  65. sqlSession.commit();
  66. sqlSession.clearCache();
  67. }
  68. }finally{
  69. sqlSession.close();
  70. }
  71. }
  72. /**
  73. * 批量更新
  74. * @param str
  75. * @param obj
  76. * @return
  77. * @throws Exception
  78. */
  79. public Object batchDelete(String str, List objs )throws Exception{
  80. return sqlSessionTemplate2.delete(str, objs);
  81. }
  82. /**
  83. * 删除对象
  84. * @param str
  85. * @param obj
  86. * @return
  87. * @throws Exception
  88. */
  89. public Object delete(String str, Object obj) throws Exception {
  90. return sqlSessionTemplate2.delete(str, obj);
  91. }
  92. /**
  93. * 查找对象
  94. * @param str
  95. * @param obj
  96. * @return
  97. * @throws Exception
  98. */
  99. public Object findForObject(String str, Object obj) throws Exception {
  100. return sqlSessionTemplate2.selectOne(str, obj);
  101. }
  102. /**
  103. * 查找对象
  104. * @param str
  105. * @param obj
  106. * @return
  107. * @throws Exception
  108. */
  109. public Object findForList(String str, Object obj) throws Exception {
  110. return sqlSessionTemplate2.selectList(str, obj);
  111. }
  112. public Object findForMap(String str, Object obj, String key, String value) throws Exception {
  113. return sqlSessionTemplate2.selectMap(str, obj, key);
  114. }
  115. }