ehcache.xml:
updateCheck="false">
2.applicationContext-ehcache.xml
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
3.CacheService implements InitializingBean
package com.tianjian.property.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tianjian.property.entity.system.Admin;
import com.tianjian.property.entity.system.Department;
import com.tianjian.property.entity.system.Position;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
@Service
public class CacheService implements InitializingBean {
@Autowired
private DepartmentService departmentService;
@Autowired
private AdminService adminService;
@Autowired
private PositionService positionService;
//设置机构缓存 格式为
public void setDepartment(){
CacheManager manager = CacheManager.create();
Cache cache=manager.getCache("ORGANAZATION");
List
Map
for(int i=0;i
}
Element element=new Element("org",map);
cache.put(element);
if(cache!=null)
cache.removeAll();
}
//岗位缓存
public void setPosition(){
CacheManager manager = CacheManager.create();
Cache cache=manager.getCache("ORGANAZATION");
List
Map
for(int i=0;i
}
Element element=new Element("position",map);
cache.put(element);
if(cache!=null)
cache.removeAll();
}
public void cacheInit(){
CacheManager manager = CacheManager.create();
Cache cache=manager.getCache("ORGANAZATION");
if(cache.get("org")==null){
setDepartment();
}
if(cache.get("position")==null){
setPosition();
}
}
//根据机构编号获取机构
public Department getDepartmentByDeptCode(String deptCode){
CacheManager manager = CacheManager.create();
Cache cache=manager.getCache("ORGANAZATION");
Map
Department dept=org.get(deptCode);
return dept;
}
//获取所有部门
public List
return departmentService.getAllDepartment();
}
public List
List
if(!child){
return departmentService.getChildDepartment(deptCode);
}else{
departments.addAll(departmentService.getChildDepartment(deptCode));
List
for(Department dept:list){
departments.addAll(getChildDepartment(dept.getDeptCode(),true));
}
return departments;
}
}
//根据部门code获取该部门下所有人员 child是否获取下级人员
public List
List
if(!child){
adminList.addAll(adminService.getAdminByDeptCode(deptCode));
}
if(child){
adminList.addAll(adminService.getAdminByDeptCode(deptCode));
List
for(Department dept:list){
adminList.addAll(getAdminByDeptCode(dept.getDeptCode(),true));
}
}
return adminList;
}
//获取该岗位下所有成员
public List
CacheManager manager = CacheManager.create();
Cache cache=manager.getCache("ORGANAZATION");
Map
Position position=positionCache.get(deptCode+"-"+positionCode);
List
if(position!=null){
JSONArray json=new JSONArray(position.getPersons());
List
for(int i=0;i
}
admin.addAll(adminService.getAdminByGuid(guids));
}
return admin;
}
@Override
public void afterPropertiesSet() throws Exception {
cacheInit();
}
}
4.DepartmentService
package com.tianjian.property.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tianjian.property.dao.system.DepartmentDao;
import com.tianjian.property.entity.system.Department;
@Service
@Transactional
public class DepartmentService {
@Autowired
private DepartmentDao deptDao;
public List
return deptDao.getDepartmentByCode(deptCode);
}
public List
List
return list;
}
@Cacheable(value="ORGANAZATION", key="deptAll")
public List
return deptDao.getAllDepartment();
}
@Cacheable(value="ORGANAZATION", key="#deptCode+'Child'")
public List
return deptDao.getChildDepartment(deptCode);
}
/*public List
List
return list;
}*/
public List
List
return list;
}
}
maven依赖: