MVC下的DAO接口类和SERVICE接口类区别?
简单理解:
DAO数据库访问对象实现连接数据库修改、添加等细节
service服务层面向功能把一个整个服务细化调用DAO
其实service其中都是一些方法去调用DAO甚至方法名都和DAO中一样的
如某个service是用作用户注册的
其中可能包括检测用户名是否存在和插入用户数据两部分
分别调用DAO中具体实现操纵数据库
看起来逻辑更清晰而已
进一步说明:
Dao层实现是简单的CRUD操作。相当于sql中的单条select,insert,upate,delete语句。
而service层就是具体业务的实现。一般多次组合dao层的方法(dao层方法达到了重用目的),是多个数据库操作的集合,可以看做数据库中的存储过程,而且事务一般控制在service层。这些数据库操作一起提交或回滚。
当然,简单的系统完全可以不划分service层,只用dao层。但那这样的话,代码从用性就不高。
用户的Dao层
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142public
class
UserDaoHibernate
extends
BaseDaoHibernate
implements
IUserDao{
/**
*增加用户
*
*@paramuser
*/
public
LongaddUser(Useruser){
return
addEntityRetVal(user);
}
/**
*通过id删除用户
*
*@paramuser
*/
public
void
deleteUser(Longid){
UseruserPO=(User)getHibernateTemplate().load(
User.
class
,id);
deleteEntity(userPO);
}
/**
*删除用户
*
*@paramuser
*/
public
void
deleteUser(Useruser){
UseruserPO=(User)getHibernateTemplate().load(
User.
class
,user.getUserid());
deleteEntity(userPO);
}
/**
*更新用户
*
*@paramuser
*/
public
void
updateUser(Useruser){
UseruserPO=(User)getHibernateTemplate().load(
User.
class
,user.getUserid());
BeanUtil.copyProperties(userPO,user);
updateEntity(userPO);
}
/**
*通过id查询用户
*
*@paramid
*@return
*/
public
UserqueryUserById(Longid){
return
(User)getHibernateTemplate().load(User.
class
,id);
}
/**
*通过用户名字查询用户实体--这个方法存在SQL注入攻击问题
*@paramusernme
*@return
*/
public
UserqueryUserByName(Stringusername){
Stringhql=
"selectufromUseruwhereu.username='"
+username+
"'"
;
return
(User)
this
.queryObjectByHql(hql);
}
/**
*查询所有用户
*
*@return
*/
public
List<User>queryAllUser(){
return
queryAllEntitys(User.
class
);
}
/**
*分页查询用户
*/
public
List<User>queryAllUser(Stringhql,
int
currentPage,
int
pageSize){
return
queryAllEntitys(currentPage,pageSize,hql);
//调用的是有currentPage的分页方法
}
/**
*
*通过用户id查询用户名称,查不到返回null
*@paramid
*@return
*/
public
StringqueryNameById(Longid){
Stringhql=
"fromUserdwhered.userId=?"
;
List<?>users=getHibernateTemplate().find(hql,id);
if
(users.size()>
){
return
((User)users.get(
)).getUsername();
}
else
{
return
null
;
}
}
/**
*得到用户分页记录总数
*@paramparentId
*@return
*/
/*
publicLongqueryTotalNumberOfUser(){
Stringhql="selectcount(*)fromUser";
List<?>childNumber=getHibernateTemplate().find(hql);
return(Long)childNumber.get(0);
}*/
publicintqueryAllUserNubmer(Stringhql){
returnqueryAllEntitysNumer(hql);
}
/**
*查询用户的权限
*@paramuserId
*@return
*/
publicList<UserAuth>queryFunctionOnlyByUserId(LonguserId){
Stringhql="selectuafromUserAuthuawhereua.userid="+userId;
List<UserAuth>userAuths=queryAllObjectByHql(hql);
returnuserAuths;
}
/**
*查询用户的角色
*@paramuserId
*@return
*/
@SuppressWarnings
(
"unchecked"
)
public
List<UserRole>queryRoleOnlyByUserId(LonguserId){
Stringhql=
"selecturfromUserRoleurwhereur.userid="
+userId;
List<UserRole>userAuths=queryAllObjectByHql(hql);
return
userAuths;
}
}
service层,又可细化为查询业务UserHelper,还有增加,更新,删除业务集中在UserFacade中。这里贴出UserHelper.java
@WebService
public
class
UserHelper
implements
IUserHelper{
private
IUserDaouserDao=
null
;
private
IDepartmentDaodepartDao=
null
;
private
IFunctionHelperfunctionHelper=
null
;
private
IRoleHelperroleHelper=
null
;
public
void
setUserDao(IUserDaouserDao){
this
.userDao=userDao;
}
public
void
setFunctionHelper(IFunctionHelperfunctionHelper){
this
.functionHelper=functionHelper;
}
public
void
setDepartDao(IDepartmentDaodepartDao){
this
.departDao=departDao;
}
public
void
setRoleHelper(IRoleHelperroleHelper){
this
.roleHelper=roleHelper;
}
/**
*通过id查询权限,没有则返回null
*
*@paramid
*@return
*/
public
UserVOqueryUserById(Longid)
throws
GeneralException{
Useruser=
null
;
try
{
user=userDao.queryUserById(id);
}
catch
(Exceptione){
e.printStackTrace();
throw
new
GeneralException(
"error.userDeatil"
,
"通过id查询权限时出错!"
);
}
//PO转VO
UserVOuserVO=userPoToVo(user);
return
userVO;
}
/**
*得到所有权限的集合,没有则返回null
*
*@return
*/
public
List<UserVO>queryAllUser()
throws
GeneralException{
List<UserVO>allFuncVOs=
new
ArrayList<UserVO>();
List<User>allFuncs=
null
;
try
{
allFuncs=userDao.queryAllUser();
}
catch
(Exceptione){
throw
new
GeneralException(
"error.userList"
,
"得到所有权限的集合时出错!"
);
}
for
(Iterator<?>iterator=allFuncs.iterator();iterator.hasNext();){
Useruser=(User)iterator.next();
//PO转VO
UserVOuserVO=userPoToVo(user);
allFuncVOs.add(userVO);
}
return
allFuncVOs;
}
/**
*权限的PO到VO转换的方法
*
*@paramuser
*@return
*/
public
UserVOuserPoToVo(Useruser)
throws
GeneralException{
UserVOuserVO=
new
UserVO();
BeanUtil.copyProperties(userVO,user);
try
{
userVO.setDepartName(departDao.queryNameById(user.getDepartid()));
//设置部门名称
}
catch
(Exceptione){
throw
new
GeneralException(
"error.user"
,
"权限的PO到VO转换时出错!"
);
}
if
(userVO.getStatus().equals(
"1"
)){
userVO.setStatus(
"可用"
);
}
else
{
userVO.setStatus(
"不可用"
);
}
userVO.setRegisterName(
"ZHANG"
);
userVO.setChangerName(
"ZHANG"
);
return
userVO;
}
/**
*通过分页查询权限信息集合
*
*@paramhql
*@paramcurrentPage
*@parampageSize
*@return
*@throwsGeneralException
*/
public
List<UserVO>queryUserByPage(Stringhql,
int
currentPage,
int
pageSize)
throws
GeneralException{
List<UserVO>allFuncVOs=
new
ArrayList<UserVO>();
List<User>allFuncs=
null
;
try
{
allFuncs=userDao.queryAllUser(hql,currentPage,pageSize);
}
catch
(Exceptione){
throw
new
GeneralException(
"error.userList"
,
"分页得到权限的集合时出错!"
);
}
for
(Iterator<?>iterator=allFuncs.iterator();iterator.hasNext();){
Useruser=(User)iterator.next();
//PO转VO
UserVOuserVO=userPoToVo(user);
allFuncVOs.add(userVO);
}
return
allFuncVOs;
}
/**
*返回User分页对象
*
*@paramcurrentPage
*@return
*/
public
PaginationgetPagination(
int
currentPage,Stringhql){
return
new
Pagination(currentPage,
DisplayRecordCount.DISPLAY_IN_USER_LIST,userDao
.queryAllUserNubmer(hql));
}
/**
*查到用户的所有角色ID
*
*@paramuserId
*@return
*@throwsGeneralException
*/
public
List<Long>queryAllRoleidsOfUser(LonguserId)
throws
GeneralException{
List<Long>rolesOfUser=
new
ArrayList<Long>();
List<UserRole>userRoles=
null
;
try
{
userRoles=userDao.queryRoleOnlyByUserId(userId);
//查到角色权限
}
catch
(Exceptione){
throw
new
GeneralException(
"error.userRoleidsList"
,
"得到用户的角色ID集合出错!"
);
}
for
(Iterator<?>iterator=userRoles.iterator();iterator.hasNext();){
UserRoleuserRole=(UserRole)iterator.next();
Longroleid=userRole.getRoleid();
rolesOfUser.add(roleid);
}
return
rolesOfUser;
}
/**
*查到用户的所有角色
*
*@paramuserId
*@return
*@throwsGeneralException
*/
public
List<RoleVO>queryAllRoleOfUser(LonguserId)
throws
GeneralException{
List<Long>rolesOfUser=
new
ArrayList<Long>();
List<RoleVO>userRoles=
new
ArrayList<RoleVO>();
try
{
rolesOfUser=queryAllRoleidsOfUser(userId);
for
(Iterator<?>iterator=rolesOfUser.iterator();iterator
.hasNext();){
Longroleid=(Long)iterator.next();
RoleVOroleVO=roleHelper.queryRoleById(roleid);
userRoles.add(roleVO);
}
}
catch
(Exceptione){
e.printStackTrace();
throw
new
GeneralException(
"error.userRoleList"
,
"得到用户的角色集合出错!"
);
}
return
userRoles;
}
/**
*查询用户的所有权限1.查询所有用户的权限2.查询所有用户的角色3.查询所有用户的权限+角色的权限-共同的权限
*
*@paramuserId
*@return
*/
public
List<FunctionVO>queryAllFunctionOfUser(LonguserId,Stringsystem)
throws
GeneralException{
Set<FunctionVO>userAllFuncs=
new
HashSet<FunctionVO>();
List<FunctionVO>userAllFuncsList=
new
ArrayList<FunctionVO>();
try
{
List<UserAuth>userFuncs=userDao
.queryFunctionOnlyByUserId(userId);
//查到权限
if
(userFuncs!=
null
){
for
(Iterator<?>iterator=userFuncs.iterator();iterator
.hasNext();){
UserAuthuserFunc=(UserAuth)iterator.next();
LongfuncId=userFunc.getFuncid();
FunctionVOfuncVO=functionHelper
.queryFunctionById(funcId);
userAllFuncs.add(funcVO);
}
}
List<UserRole>userRoles=userDao.queryRoleOnlyByUserId(userId);
//查到角色
if
(userRoles!=
null
){
//查到所有角色的所有权限,将权限放入到userAllFuncs中
for
(Iterator<?>iterator=userRoles.iterator();iterator
.hasNext();){
UserRoleuserRole=(UserRole)iterator.next();
LongroleId=userRole.getRoleid();
List<FunctionVO>funcVOs=roleHelper
.queryFunctionOfRole(roleId);
for
(Iterator<?>iterator2=funcVOs.iterator();iterator2
.hasNext();){
FunctionVOfunctionVO=(FunctionVO)iterator2.next();
userAllFuncs.add(functionVO);
}
}
}
//将筛选了数据的无序Set集合转换为有序的List集合.一定要从小到大,否则权限树显示就会有问题
for
(Iterator<?>iterator=userAllFuncs.iterator();iterator
.hasNext();){
FunctionVOuserAllFun=(FunctionVO)iterator.next();
if
(system.equals(
"crm"
)){
if
(userAllFun.getFuncid()>=20000000l){
userAllFuncsList.add(userAllFun);
}
}
else
if
(system.equals(
"hr"
)){
if
(userAllFun.getFuncid()<20000000l){
userAllFuncsList.add(userAllFun);
}
}
}
FunctionComparatorfc=
new
FunctionComparator();
Collections.sort(userAllFuncsList,fc);
}
catch
(Exceptione){
e.printStackTrace();
throw
new
GeneralException(
"error.userAllFuncsList"
,
"得到用户的权限集合出错!"
);
}
return
userAllFuncsList;
}
}
dao层是sql的增删改查service层是给dao层得到结果添加业务逻辑
以‘用户登录’为例
dao层只负责查询用户名是username、密码是password的用户返回list
service层添加逻辑判断,list的size如果大于0,返回用户;size小于0,提示‘用户名或密码错误’
http://shenzhen.offcn.com/
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。