📄 roledao.java
字号:
package com.oa.lp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import com.oa.lp.model.Menu;
import com.oa.lp.model.Role;
import com.oa.lp.util.DTOPopulator;
import com.oa.lp.util.PageList;
import com.oa.lp.util.Pages;
public class RoleDAO {
private Connection conn;
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
/**
* 新增角色
* @param menu
* @throws SQLException
*/
public void addRole(Role role) throws SQLException{
String sql = "insert into SYS_ROLE(ROLE_NAME,ROLE_DESCI,ROLE_STATE)values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,role.getRoleName());
pstmt.setString(2,role.getRoleDesci());
pstmt.setInt(3,role.getRoleState());
pstmt.executeUpdate();
pstmt.close();
}
/**
* 更新角色
*
*/
public void updateRole(Role role) throws SQLException{
String sql = "update SYS_ROLE set ROLE_NAME=?,ROLE_DESCI=?,ROLE_STATE=? where ROLE_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,role.getRoleName());
pstmt.setString(2,role.getRoleDesci());
pstmt.setInt(3,role.getRoleState());
pstmt.setInt(4,role.getRoleId());
pstmt.executeUpdate();
pstmt.close();
}
/**
* 通过ID查找
*/
public Role getById(int roleId) throws Exception{
Role role = null;
String sql = "select * from SYS_ROLE where ROLE_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,roleId);
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
List list = DTOPopulator.populate(rs, Role.class);
//取集合中的第一个元素返回
if(list.size()>0){
role=(Role)list.get(0);
}
rs.close();
pstmt.close();
return role;
}
/**
* 角色分页列表
* @param page
* @return
* @throws Exception
*/
public PageList listAllRole(Pages page) throws Exception{
PageList pageList = new PageList();
//总录数
page.setAllRecord(countAllRole());
page.doPage();
StringBuffer sql = new StringBuffer();
sql.append("select * from (");
sql.append("select top "+page.getPageSize()+" * from (");
sql.append("select top "+(page.getPageSize()*page.getCPage())+" * from ");
sql.append("SYS_ROLE order by ROLE_ID desc) t1 order by ROLE_ID asc) t2 order by ROLE_ID desc");
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
List list = DTOPopulator.populate(rs, Role.class);
pageList.setPage(page);
pageList.setObjectList(list);
pstmt.close();
return pageList;
}
/**
* 得到所有的角色但不分页
* @return
* @throws Exception
*/
public List listAllRole() throws Exception{
List list=null;
String sql="select *from SYS_ROLE order by ROLE_ID desc";
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
ResultSet rs = pstmt.executeQuery();
//将结果集中的每一行记录封装成一个对象,再放进集合返回
list=DTOPopulator.populate(rs, Role.class);
rs.close();
pstmt.close();
return list;
}
/**
* 角色总记录数
* @return
* @throws SQLException
*/
public int countAllRole() throws SQLException{
int count = 0;
String sql = "select count(*) from SYS_ROLE";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
count = rs.getInt(1);
}
pstmt.close();
return count;
}
/**
* 删除角色
*/
public boolean delRole(int roleId) throws SQLException{
boolean flag=true;
//是否给角色分配权限
String sql = "select count(*) from SYS_ROLE_POWER where ROLE_ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,roleId);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
if(rs.getInt(1)>0){
flag = false;
}
}
rs.close();
pstmt.close();
if(flag){
//删除
sql = "delete from SYS_ROLE where ROLE_ID=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,roleId);
pstmt.executeUpdate();
pstmt.close();
}
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -