决定一个人的一生,以及整个命运的,只是一瞬之间。—–歌德
项目各个源程序包的作用和类
com/base/aop/SimpleProfiler.java代码:
package com.base.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.core.Ordered;
import org.springframework.util.StopWatch;
public class SimpleProfiler implements Ordered {
private int order;
public int getOrder() {
return this.order;
}
public void setOrder(int order) {
this.order = order;
}
public Object profile(ProceedingJoinPoint call) throws Throwable {
Object returnValue;
StopWatch clock = new StopWatch(getClass().getName());
try {
clock.start(call.toShortString());
returnValue = call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
return returnValue;
}
}
com/base/common/Constant.java代码:
package com.base.common;
public class Constant {
public final static String USER_SESSON_KEY = "USER_SESSION";
public final static String USER_MENU_KEY = "USER_MENU";
public final static Integer STATUS_ENABLE = 1;
public final static Integer STATUS_DISABLE = 0;
public final static Integer BOOLEAN_YES = 1;
public final static Integer BOOLEAN_NO = 0;
public final static Integer PAGE_SIZE = 10;
public final static String ROLE_PATIENT = "patient";
public final static String ROLE_DOCTOR = "doctor";
public final static String ROLE_NURSE = "nurse";
public final static String ROLE_ADMIN = "hospital_admin";
public final static String ROLE_MONITOR_ADMIN = "monitor_admin";
public final static String URL_KEY = "f5253d3e727685745c59b697babcc080";
public final static String LOGS_FUNCTION_LOGIN = "user_login";
}
com/base/controller/BasicController.java代码:
package com.base.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
@Controller
public class BasicController {
protected transient Logger logger = LoggerFactory.getLogger(getClass());
/**
*通用操作返回错误
*/
public static String RESULT_ERROR = "error";
/**
*通用操作返回成功
*/
public static String RESULT_SUCCESS = "success";
public static String RESULT_ADD_SUCCESS = "add_success";
public static String RESULT_EDIT_SUCCESS = "edit_success";
public static String RESULT_DELETE_SUCCESS = "delete_success";
/**
* 操作结果常量
*/
public static String OPE_RESULT = "ope_result";
/**
* 操作结果流水号
*/
public static String OPE_NUMBER = "ope_number";
}
com/base/dao/IBasicDao.java及实现类MybatisBasicDao.java代码
package com.base.dao;
import com.base.exception.DaoException;
import com.base.util.Criteria;
import org.springframework.data.domain.Page;
import java.util.List;
import java.util.Map;
/**
* 持久层基本的接口定义 所有的DAO接口都必须继承该接口
*/
public interface IBasicDao<T> {
/**
* 从数据库读取所有对象
*
* @return list 对象的记录集合
* @throws com.base.exception.DaoException
*/
public List<T> getAll() throws DaoException;
/**
* 从数据库读取所有对象
*
* @param first 第一条记录
* @param limit 最大记录数
* @return 包含对象的记录集合
* @throws com.base.exception.DaoException
*/
public List<T> getList(int first, int limit) throws DaoException;
/**
* 得到对象的总数量
*
* @return 对象的总数量
* @throws com.base.exception.DaoException
*/
public Integer getCount() throws DaoException;
/**
* 根据id得到符合条件的该对象,对象不存在时返回空
*
* @param key 对象的ID
* @return 符合条件的该对象
* @throws com.base.exception.DaoException
*/
public T getObject(Long key) throws DaoException;
/**
* 将对象信息持久化
*
* @param object
* @throws com.base.exception.DaoException
*/
public void create(T object) throws DaoException;
/**
* 修改对象信息
*
* @param object
* @throws com.base.exception.DaoException
*/
public void update(T object) throws DaoException;
/**
* 根据条件删除对象的信息
*
* @param id 要删除对象的ID
* @throws com.base.exception.DaoException
*/
public void delete(int id) throws DaoException;
/**
* 简单查询,将map中的条件组合进行"="查询
* @param map 查询条件,其中key是字段名,value是条件
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.DaoException
*/
public T findOne(Map<String, Object> map) throws DaoException;
/**
* 根据对象进行单一查询
* @param object 查询的对象
* @return 符合条件的单一对象
* @throws DaoException
*/
public T findOne(T object) throws DaoException;
/**
* 简单查询,将map中的条件组合进行"="查询
* @param map 查询条件,其中key是字段名,value是条件
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.DaoException
*/
public List<T> find(Map<String, Object> map) throws DaoException;
/**
* 根据对象进行列表查询
* @param object 查询的对象
* @return 符合条件的对象列表
* @throws DaoException
*/
public List<T> find(T object) throws DaoException;
/**
* 根据in条件查询
*
* @param map 查询条件,多个值时请用,分割
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.DaoException
*/
public List<T> findByIn(Map<String, Object> map) throws DaoException;
/**
* 得到符合条件的该对象,用Like进行查询
*
* @param map 查询条件,其中key是字段名,value是条件
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.DaoException
*/
public List<T> findByLike(Map<String, Object> map) throws DaoException;
/**
* 根据传入的Criteria条件查询,返回结果集
*
* @param c 传入的Criteria条件查询对象
* @return 返回符合条件的结果集
* @throws com.base.exception.DaoException
*/
public Page<T> getList(final Criteria c) throws DaoException;
/**
* 根据传入的Criteria条件查询,返回结果数量
*
* @param c 传入的Criteria条件查询对象
* @return 结果数量
* @throws com.base.exception.DaoException
*/
public Integer getCount(Criteria c) throws DaoException;
}
package com.base.dao;
import com.base.exception.DaoException;
import com.base.util.Criteria;
import com.base.util.Parameter;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.util.*;
public abstract class MybatisBasicDao<T> implements IBasicDao<T> {
protected transient final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
protected SqlSession sqlSession;
protected Class<T> modelClass;
private String namespace;
public MybatisBasicDao(Class<T> modelClass){
this.modelClass = modelClass;
this.namespace = modelClass.getName();
}
public List<T> getAll() throws DaoException {
return this.sqlSession.selectList(namespace + ".selectAll");
}
public List<T> getList(int first, int limit) throws DaoException {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("offset", first);
map.put("limit", limit);
return this.sqlSession.selectList(namespace + ".selectSome",map);
}
public Integer getCount() throws DaoException {
return (Integer)this.sqlSession.selectOne(namespace + ".getTotalCount");
}
public T getObject(Long key) throws DaoException {
return (T)this.sqlSession.selectOne(namespace + ".getObject", key);
}
public void create(T object) throws DaoException {
this.sqlSession.insert(namespace + ".insert", object);
}
public void update(T object) throws DaoException {
this.sqlSession.update(namespace + ".update", object);
}
public void delete(int id) throws DaoException {
this.sqlSession.delete(namespace + ".delete", id);
}
public T findOne(Map<String, Object> map) throws DaoException {
StringBuilder sb = new StringBuilder();
List<Parameter> params = new ArrayList<Parameter>();
Set<String> keys = map.keySet();
for(String key : keys){
params.add(new Parameter(key, map.get(key)));
}
return (T)this.sqlSession.selectOne(namespace + ".findOneByMap",params);
}
public T findOne(T object) throws DaoException {
return (T)this.sqlSession.selectOne(namespace + ".findOneByObject",object);
}
public List<T> find(Map<String, Object> map) throws DaoException {
StringBuilder sb = new StringBuilder();
List<Parameter> params = new ArrayList<Parameter>();
Set<String> keys = map.keySet();
for(String key : keys){
params.add(new Parameter(key, map.get(key)));
}
return this.sqlSession.selectList(namespace + ".findByMap",params);
}
public List<T> find(T object) throws DaoException {
return this.sqlSession.selectList(namespace + ".findByObject",object);
}
public List<T> findByIn(Map<String, Object> map) throws DaoException {
StringBuilder sb = new StringBuilder();
List<Parameter> params = new ArrayList<Parameter>();
Set<String> keys = map.keySet();
for(String key : keys){
params.add(new Parameter(key, map.get(key)));
}
return this.sqlSession.selectList(namespace + ".findByIn",params);
}
public List<T> findByLike(Map<String, Object> map) throws DaoException {
StringBuilder sb = new StringBuilder();
List<Parameter> params = new ArrayList<Parameter>();
Set<String> keys = map.keySet();
for(String key : keys){
params.add(new Parameter(key, map.get(key)));
}
return this.sqlSession.selectList(namespace + ".findByLike",params);
}
public Page<T> getList(Criteria c) throws DaoException {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("fieldsClause", c.getFieldClause());
map.put("whereClause", c.getWhereClause());
map.put("orderClause", c.getOrderClause());
map.put("offset", String.valueOf(c.getStart()));
map.put("limit", String.valueOf(c.getFetchSize()));
int count = getCount(c);
List<T> list = this.sqlSession.selectList(namespace + ".query",map);
final Criteria cri = c;
Page<T> page = new PageImpl<T>(list, new Pageable() {
public int getPageNumber() {
return cri.getPageNo();
}
public int getPageSize() {
return cri.getFetchSize();
}
public int getOffset() {
return cri.getStart();
}
public Sort getSort() {
List<String> orders = cri.getOrder();
if (orders.size()==0) return null;
String order = orders.get(0);
String[] splits = order.split(" ");
if (splits.length>1){
if ("asc".equals(splits[1])){
return new Sort(Sort.Direction.ASC, splits[0]);
}else{
return new Sort(Sort.Direction.DESC, splits[0]);
}
}else{
return new Sort(splits[0]);
}
}
}, count);
return page;
}
public Integer getCount(Criteria c) throws DaoException {
int count = 0;
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("whereClause", c.getWhereClause());
count = (Integer)this.sqlSession.selectOne(namespace + ".getCount",map);
return count;
}
}
com/base/exception/DaoException.java及ServiceException.java和TransactionException.java的代码:
package com.base.exception;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* <p>
* Description: DAO exception 基类,所有DAO实现类异常均由此类派生
* </p>
*
* @version 1.0
*/
public class DaoException extends Exception {
/**
*
*/
private static final long serialVersionUID = 8014536628694723159L;
private Throwable nestedThrowable = null;
public DaoException() {
super();
}
public DaoException(String msg) {
super(msg);
}
public DaoException(Throwable nestedThrowable) {
this.nestedThrowable = nestedThrowable;
}
public DaoException(String msg, Throwable nestedThrowable) {
super(msg);
this.nestedThrowable = nestedThrowable;
}
@Override
public void printStackTrace() {
super.printStackTrace();
if (nestedThrowable != null) {
nestedThrowable.printStackTrace();
}
}
@Override
public void printStackTrace(PrintStream ps) {
super.printStackTrace(ps);
if (nestedThrowable != null) {
nestedThrowable.printStackTrace(ps);
}
}
@Override
public void printStackTrace(PrintWriter pw) {
super.printStackTrace(pw);
if (nestedThrowable != null) {
nestedThrowable.printStackTrace(pw);
}
}
}
package com.base.exception;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* <p>
* Description: Service exception 基类,所有Service实现类异常均由此类派生
* </p>
*
* @version 1.0
*/
public class ServiceException extends Exception {
/**
*
*/
private static final long serialVersionUID = -7074796630261537939L;
private Throwable nestedThrowable = null;
public ServiceException() {
super();
}
public ServiceException(String msg) {
super(msg);
}
public ServiceException(Throwable nestedThrowable) {
this.nestedThrowable = nestedThrowable;
}
public ServiceException(String msg, Throwable nestedThrowable) {
super(msg);
this.nestedThrowable = nestedThrowable;
}
@Override
public void printStackTrace() {
super.printStackTrace();
if (nestedThrowable != null) {
nestedThrowable.printStackTrace();
}
}
@Override
public void printStackTrace(PrintStream ps) {
super.printStackTrace(ps);
if (nestedThrowable != null) {
nestedThrowable.printStackTrace(ps);
}
}
@Override
public void printStackTrace(PrintWriter pw) {
super.printStackTrace(pw);
if (nestedThrowable != null) {
nestedThrowable.printStackTrace(pw);
}
}
}
package com.base.exception;
import java.io.PrintStream;
import java.io.PrintWriter;
public class TransactionException extends Exception {
private static final long serialVersionUID = 7985658799424168444L;
private Throwable nestedThrowable = null;
public TransactionException() {
super();
}
public TransactionException(String msg) {
super(msg);
}
public TransactionException(Throwable nestedThrowable) {
this.nestedThrowable = nestedThrowable;
}
public TransactionException(String msg, Throwable nestedThrowable) {
super(msg);
this.nestedThrowable = nestedThrowable;
}
public void printStackTrace() {
super.printStackTrace();
if (nestedThrowable != null) {
nestedThrowable.printStackTrace();
}
}
public void printStackTrace(PrintStream ps) {
super.printStackTrace(ps);
if (nestedThrowable != null) {
nestedThrowable.printStackTrace(ps);
}
}
public void printStackTrace(PrintWriter pw) {
super.printStackTrace(pw);
if (nestedThrowable != null) {
nestedThrowable.printStackTrace(pw);
}
}
}
com/base/exception/handler/BasicExceptionHanler.java代码:
package com.base.exception.handler;
import com.base.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BasicExceptionHanler implements HandlerExceptionResolver {
private transient Logger logger = LoggerFactory.getLogger(getClass());
@ExceptionHandler({ServiceException.class})
public String exception(ServiceException e){
return "error";
}
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
return new ModelAndView("error");
}
}
com/base/service/BasicService.java及IBasicService.java代码:
package com.base.service;
import com.base.exception.ServiceException;
import com.base.util.Criteria;
import org.springframework.data.domain.Page;
import java.util.List;
import java.util.Map;
/**
* 持久层基本的接口定义 所有的DAO接口都必须继承该接口
*/
public interface IBasicService<T> {
/**
* 从数据库读取所有对象
*
* @return list 对象的记录集合
* @throws com.base.exception.ServiceException
*/
public List<T> getAll() throws ServiceException;
/**
* 从数据库读取所有对象
*
* @param first 第一条记录
* @param limit 最大记录数
* @return 包含对象的记录集合
* @throws com.base.exception.ServiceException
*/
public List<T> getList(int first, int limit) throws ServiceException;
/**
* 得到对象的总数量
*
* @return 对象的总数量
* @throws com.base.exception.ServiceException
*/
public Integer getCount() throws ServiceException;
/**
* 根据id得到符合条件的该对象,对象不存在时返回空
*
* @param key 对象的ID
* @return 符合条件的该对象
* @throws com.base.exception.ServiceException
*/
public T getObject(Long key) throws ServiceException;
/**
* 将对象信息持久化
*
* @param object
* @throws com.base.exception.ServiceException
*/
public void create(T object) throws ServiceException;
/**
* 修改对象信息
*
* @param object
* @throws com.base.exception.ServiceException
*/
public void update(T object) throws ServiceException;
/**
* 根据条件删除对象的信息
*
* @param id 要删除对象的ID
* @throws com.base.exception.ServiceException
*/
public void delete(int id) throws ServiceException;
/**
* 简单查询,将map中的条件组合进行"="查询
* @param map 查询条件,其中key是字段名,value是条件
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.ServiceException
*/
public T findOne(Map<String, Object> map) throws ServiceException;
/**
* 简单查询,将map中的条件组合进行"="查询
* @param map 查询条件,其中key是字段名,value是条件
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.ServiceException
*/
public List<T> find(Map<String, Object> map) throws ServiceException;
/**
* 根据in条件查询
*
* @param map 查询条件,多个值时请用,分割
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.ServiceException
*/
public List<T> findByIn(Map<String, Object> map) throws ServiceException;
/**
* 得到符合条件的该对象,用Like进行查询
*
* @param map 查询条件,其中key是字段名,value是条件
* @return list 包含符合条件对象的记录集合
* @throws com.base.exception.ServiceException
*/
public List<T> findByLike(Map<String, Object> map) throws ServiceException;
/**
* 根据传入的Criteria条件查询,返回结果集
*
* @param c 传入的Criteria条件查询对象
* @return 返回符合条件的结果集
* @throws com.base.exception.ServiceException
*/
public Page<T> getList(final Criteria c) throws ServiceException;
/**
* 根据传入的Criteria条件查询,返回结果数量
*
* @param c 传入的Criteria条件查询对象
* @return 结果数量
* @throws com.base.exception.ServiceException
*/
public Integer getCount(Criteria c) throws ServiceException;
/**
* 根据对象进行单一查询
* @param object 查询的对象
* @return 符合条件的单一对象
* @throws ServiceException
*/
public T findOne(T object) throws ServiceException;
/**
* 根据对象进行列表查询
* @param object 查询的对象
* @return 符合条件的对象列表
* @throws ServiceException
*/
public List<T> find(T object) throws ServiceException;
}
package com.base.service;
import com.base.dao.IBasicDao;
import com.base.exception.DaoException;
import com.base.exception.ServiceException;
import com.base.util.Criteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
public abstract class BasicService<T> implements IBasicService<T> {
protected transient final Logger logger = LoggerFactory.getLogger(getClass());
public abstract IBasicDao<T> getDao();
@Transactional(propagation= Propagation.NOT_SUPPORTED)
public List<T> getAll() throws ServiceException {
try{
return getDao().getAll();
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<T> getList(int first, int limit) throws ServiceException {
try{
return getDao().getList(first, limit);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public Integer getCount() throws ServiceException {
try{
return getDao().getCount();
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public T getObject(Long key) throws ServiceException {
try{
return getDao().getObject(key);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation = Propagation.REQUIRED)
public void create(T object) throws ServiceException {
try{
getDao().create(object);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation = Propagation.REQUIRED)
public void update(T object) throws ServiceException {
try{
getDao().update(object);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation = Propagation.REQUIRED)
public void delete(int id) throws ServiceException {
try{
getDao().delete(id);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public T findOne(Map<String, Object> map) throws ServiceException {
try{
return getDao().findOne(map);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<T> find(Map<String, Object> map) throws ServiceException {
try{
return getDao().find(map);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<T> findByIn(Map<String, Object> map) throws ServiceException {
try{
return getDao().findByIn(map);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<T> findByLike(Map<String, Object> map) throws ServiceException {
try{
return getDao().findByLike(map);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public Page<T> getList(Criteria c) throws ServiceException {
try{
return getDao().getList(c);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public Integer getCount(Criteria c) throws ServiceException {
try{
return getDao().getCount(c);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public T findOne(T object) throws ServiceException{
try{
return (T)getDao().findOne(object);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public List<T> find(T object) throws ServiceException{
try{
return getDao().find(object);
}catch (DaoException e){
throw new ServiceException("ERROR:",e);
}catch (Exception e){
throw new ServiceException("ERROR:",e);
}
}
}
未完待续