mybaits之dao层通用写法sqlsessiontemplate

1.编写dao接口


   
   
   
   
  1. public interface DAO {
  2. /**
  3. * 保存对象
  4. * @param str
  5. * @param obj
  6. * @return
  7. * @throws Exception
  8. */
  9. public Object save(String str, Object obj) throws Exception;
  10. /**
  11. * 修改对象
  12. * @param str
  13. * @param obj
  14. * @return
  15. * @throws Exception
  16. */
  17. public Object update(String str, Object obj) throws Exception;
  18. /**
  19. * 删除对象
  20. * @param str
  21. * @param obj
  22. * @return
  23. * @throws Exception
  24. */
  25. public Object delete(String str, Object obj) throws Exception;
  26. /**
  27. * 查找对象
  28. * @param str
  29. * @param obj
  30. * @return
  31. * @throws Exception
  32. */
  33. public Object findForObject(String str, Object obj) throws Exception;
  34. /**
  35. * 查找对象
  36. * @param str
  37. * @param obj
  38. * @return
  39. * @throws Exception
  40. */
  41. public Object findForList(String str, Object obj) throws Exception;
  42. /**
  43. * 查找对象封装成Map
  44. * @param s
  45. * @param obj
  46. * @return
  47. * @throws Exception
  48. */
  49. public Object findForMap(String sql, Object obj, String key , String value) throws Exception;
  50. }
2.编写dao 实现类

   
   
   
   
  1. public class DaoSupport implements DAO {
  2. @Resource(name = "sqlSessionTemplate")
  3. private SqlSessionTemplate sqlSessionTemplate;
  4. /**
  5. * 保存对象
  6. * @param str
  7. * @param obj
  8. * @return
  9. * @throws Exception
  10. */
  11. public Object save(String str, Object obj) throws Exception {
  12. return sqlSessionTemplate.insert(str, obj);
  13. }
  14. /**
  15. * 批量更新
  16. * @param str
  17. * @param obj
  18. * @return
  19. * @throws Exception
  20. */
  21. public Object batchSave(String str, List objs )throws Exception{
  22. return sqlSessionTemplate.insert(str, objs);
  23. }
  24. /**
  25. * 修改对象
  26. * @param str
  27. * @param obj
  28. * @return
  29. * @throws Exception
  30. */
  31. public Object update(String str, Object obj) throws Exception {
  32. return sqlSessionTemplate.update(str, obj);
  33. }
  34. /**
  35. * 批量更新
  36. * @param str
  37. * @param obj
  38. * @return
  39. * @throws Exception
  40. */
  41. public void batchUpdate(String str, List objs )throws Exception{
  42. SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
  43. //批量执行器
  44. SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
  45. try{
  46. if(objs!= null){
  47. for( int i= 0,size=objs.size();i
  48. sqlSession.update(str, objs.get(i));
  49. }
  50. sqlSession.flushStatements();
  51. sqlSession.commit();
  52. sqlSession.clearCache();
  53. }
  54. } finally{
  55. sqlSession.close();
  56. }
  57. }
  58. /**
  59. * 批量更新
  60. * @param str
  61. * @param obj
  62. * @return
  63. * @throws Exception
  64. */
  65. public Object batchDelete(String str, List objs )throws Exception{
  66. return sqlSessionTemplate.delete(str, objs);
  67. }
  68. /**
  69. * 删除对象
  70. * @param str
  71. * @param obj
  72. * @return
  73. * @throws Exception
  74. */
  75. public Object delete(String str, Object obj) throws Exception {
  76. return sqlSessionTemplate.delete(str, obj);
  77. }
  78. /**
  79. * 查找对象
  80. * @param str
  81. * @param obj
  82. * @return
  83. * @throws Exception
  84. */
  85. public Object findForObject(String str, Object obj) throws Exception {
  86. return sqlSessionTemplate.selectOne(str, obj);
  87. }
  88. /**
  89. * 查找对象
  90. * @param str
  91. * @param obj
  92. * @return
  93. * @throws Exception
  94. */
  95. public Object findForList(String str, Object obj) throws Exception {
  96. return sqlSessionTemplate.selectList(str, obj);
  97. }
  98. public Object findForMap(String str, Object obj, String key, String value) throws Exception {
  99. return sqlSessionTemplate.selectMap(str, obj, key);
  100. }
  101. }

3.那么怎么使用呢?


   
   
   
   
  1. public class UserService {
  2. @Resource(name = "daoSupport")
  3. private DaoSupport dao;
  4. /*
  5. *通过id获取数据
  6. */
  7. public User getUserAndRoleById(String userid) throws Exception {
  8. return (User) dao.findForObject( "UserMapper.getUserAndRoleById", "font-family: Arial, Helvetica, sans-serif;">userid);
  9. }
  10. }
4. UserMapper.xml中的配置



	


	
			
			
			
		
	
	
	
		ROLE_ID,
		ROLE_NAME,
		RIGHTS,
		PARENT_ID,
		ADD_QX,
		DEL_QX,
		EDIT_QX,
		CHA_QX
	
	
	
	
		#{ROLE_ID},
		#{ROLE_NAME},
		#{RIGHTS},
		#{PARENT_ID},
		#{ADD_QX},
		#{DEL_QX},
		#{EDIT_QX},
		#{CHA_QX}
	
	
	
	
		SYS_ROLE
	

	
	
	
	
	
	
	
		insert into 
		
		(
		
		) values (
			
		)
	
	
	
	
		update 
		
		set ROLE_NAME = #{ROLE_NAME}
		where ROLE_ID = #{ROLE_ID}
	
	
	
	
		delete from 
		
		where ROLE_ID=#{ROLE_ID}
	
	
	
	
		update 
		
		set RIGHTS=#{RIGHTS} 
		where ROLE_ID=#{ROLE_ID}
	
	
	
	
	
	
	
		update 
		 
		set RIGHTS=#{rights} 
		where PARENT_ID=#{ROLE_ID}
	
	
	
	
		update 
		 
		set ADD_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	
	
	
	
		update
		
		set DEL_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	
	
	
	
		update 
		 
		set EDIT_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	
	
	
	
		update 
		
		set CHA_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	
	
	

好了,就这样,是不是觉得很简单,直接释放出dao层,一个通用dao.

你可能感兴趣的:(Mybatis)