📄 roolserviceimpl.java
字号:
package com.chinatelecom.service.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.chinatelecom.mode.TRight;
import com.chinatelecom.mode.TRole;
import com.chinatelecom.service.IRoolService;
/**
* 权限管理模块
*
* @author 李海冰
*
*/
public class RoolServiceImpl implements IRoolService {
SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private static int ROWS_PER_PAGE = 4;
/**
* 得到总页数
*/
public int getTotalPages() {
// TODO Auto-generated method stub
int count = this.getTotalCounts();
// System.out.println("count" + count);
if ((count % ROWS_PER_PAGE) == 0) {
return count / ROWS_PER_PAGE;
} else {
return count / ROWS_PER_PAGE + 1;
}
}
/**
* 新增角色
*/
public void addRole(TRole role) {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
template.save(role);
}
/**
* 得到角色列表
*/
public List findAllRoles(int page) {
int startPage = (page - 1) * ROWS_PER_PAGE;
Session session = this.getSessionFactory().openSession();
Query q = session.createQuery("from TRole");
q.setFirstResult(startPage);
q.setMaxResults(ROWS_PER_PAGE);
List list = q.list();
if (session.isOpen()) {
session.close();
}
return list;
}
/**
* 通过id查找角色
*/
public TRole findRoleById(String id) {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
TRole room = (TRole) template.get(TRole.class, Long.parseLong(id));
return room;
}
/**
* 查找角色列表
*/
public List findRoleList() {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
List list = template.find("select dept from TRole dept");
return list;
}
/**
* 得到总记录数
*/
public int getTotalCounts() {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
List list = template.find("select dept from TRole dept");
return list.size();
}
/**
* deleteDept
*
* 删除
*
* 需要传递参数id,id为SLong类型的数组变量,根据str来删除机房 作者:李海冰
*/
public void deleteRole(Long[] id) {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
for (int i = 0; i < id.length; i++) {
TRole room = (TRole) template.get(TRole.class, id[i]);
template.delete(room);
}
}
/**
* 更新角色
*/
public void updaterole(String id, String str) {
// TODO Auto-generated method stub
HibernateTemplate template = new HibernateTemplate(sessionFactory);
TRole room = (TRole) template.get(TRole.class, Long.parseLong(id));
room.setRoleFunction(str);
template.update(room);
}
/**
* 配置角色
*/
public void addRightList(String[] id, String userid) {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
for (int i = 0; i < id.length; i++) {
TRole room = (TRole) template.get(TRole.class, Long
.parseLong(id[i]));
TRight right = new TRight();
right.setTRole(room);
right.setUserId(userid);
template.save(right);
}
}
/**
* 通过userid来查找属于该人员的角色列表
*/
public List findRoleByuserId(String userid) {
HibernateTemplate template = new HibernateTemplate(sessionFactory);
// System.out.println(userid);
List list = template
.find("select dept from TRight dept where dept.userId='"
+ userid + "'");
// System.out.println(userid + "dddddd");
return list;
}
/**
* 删除角色
*/
public void deleteRole(String userId) {
// TODO Auto-generated method stub
HibernateTemplate ht = new HibernateTemplate(sessionFactory);
List list = ht.find("from TRight t where t.userId = '" + userId + "'");
for (int i = 0; i < list.size(); i++) {
TRight right = (TRight) list.get(i);
ht.delete(right);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -