Java tutorial
/** * Copyright (c) 2005-2012 https://github.com/zhangkaitao * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.phone.cn.service; import java.io.Serializable; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; 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.Pageable; import org.springframework.data.domain.Sort; import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import com.github.miemiedev.mybatis.paginator.domain.PageList; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.phone.cn.bean.SearchBean; import com.phone.cn.constant.ErrorCodeConstant; import com.phone.cn.entity.BaseEntity; import com.phone.cn.entity.search.Searchable; import com.phone.cn.exception.SimpleException; import com.phone.cn.mapper.BaseMapper; /** * <p> * service ?? * <p/> * <p> * M ID * <p/> * <p> * User: Zhang Kaitao * <p> * Date: 13-1-12 ?4:43 * <p> * Version: 1.0 */ public abstract class BaseService<M extends BaseEntity<ID>, ID extends Serializable> { protected BaseMapper<M, ID> baseRepository; protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired public void setBaseRepository(BaseMapper<M, ID> baseRepository) { this.baseRepository = baseRepository; } /** * ?? * * @param m * * @return ? */ public M save(M m) { if (m.getId() != null) { baseRepository.updateByPrimaryKeySelective(updateBaseEnity(m)); } else { initSave(m); baseRepository.insertSelective(createBaseEnity(m)); } return m; } /** * ??? * @param m */ protected void initSave(M m) { } /** * ???? * * @param m * * @return ? */ public Boolean isSave(M m) { int count = 0; if (m.getId() != null) { count = baseRepository.updateByPrimaryKeySelective(updateBaseEnity(m)); } else { count = baseRepository.insertSelective(createBaseEnity(m)); } if (count == 1) { return Boolean.TRUE; } else { return Boolean.FALSE; } } public ID saveGetId(M m) { if (m.getId() != null) { baseRepository.updateByPrimaryKeySelective(updateBaseEnity(m)); } else { baseRepository.insertSelective(createBaseEnity(m)); } return m.getId(); } /** * ??? update time * @param m * @return */ public M updateBaseEnity(M m) { return setBaseEnity(m, Boolean.FALSE); } public M createBaseEnity(M m) { return setBaseEnity(m, Boolean.TRUE); } public <T extends BaseEntity<?>> T setBaseEnity(T m, Boolean createFlag) { if (createFlag) { // ? // if (MemberUtils.getContext() == null) { // m.setCreateUser("test"); // } else { // m.setCreateUser(MemberUtils.getContext().getUserName()); // } if (m.getCreateTime() == null) m.setCreateTime(Calendar.getInstance().getTime()); } // ? // if (MemberUtils.getContext() == null) { // m.setUpdateUser("test"); // } else { // m.setUpdateUser(MemberUtils.getContext().getUserName()); // } m.setUpdateTime(Calendar.getInstance().getTime()); return m; } /** * ? * * @param m * * @return */ public M update(M m) { baseRepository.updateByPrimaryKeySelective(updateBaseEnity(m)); return m; } /** * * isUpdate:(????). * * @author zhangguodong * @param m * @return ?? * @since JDK 1.6 */ public Boolean isUpdate(M m) { Integer count = baseRepository.updateByPrimaryKeySelective(updateBaseEnity(m)); if (count != null && count == 1) { return Boolean.TRUE; } return Boolean.FALSE; } /** * ? * * @param id * */ public void delete(ID id) { baseRepository.deleteByPrimaryKey(id); } /** * * isDelete:(????). * * @author zhangguodong * @param id * @return ?? * @since JDK 1.6 */ public Boolean isDelete(ID id) { // id ? if (baseRepository.selectByPrimaryKey(id) == null) return Boolean.FALSE; Integer count = baseRepository.deleteByPrimaryKey(id); if (count != null && count == 1) { return Boolean.TRUE; } return Boolean.FALSE; } /** * ? * * @param ids * */ public void delete(ID[] ids) { baseRepository.delete(ids); } /** * * * @param id * * @return id */ public M findOne(ID id) { return baseRepository.selectByPrimaryKey(id); } /** * * * @param id * * @return id */ public M findOneView(ID id) { return baseRepository.selectByPrimaryKey(id); } /** * ? * * @param id * * @return true?false */ public boolean exists(ID id) { return baseRepository.exists(id); } /** * * * @return */ public long count() { return baseRepository.count(); } /** * ? * * @param cc * @return */ public List<M> findByIds(List<ID> ids) { List<M> list = new ArrayList<M>(); for (ID id : ids) { M m = findOne(id); list.add(m); } return list; // return baseRepository.findByIds(ids); } public Map<String, M> findMapStringByIds(List<ID> ids) { if (ids == null || ids.isEmpty()) { return Maps.newHashMap(); } Map<String, M> map = new HashMap<String, M>(); List<M> entitys = findByIds(ids); for (M e : entitys) { map.put(String.valueOf(e.getId()), e); } return map; } public Map<ID, M> findMapByIds(List<ID> ids) { if (ids == null || ids.isEmpty()) { return Maps.newHashMap(); } Map<ID, M> map = new HashMap<ID, M>(); List<M> entitys = findByIds(ids); for (M e : entitys) { map.put(e.getId(), e); } return map; } /** * ? * * @param cc * @return */ public List<M> findBySelective(M cc) { return baseRepository.findBySelective(cc); } /** * * * @return */ //@Cacheable(cacheName="day_cache_key") public List<M> findAll() { return baseRepository.query(null); } /** * ? * * @param sort * @return */ public List<M> findAll(Sort sort) { return baseRepository.findAll(sort); } /** * ?? * * @param pageable * ??? * @return */ public Page<M> findAll(Pageable pageable) { return baseRepository.findAll(pageable); } /** * ?? * * @param searchable * ? * @return */ public Page<M> findAll(Searchable searchable) { return baseRepository.findAll(searchable); } /** * ???? * * @param searchable * ? * @return */ public List<M> findAllWithNoPageNoSort(Searchable searchable) { return Lists.newArrayList(baseRepository.findAll(searchable).getContent()); } /** * ??(?) * * @param searchable * ? * @return */ public List<M> findAllWithSort(Searchable searchable) { return Lists.newArrayList(baseRepository.findAll(searchable).getContent()); } /** * ??? * * @param searchable * ? * @return */ public Long count(Searchable searchable) { return baseRepository.count(searchable); } /** * ??? queryAll:(????). * * @author zhangguodong * @param bean * @return * @since JDK 1.6 */ public List<M> queryAll(SearchBean bean) { return baseRepository.query(bean); } /** * ??? queryAllWithPage:(????). * * @author zhangguodong * @param bean * @param pageBounds * @return * @since JDK 1.6 */ public PageList<M> queryAllWithPage(SearchBean bean, PageBounds pageBounds) { return baseRepository.query(bean, pageBounds); } public PageList<M> queryPage(SearchBean bean) { return baseRepository.query(bean, bean.toPageBounds()); } public M selectOneByExample(SearchBean t) { List<M> list = queryAll(t); if (list != null && list.size() > 0) { return list.get(0); } return null; } /** *?? * @param str * @return */ protected String doEditor(String str) { if (StringUtils.isNotEmpty(str)) { return StringEscapeUtils.unescapeHtml3(str).replace("\n", "").replace("\"", "\\\"").replace("\t", "") .replace("\r", ""); } return str; } /** *?? * @param str * @return */ protected String doEditorForIndex(String str) { if (StringUtils.isNotEmpty(str)) { return StringEscapeUtils.unescapeHtml3(str).replace("\n", "") // .replace("\"", "") .replace("\t", "").replace("\r", ""); } return str; } // public String showEditor(String str) { // if (StringUtils.isNotEmpty(str)) { // return StringEscapeUtils.unescapeHtml3(str).replace("\n", "").replace("\t", "").replace("\r", ""); // } // return str; // } protected void notNull(Object check, String string) throws SimpleException { if (check == null) { SimpleException exception = new SimpleException(ErrorCodeConstant.NULL_MSG, ErrorCodeConstant.NULL_CODE); logger.error(string, exception); throw exception; } } }