RoleDaoImpl.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package me.zhengjie.dao.jdbc.impl;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.jdbc.core.JdbcTemplate;
  8. import org.springframework.jdbc.core.RowCallbackHandler;
  9. import org.springframework.jdbc.core.RowMapper;
  10. import org.springframework.stereotype.Repository;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import me.zhengjie.dao.jdbc.RoleDao;
  13. import me.zhengjie.application.bank.controller.vo.Role;
  14. @Repository
  15. @Transactional
  16. public class RoleDaoImpl implements RoleDao {
  17. @Autowired
  18. private JdbcTemplate jdbcTemplate;
  19. /**
  20. * 根据登录账户查询账户列表
  21. *
  22. * @param accountId
  23. * @param request
  24. * @return
  25. */
  26. @Override
  27. public List<Role> selectRoleByAccountId(Integer accountId, HttpServletRequest request) {
  28. String sql = "select * from `role` where `accountId`=" + accountId + " order by `sort` asc,`id` desc";
  29. return this.jdbcTemplate.query(sql, new RowMapper<Role>() {
  30. @Override
  31. public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
  32. Role role = new Role();
  33. role.setId(rs.getInt("id"));
  34. role.setAccountId(rs.getInt("accountId"));
  35. role.setSort(rs.getInt("sort"));
  36. role.setTitle(rs.getString("title"));
  37. role.setMenuId(rs.getString("menuId"));
  38. role.setCreateTime(rs.getDate("createTime"));
  39. role.setUpdateTime(rs.getDate("updateTime"));
  40. return role;
  41. }
  42. });
  43. }
  44. /**
  45. * 查询角色详情
  46. *
  47. * @param id
  48. * @return
  49. */
  50. @Override
  51. public Role selectRoleDetailById(Integer id) {
  52. String sql = "select * from `role` where `id`=? order by id asc";
  53. Role role = new Role();
  54. Object[] arr = new Object[] { id };
  55. this.jdbcTemplate.query(sql, arr, new RowCallbackHandler() {
  56. @Override
  57. public void processRow(ResultSet rs) throws SQLException {
  58. role.setId(rs.getInt("id"));
  59. role.setTitle(rs.getString("title"));
  60. role.setMenuId(rs.getString("menuId"));
  61. }
  62. });
  63. return role;
  64. }
  65. /**
  66. * 修改角色信息
  67. *
  68. * @param role
  69. */
  70. @Override
  71. public void modifyRoleDetailById(Role role) {
  72. String menuId = "";
  73. if (role.getMenuId().equals("")) {
  74. menuId = null;
  75. } else {
  76. menuId = "0," + role.getMenuId() + ",0";
  77. }
  78. String sql = "update `role` set title=? , menuId=? where id=?";
  79. this.jdbcTemplate.update(sql, role.getTitle(), menuId, role.getId());
  80. }
  81. /**
  82. * 删除角色
  83. *
  84. * @param role
  85. */
  86. @Override
  87. public void deleteRoleDetailById(Role role) {
  88. String sql = "delete from `role` where id=?";
  89. this.jdbcTemplate.update(sql, role.getId());
  90. }
  91. /**
  92. * 创建角色
  93. *
  94. * @param role
  95. */
  96. @Override
  97. public void insertRoleDetailById(Role role) {
  98. String menuId = "";
  99. if (role.getMenuId().equals("")) {
  100. menuId = null;
  101. } else {
  102. menuId = "0," + role.getMenuId() + ",0";
  103. }
  104. String sql = "insert into `role` (accountId,title,menuId,createTime) VALUES (?,?,?,?)";
  105. this.jdbcTemplate.update(sql, role.getAccountId(), role.getTitle(), menuId, role.getCreateTime());
  106. }
  107. }