Spring整合hibernate(5)之Service层的分析
一般中小型项目通常都会分为四个层
视图层(页面显示)
服务层(用来做整个项目的逻辑控制,异常处理,对象判断等都使用服务层)
DAO层(仅做对象的基本CRUD操作,不做任何判断和任何处理)
数据层(数据库,专门存储数据)
嵌套注入会报错,因此我们一般会抽取一层来专门做业务处理!
在文章Spring整合hibernate(4)之BaseDao的编写示例的基础上添加Service层等相关类;
此处源码下载地址:
具体实现代码:
IUserService类
package org.oms.spring.service; import java.util.List; import org.oms.spring.model.User; public interface IUserService { public void add(User user,int gid); public void delete(int id); public void uodate(User user); public User load(int id); public List<User> listAllUsers(); public List<User> list(int gid); }
UserService类
package org.oms.spring.service; import java.util.List; import javax.annotation.Resource; import org.oms.spring.dao.GroupHibernateDao; import org.oms.spring.dao.IGroupDao; import org.oms.spring.dao.IUserDao; import org.oms.spring.dao.UserHibernateDao; import org.oms.spring.exception.UserException; import org.oms.spring.model.Group; import org.oms.spring.model.User; import org.springframework.stereotype.Service; @Service("userService") public class UserService implements IUserService { private IUserDao userHibernateDao; public IUserDao getUserHibernateDao() { return userHibernateDao; } @Resource public void setUserHibernateDao(IUserDao userHibernateDao) { this.userHibernateDao = userHibernateDao; } public IGroupDao getGroupHibernateDao() { return groupHibernateDao; } @Resource public void setGroupHibernateDao(IGroupDao groupHibernateDao) { this.groupHibernateDao = groupHibernateDao; } private IGroupDao groupHibernateDao; @Override public void add(User user, int gid) { Group group=groupHibernateDao.load(gid); if(group==null) throw new UserException("添加的用户组不存在"); user.setGroup(group); userHibernateDao.add(user); } @Override public void delete(int id) { userHibernateDao.delete(id); } @Override public void uodate(User user) { userHibernateDao.uodate(user); } @Override public User load(int id) { return userHibernateDao.load(id); } @Override public List<User> listAllUsers() { String hql="from User"; return userHibernateDao.list(hql); } @Override public List<User> list(int gid) { String hql="from User where goup.id=?"; return userHibernateDao.list(hql, gid); } }IGroupService类
package org.oms.spring.service; import java.util.List; import org.oms.spring.model.Group; public interface IGroupService { public void add(Group group); public void delete(int id); public void update(Group group); public Group load(int id); public List<Group> list(); } <pre name="code" class="java">
GroupService类
<pre name="code" class="java">package org.oms.spring.service; import java.util.List; import javax.annotation.Resource; import org.oms.spring.dao.GroupHibernateDao; import org.oms.spring.dao.IGroupDao; import org.oms.spring.dao.IUserDao; import org.oms.spring.exception.UserException; import org.oms.spring.model.Group; import org.springframework.stereotype.Service; @Service("groupService") public class GroupService implements IGroupService { private IGroupDao groupHibernateDao; private IUserDao userHibernateDao; public IUserDao getUserHibernateDao() { return userHibernateDao; } @Resource public void setUserHibernateDao(IUserDao userHibernateDao) { this.userHibernateDao = userHibernateDao; } public IGroupDao getGroupHibernateDao() { return groupHibernateDao; } @Resource public void setGroupHibernateDao(IGroupDao groupHibernateDao) { this.groupHibernateDao = groupHibernateDao; } @Override public void add(Group group) { groupHibernateDao.add(group); } @Override public void delete(int id) { long count=userHibernateDao.getGroupUserCount(id); if (count>0) throw new UserException("删除的组还有用户"); groupHibernateDao.delete(id); } @Override public void update(Group group) { groupHibernateDao.uodate(group); } @Override public Group load(int id) { return groupHibernateDao.load(id); } @Override public List<Group> list() { String hql="from Group"; return groupHibernateDao.list(hql); } }
在次创建了一个异常类,具体代码如下:
package org.oms.spring.exception; public class UserException extends RuntimeException { public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String arg0, Throwable arg1) { super(arg0, arg1); // TODO Auto-generated constructor stub } public UserException(String arg0) { super(arg0); // TODO Auto-generated constructor stub } public UserException(Throwable arg0) { super(arg0); // TODO Auto-generated constructor stub } }
package org.oms.spring.dao; import java.util.List; public interface IBaseDao<T> { public void add(T t); public void delete(int id); public void uodate(T t); public T load(int id); public List<T> list(String hql, Object[] args); public List<T> list(String hql); public List<T> list(String hql, Object args); }
项目结果图:
个人备忘笔记~~~