📄 rolemanageimpl.java
字号:
/**
*
*/
package com.seavision.PermissionManage.components;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import net.sf.hibernate.HibernateException;
import com.seavision.PermissionManage.help.Constants;
import com.seavision.PermissionManage.help.HibernateUtil;
import com.seavision.PermissionManage.vo.RoleAndFunctionVO;
import com.seavision.PermissionManage.vo.RoleVO;
/**
* @author Administrator
*
*/
public class RoleManageImpl implements RoleManage {
/**
* 1.获取角色列表
* 从role表中读取信息,封装到RoleVO对象中。
* 成功返回List,失败返回null。
*/
public List getRoleList(){
List result=null;
String hql = "from RoleVO";
try {
//调用HibernateUtil方法进行查询
result = HibernateUtil.queryHQL(hql);
} catch (HibernateException e) {
e.printStackTrace();
}
if (null == result || result.size() == 0) {
return result;
} else {
return result;
}
}
// Connection conn=HibernateUtil.getConnection();
// List result=null;
// String sql = "select * from role ";
// try {
// Statement stm=conn.createStatement();
// ResultSet rs=stm.executeQuery(sql);
//
//
//
// while (rs.next()) {
// RoleVO roleVo = new RoleVO();
// roleVo.setRoleId(rs.getInt("roleId"));
// roleVo.setRoleName(rs.getString("roleName"));
// roleVo.setRoleNumber(rs.getInt("roleNumber"));
// result.add(roleVo);
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }finally{
// try {
// conn.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// return result;
// }
/**
* 2.获取角色信息
* @param roleId
* 根据参数从role表中读取信息,封装到RoleVO中。
* 成功返回RoleVO对象,失败返回null。
*/
public RoleVO getRole(String roleId){
RoleVO roleVo = (RoleVO) HibernateUtil.getVOByID(
RoleVO.class, Integer.parseInt(roleId));
return roleVo;
}
/**
* 3.获取角色功能点
*
* 从roleAndFunction表中读取信息,
* 封装到roleAndFunctionVO对象中。成功返回List,失败返回null。
*/
public List getRoleOfFunction(int roleId){
Connection conn = HibernateUtil.getConnection();
List result = new ArrayList();
String sql="select functionName,roleId from functionandrole where roleId =" + roleId ;
try {
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
System.out.println("sql===" + sql);
while(rs.next()){
RoleAndFunctionVO roleAndFunctionVo = new RoleAndFunctionVO();
System.out.println("rs.getString(functionName)======================dfsd========"+rs.getString("functionName"));
roleAndFunctionVo.setFunctionName(rs.getString("functionName"));
roleAndFunctionVo.setRoleId(rs.getInt("roleId"));
result.add(roleAndFunctionVo);
}
rs.close();
stm.close();
return result;
}catch(Exception e){
e.printStackTrace();
}finally{
// try {
// conn.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
return result;
}
/**
* 4.新增角色
* @param roleVO
* @param list
*将RoleVO对象新增到role表中,list新增到roleAndFunction表中。
*两次新增用事务处理机制。成功返回SUCCESS,失败返回FALSE。
*/
public String saveRole(RoleVO roleVO,List list){
String[] str = new String[100];
boolean commit = true;
try{
HibernateUtil.beginTransaction();
}
catch(HibernateException e){
e.printStackTrace();
}
HibernateUtil.save(roleVO);
int roleId = roleVO.getRoleId();
Connection conn = HibernateUtil.getConnection();
for(int i = 0;i < list.size();i++){
str[i] = String.valueOf(list.get(i));
}
try{
Statement stm = conn.createStatement();
if (!list.isEmpty()){
for(int j = 0;j < list.size();j++){
String sql="insert into functionandrole (roleId,functionName) values("+roleId+",'"+str[j]+"')";
stm.execute(sql);
System.out.println("sql=" + sql);
}
}
stm.close();
}catch(Exception e){
commit = false;
e.printStackTrace();
}
try{
HibernateUtil.endTransaction(commit);}
catch(HibernateException e){
e.printStackTrace();
}
finally{
// try {
// conn.close();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
if(commit){
return Constants.SUCCESS;}
else{
return Constants.FAILURE;
}
}
/**
* 5.修改角色
* @param roleVO
* @param list
* 将RoleVO对象更新到role表中,list更新到roleAndFunction表中。
* 两次更新用事务处理机制。成功返回SUCCESS,失败返回FALSE。
*/
public String updateRole(RoleVO roleVO, List list) {
String[] a = new String[100];
boolean commit = true;
try {
HibernateUtil.beginTransaction();
} catch (HibernateException e) {
e.printStackTrace();
}
HibernateUtil.saveOrUpdate(roleVO);
int roleId = roleVO.getRoleId();
Connection conn = HibernateUtil.getConnection();
for (int i = 0; i < list.size(); i++) {
a[i] = String.valueOf(list.get(i));
System.out.println("a["+i+"] == " + a[i]);
}
try {
Statement stm = conn.createStatement();
if (!list.isEmpty()) {
//删除原来的纪录
String sql_delete = "delete from functionandrole where roleId=" + roleId;
stm.execute(sql_delete);
System.out.println("sql_delete===" + sql_delete);
for (int j = 0; j < list.size(); j++) {
//添加新纪录
String sql="insert into functionandrole (roleId,functionName) values("+roleId+",'"+a[j]+"')";
stm.execute(sql);
System.out.println("sql=" + sql);
}
stm.close();
}
} catch (Exception e) {
commit = false;
e.printStackTrace();
}
try {
HibernateUtil.endTransaction(commit);
} catch (HibernateException e) {
e.printStackTrace();
} finally {
// try {
// conn.close();
// } catch (SQLException e) {
// e.printStackTrace();
// }
}
if (commit) {
return Constants.SUCCESS;
} else {
return Constants.FAILURE;
}
}
/**
* 6.删除角色
*
* @param roleId
* 根据参数删除表role中信息,成功返回SUCCESS,失败返回FALSE。
*/
public String deleteRole(String roleId){
RoleVO aa = ( RoleVO) HibernateUtil.getVOByID(
RoleVO.class, Integer.parseInt(roleId));
String str=HibernateUtil.delete(aa);
return str;
}
public boolean checkNumber(int roleNumber){
List result = null;
String hql = null;
hql = "from RoleVO as r where r.roleNumber='" + roleNumber + "'";
try {
result = HibernateUtil.queryHQL(hql);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(result.isEmpty())
{
return false;
}
else
{
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -