//新增
public void save(Info entity);
//更新
public void update(Info enetity);
//根据id删除
public void delete(Serializable id);
//根据id查找
public Info findObjectById(Serializable id);
//查找列表
public List findObjects();
package cn.edu.hpu.tax.core.action;
import java.io.Serializable;
import java.util.List;
public interface BaseService {
//新增
public void save(T entity);
//更新
public void update(T enetity);
//根据id删除
public void delete(Serializable id);
//根据id查找
public T findObjectById(Serializable id);
//查找列表
public List findObjects();
}
package cn.edu.hpu.tax.core.service.impl;
import java.io.Serializable;
import java.util.List;
import cn.edu.hpu.tax.core.dao.BaseDao;
import cn.edu.hpu.tax.core.service.BaseService;
public class BaseServiceImpl implements BaseService{
private BaseDao baseDao;
@Override
public void save(T entity) {
baseDao.save(entity);
}
@Override
public void update(T enetity) {
baseDao.update(enetity);
}
@Override
public void delete(Serializable id) {
baseDao.delete(id);
}
@Override
public T findObjectById(Serializable id) {
return baseDao.findObjectById(id);
}
@Override
public List findObjects() {
return baseDao.findObjects();
}
}
package cn.edu.hpu.tax.info.service;
import cn.edu.hpu.tax.core.service.BaseService;
import cn.edu.hpu.tax.info.entity.Info;
public interface InfoService extends BaseService{
}
package cn.edu.hpu.tax.info.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.edu.hpu.tax.core.service.impl.BaseServiceImpl;
import cn.edu.hpu.tax.info.dao.InfoDao;
import cn.edu.hpu.tax.info.entity.Info;
import cn.edu.hpu.tax.info.service.InfoService;
@Service("infoService")
public class InfoServiceImpl extends BaseServiceImpl implements InfoService {
@Resource
private InfoDao infoDao;//留他是为了info以后自己的业务方法
}
package cn.edu.hpu.tax.info.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.edu.hpu.tax.core.service.impl.BaseServiceImpl;
import cn.edu.hpu.tax.info.dao.InfoDao;
import cn.edu.hpu.tax.info.entity.Info;
import cn.edu.hpu.tax.info.service.InfoService;
@Service("infoService")
public class InfoServiceImpl extends BaseServiceImpl implements InfoService {
private InfoDao infoDao;
@Resource
public void setInfoDao(InfoDao infoDao) {
super.setBaseDao(infoDao);
this.infoDao = infoDao;
}
}
public class BaseServiceImpl implements BaseService{
private BaseDao baseDao;
public void setBaseDao(BaseDao baseDao) {
this.baseDao=baseDao;
}
//下面代码不在赘述
}
package cn.edu.hpu.tax.role.service;
import cn.edu.hpu.tax.core.service.BaseService;
import cn.edu.hpu.tax.role.entity.Role;
public interface RoleService extends BaseService{
}
package cn.edu.hpu.tax.role.service.impl;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.edu.hpu.tax.core.service.impl.BaseServiceImpl;
import cn.edu.hpu.tax.info.dao.InfoDao;
import cn.edu.hpu.tax.role.dao.RoleDao;
import cn.edu.hpu.tax.role.entity.Role;
import cn.edu.hpu.tax.role.service.RoleService;
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements RoleService{
private RoleDao roleDao;
@Resource
public void setRoleDao(RoleDao roleDao) {
super.setBaseDao(roleDao);
this.roleDao = roleDao;
}
@Override
public void update(Role role) {
//1.删除该角色对应的所有权限
roleDao.deletePrivilegeByRoleId(role.getRoleId());
//2.更新角色及其权限
roleDao.update(role);
}
}
package cn.edu.hpu.tax.user.service;
import java.io.File;
import java.io.Serializable;
import java.util.List;
import javax.servlet.ServletOutputStream;
import cn.edu.hpu.tax.core.exception.ServiceException;
import cn.edu.hpu.tax.core.service.BaseService;
import cn.edu.hpu.tax.info.entity.Info;
import cn.edu.hpu.tax.user.entity.User;
public interface UserService extends BaseService{
//导出列表
public void exportExcel(List userList,
ServletOutputStream outputStream);
//导入用户列表
public void importExcel(File userExcel, String userExcelFileName);
//根据Id和账号查找用户
public List findUserByAccountAndId(String id, String account);
//保存用户和角色信息
public void saveUserAndRole(User user, String[] userRoleIds);
//修改用户和角色信息
public void updateUserAndRole(User user, String[] userRoleIds);
//根据用户id获取其所有角色的id
public String[] getRoleIdByUserId(String id);
//根据用户账号密码获取用户列表
public List findUserByAccountAndPassword(String account,
String password);
}
package cn.edu.hpu.tax.user.service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import cn.edu.hpu.tax.core.service.impl.BaseServiceImpl;
import cn.edu.hpu.tax.core.util.ExcelUtil;
import cn.edu.hpu.tax.role.entity.Role;
import cn.edu.hpu.tax.user.dao.UserDao;
import cn.edu.hpu.tax.user.entity.User;
import cn.edu.hpu.tax.user.entity.UserRole;
import cn.edu.hpu.tax.user.entity.UserRoleId;
import cn.edu.hpu.tax.user.service.UserService;
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService{
private UserDao userDao;
@Resource
public void setUserDao(UserDao userDao) {
super.setBaseDao(userDao);
this.userDao = userDao;
}
@Override
public void delete(Serializable id) {
userDao.delete(id);
//删除用户对应的所有权限
userDao.deleteUserRoleByUserId(id.toString());
}
@Override
public void exportExcel(List userList,
ServletOutputStream outputStream) {
ExcelUtil.exportExcel(userList, outputStream);
}
@Override
public void importExcel(File userExcel, String userExcelFileName) {
try {
FileInputStream fileInputStream = new FileInputStream(userExcel);
//判断是否是03版本的Excel(还是07的)
boolean is03Excel = userExcelFileName.matches("^.+\\.(?i)(xls)$");
//1、读取工作簿
Workbook workbook = is03Excel ? new HSSFWorkbook(fileInputStream)
: new XSSFWorkbook(fileInputStream);
//2、读取工作表
Sheet sheet=workbook.getSheetAt(0);
//3、读取行
if(sheet.getPhysicalNumberOfRows()>2){
User user=null;
for (int i = 2; i < sheet.getPhysicalNumberOfRows(); i++) {
//4、读取单元格
Row row=sheet.getRow(i);
user=new User();
//用户名
Cell cell1=row.getCell(0);
user.setName(cell1.getStringCellValue());
//账号
Cell cell2=row.getCell(1);
user.setAccount(cell2.getStringCellValue());
//所属部门
Cell cell3=row.getCell(2);
user.setDept(cell3.getStringCellValue());
//性别
Cell cell4=row.getCell(3);
user.setGender(cell4.getStringCellValue().equals("男"));
//电子邮箱
Cell cell5=row.getCell(4);
user.setEmail(cell5.getStringCellValue());
//导入用户的初始密码为123456
user.setPassword("123456");
//默认用户状态为有效
user.setState(User.USER_STATE_VALID);
//5、保存用户
userDao.save(user);
}
}
workbook.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public List findUserByAccountAndId(String id, String account) {
return userDao.findUserByAccountAndId(id,account);
}
@Override
public void saveUserAndRole(User user, String[] userRoleIds) {
//1.保存用户
save(user);
//2.保存用户对于的角色
if(userRoleIds!=null){
for (String roleId:userRoleIds) {
userDao.saveUserRole(new UserRole(new UserRoleId(user.getId(),new Role(roleId))));
}
}
}
@Override
public void updateUserAndRole(User user, String[] userRoleIds) {
//1.根据用户删除该用户的所有角色
userDao.deleteUserRoleByUserId(user.getId());
//2.更新用户
update(user);
//3.保存用户对应的角色
if(userRoleIds!=null){
for (String roleId:userRoleIds) {
userDao.saveUserRole(new UserRole(new UserRoleId(user.getId(),new Role(roleId))));
}
}
}
@Override
public String[] getRoleIdByUserId(String id) {
return userDao.getRoleIdByUserId(id);
}
@Override
public List findUserByAccountAndPassword(String account,
String password) {
return userDao.findUserByAccountAndPassword(account,password);
}
}
工程源码:HpuTax2.0.zip
转载请注明出处:http://blog.csdn.net/acmman/article/details/49864349