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<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", <span style= "font-family: Arial, Helvetica, sans-serif;">userid</span>);
  9. }
  10. }
4. UserMapper.xml中的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	
<mapper namespace="RoleMapper">

	<resultMap type="Role" id="roleResultMap">
			<id column="ROLE_ID" property="ROLE_ID"/>
			<result column="ROLE_NAME" property="ROLE_NAME"/>
			<result column="RIGHTS" property="RIGHTS"/>
		</resultMap>
	
	<!-- 字段 -->
	<sql id="Field">
		ROLE_ID,
		ROLE_NAME,
		RIGHTS,
		PARENT_ID,
		ADD_QX,
		DEL_QX,
		EDIT_QX,
		CHA_QX
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{ROLE_ID},
		#{ROLE_NAME},
		#{RIGHTS},
		#{PARENT_ID},
		#{ADD_QX},
		#{DEL_QX},
		#{EDIT_QX},
		#{CHA_QX}
	</sql>
	
	<!--表名 -->
	<sql id="tableName">
		SYS_ROLE
	</sql>
<!-- 列出此组下的角色 -->
	<select id="listAllRolesByPId" resultMap="roleResultMap">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		where
			PARENT_ID = #{ROLE_ID}
		ORDER BY ROLE_ID
	</select>
	
	<!-- 通过id查找 -->
	<select id="findObjectById" parameterType="pd" resultType="pd">
		select  
		<include refid="Field"></include>
		from 
		<include refid="tableName"></include>
		where ROLE_ID = #{ROLE_ID}
	</select>
	
	<!-- 添加 -->
	<insert id="insert" parameterType="pd">
		insert into 
		<include refid="tableName"></include>
		(
		<include refid="Field"></include>
		) values (
		<include refid="FieldValue"></include>	
		)
	</insert>
	
	<!-- 保存修改 -->
	<update id="edit" parameterType="pd">
		update 
		<include refid="tableName"></include>
		set ROLE_NAME = #{ROLE_NAME}
		where ROLE_ID = #{ROLE_ID}
	</update>
	
	<!-- 删除角色  -->
	<delete id="deleteRoleById" parameterType="String">
		delete from 
		<include refid="tableName"></include>
		where ROLE_ID=#{ROLE_ID}
	</delete>
	
	<!-- 给当前角色附加菜单权限  -->
	<update id="updateRoleRights" parameterType="Role">
		update 
		<include refid="tableName"></include>
		set RIGHTS=#{RIGHTS} 
		where ROLE_ID=#{ROLE_ID}
	</update>
	
	<!-- 通过id查找 -->
	<select id="getRoleById" parameterType="String" resultMap="roleResultMap">
		select * from 
		<include refid="tableName"></include>
		where ROLE_ID=#{ROLE_ID}
	</select>
	
	<!-- 给全部子角色加菜单权限 -->
	<update id="setAllRights" parameterType="pd">
		update 
		<include refid="tableName"></include> 
		set RIGHTS=#{rights} 
		where PARENT_ID=#{ROLE_ID}
	</update>
	
	<!-- 新增权限 -->
	<update id="add_qx" parameterType="pd">
		update 
		<include refid="tableName"></include> 
		set ADD_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	</update>
	
	<!-- 删除权限 -->
	<update id="del_qx" parameterType="pd">
		update
		<include refid="tableName"></include>
		set DEL_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	</update>
	
	<!-- 修改权限 -->
	<update id="edit_qx" parameterType="pd">
		update 
		<include refid="tableName"></include> 
		set EDIT_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	</update>
	
	<!-- 查看权限 -->
	<update id="cha_qx" parameterType="pd">
		update 
		<include refid="tableName"></include>
		set CHA_QX=#{value} 
		where ROLE_ID=#{ROLE_ID}
	</update>
	
	</mapper>

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值