Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.vicky.common.utils.service; import com.vicky.common.utils.page.Condition; import com.vicky.common.utils.page.Order; import com.vicky.common.utils.page.Page; import com.vicky.common.utils.statusmsg.StatusMsgException; import com.vicky.common.utils.update.UpdateProperty; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.ibatis.session.RowBounds; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.entity.Example; /** * MybatisBaseService * * @author Vicky * @param <T> * @param <PrimaryKey> */ public abstract class MybatisBaseService<T, PrimaryKey> implements BaseService<T, PrimaryKey> { /** * Mapper,?bean * <p> * @return Mapper?,? */ protected abstract Mapper<T> getMapper(); /** * ??? * <p> * @return ?Object.class */ private Class getTClass() { try { Method method = this.getClass().getDeclaredMethod("getMapper"); Type returnType = method.getGenericReturnType(); if (returnType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); return (Class) typeArguments[0]; } } catch (NoSuchMethodException | SecurityException e) { } return Object.class; } /** * ?ID * <p> * @param id ID * @return * @see tk.mybatis.mapper.common.Mapper#selectByPrimaryKey */ @Override public T selectByPrimaryKey(PrimaryKey id) { return this.getMapper().selectByPrimaryKey(id); } /** * ?,sql?where? * <p> * @param t * @return * @see tk.mybatis.mapper.common.Mapper#selectOne */ @Override public T selectOne(T t) { return this.getMapper().selectOne(t); } /** * ?,sql?where? * <p> * @param t * @return * @see tk.mybatis.mapper.common.Mapper#selectCount */ public int selectCount(T t) { return this.getMapper().selectCount(t); } /** * ???,sql?where? * <p> * @param t * @param rowBounds ? * @return ?,List * @see tk.mybatis.mapper.common.Mapper#selectByRowBounds * @see org.apache.ibatis.session.RowBounds */ public List<T> selectByRowBounds(T t, RowBounds rowBounds) { return this.getMapper().selectByRowBounds(t, rowBounds); } /** * ????,sql?where? * <p> * @param t * @param rowBounds ? * @return ?,Map,?,total,dataList * @see org.apache.ibatis.session.RowBounds */ public Map<String, Object> getPageData(T t, RowBounds rowBounds) { Map<String, Object> map = new HashMap<>(); map.put(Page.DATA_KEY, this.selectByRowBounds(t, rowBounds)); map.put(Page.TOTAL_KEY, this.selectCount(t)); return map; } /** * ?example * <p> * @param example * @return * @see tk.mybatis.mapper.common.Mapper#selectCountByExample * @see tk.mybatis.mapper.entity.Example */ public int selectCountByExample(Example example) { return this.getMapper().selectCountByExample(example); } /** * ?example?? * <p> * @param example * @param rowBounds ? * @return ?,List * @see tk.mybatis.mapper.common.Mapper#selectByExampleAndRowBounds * @see tk.mybatis.mapper.entity.Example * @see org.apache.ibatis.session.RowBounds */ public List<T> selectByExampleAndRowBounds(Example example, RowBounds rowBounds) { return this.getMapper().selectByExampleAndRowBounds(example, rowBounds); } /** * ?example??? * <p> * @param example * @param rowBounds ? * @return ?,Map,?,total,dataList * @see org.apache.ibatis.session.RowBounds * @see tk.mybatis.mapper.entity.Example */ public Map<String, Object> getPageData(Example example, RowBounds rowBounds) { Map<String, Object> map = new HashMap<>(); map.put(Page.DATA_KEY, this.selectByExampleAndRowBounds(example, rowBounds)); map.put(Page.TOTAL_KEY, this.selectCountByExample(example)); return map; } /** * ? * <p> * @param t * @see tk.mybatis.mapper.common.Mapper#insertSelective */ @Override public void save(T t) { this.getMapper().insertSelective(t); } /** * * <p> * @param t * @see tk.mybatis.mapper.common.Mapper#updateByPrimaryKey */ public void update(T t) { this.getMapper().updateByPrimaryKey(t); } /** * * <p> * @param t * @see tk.mybatis.mapper.common.Mapper#updateByPrimaryKey */ public void updateSelective(T t) { this.getMapper().updateByPrimaryKeySelective(t); } /** * ,?HttpServletRequest? * <p> * @param id * @param request HttpServletRequest * @throws java.text.ParseException * @throws java.lang.NoSuchMethodException * @throws java.lang.IllegalAccessException * @throws java.lang.reflect.InvocationTargetException * @see tk.mybatis.mapper.common.Mapper#updateByPrimaryKey */ @Override public void update(PrimaryKey id, HttpServletRequest request) throws Exception { List<UpdateProperty> updatePropertys = UpdateProperty.getUpdatePropertyListFromHttpRequest(request); this.update(id, updatePropertys); } /** * ,?UpdateProperty??? * <p> * @param id * @param updatePropertys List? * @throws java.lang.NoSuchMethodException * @throws java.lang.IllegalAccessException * @throws java.lang.reflect.InvocationTargetException * @see tk.mybatis.mapper.common.Mapper#updateByPrimaryKey */ public void update(PrimaryKey id, List<UpdateProperty> updatePropertys) throws Exception { T t = this.selectByPrimaryKey(id); boolean haveUpdate = false; for (UpdateProperty updateProperty : updatePropertys) { String property = updateProperty.getProperty(); String methodString = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); try { Method method = this.getTClass().getMethod(methodString, updateProperty.getValue().getClass()); method.invoke(t, updateProperty.getValue()); haveUpdate = true; } catch (Exception exception) { throw new StatusMsgException("?,??"); } } if (haveUpdate) { this.updateSelective(t); } else { throw new StatusMsgException(",??"); } } /** * * <p> * @param t * @see tk.mybatis.mapper.common.Mapper#delete */ public void delete(T t) { this.getMapper().delete(t); } /** * * <p> * @param id ID * @see tk.mybatis.mapper.common.Mapper#deleteByPrimaryKey */ @Override public void deleteById(PrimaryKey id) { this.selectByPrimaryKey(id); this.getMapper().deleteByPrimaryKey(id); } /** * * <p> * @param example Example * @return * @see tk.mybatis.mapper.common.Mapper#deleteByExample */ public int deleteByExample(Example example) { return this.getMapper().deleteByExample(example); } /** * ?HttpServletRequest? * <p> * @param request HttpServletRequest * @return ?,Map,?,total,dataList * @throws java.text.ParseException * @see com.vicky.common.utils.page.Condition * @see com.vicky.common.utils.page.Order * @see com.vicky.common.utils.page.Page * @see com.vicky.pageutils.Condition#getConditionListFromHttpRequest */ @Override public Map<String, Object> getPageData(HttpServletRequest request) throws Exception { Map<String, Object> map = new HashMap<>(); map.put(Page.DATA_KEY, this.list(request)); map.put(Page.TOTAL_KEY, this.listCount(request)); return map; } /** * ?HttpServletRequest? * <p> * @param request HttpServletRequest * @return * @throws java.text.ParseException * @see com.vicky.common.utils.page.Condition * @see com.vicky.common.utils.page.Order * @see com.vicky.common.utils.page.Page * @see com.vicky.pageutils.Condition#getConditionListFromHttpRequest */ public int listCount(HttpServletRequest request) throws Exception { List<Condition> conditions = Condition.getConditionListFromHttpRequest(request); return this.listCount(conditions); } /** * ?HttpServletRequest?? * <p> * @param request HttpServletRequest * @return List? * @throws java.text.ParseException * @see com.vicky.common.utils.page.Condition * @see com.vicky.common.utils.page.Order * @see com.vicky.common.utils.page.Page * @see com.vicky.pageutils.Condition#getConditionListFromHttpRequest * @see com.vicky.pageutils.Page#getPageFromHttpRequest * @see com.vicky.pageutils.Order#getOrderListFromHttpRequest */ public List<T> list(HttpServletRequest request) throws Exception { Page page = Page.getPageFromHttpRequest(request); List<Condition> conditions = Condition.getConditionListFromHttpRequest(request); List<Order> orders = Order.getOrderListFromHttpRequest(request); return this.list(page, conditions, orders); } /** * ?? * <p> * @param conditions ?? * @return * @throws java.lang.Exception * @see com.vicky.common.utils.page.Condition * @see com.vicky.pageutils.Condition#getConditionListFromHttpRequest */ public int listCount(List<Condition> conditions) throws Exception { //? Example example = new Example(this.getTClass()); Example.Criteria criteria = example.createCriteria(); for (Condition condition : conditions) { switch (condition.getCondition()) { case Condition.EQUAL: criteria.andEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.NOTEQUAL: criteria.andNotEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.GREATER_THAN: criteria.andGreaterThan(condition.getProperty(), condition.getValue()); break; case Condition.GREATER_THAN_OR_EQUAL: criteria.andGreaterThanOrEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.LESS_THAN: criteria.andLessThan(condition.getProperty(), condition.getValue()); break; case Condition.LESS_THAN_OR_EQUAL: criteria.andLessThanOrEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.LIKEC: criteria.andLike(condition.getProperty(), "%" + (String) condition.getValue() + "%"); break; case Condition.LIKER: criteria.andLike(condition.getProperty(), (String) condition.getValue()); break; case Condition.NOTLIKEC: criteria.andNotLike(condition.getProperty(), "%" + (String) condition.getValue() + "%"); break; case Condition.NOTLIKER: criteria.andNotLike(condition.getProperty(), (String) condition.getValue()); break; case Condition.IN: criteria.andIn(condition.getProperty(), condition.getInSet()); break; case Condition.NOTIN: criteria.andNotIn(condition.getProperty(), condition.getNotInSet()); break; case Condition.NULL: criteria.andIsNull(condition.getProperty()); break; case Condition.NOTNULL: criteria.andIsNotNull(condition.getProperty()); break; default: throw new StatusMsgException("???" + condition.getCondition()); } } return this.getMapper().selectCountByExample(example); } /** * ?? * <p> * @param page ? * @param conditions ? * @param orders ?? * @return List? * @throws java.lang.Exception */ public List<T> list(Page page, List<Condition> conditions, List<Order> orders) throws Exception { // RowBounds rowBounds = new RowBounds(page.getPageOffset(), page.getPageSize()); //? Example example = new Example(this.getTClass()); Example.Criteria criteria = example.createCriteria(); for (Condition condition : conditions) { switch (condition.getCondition()) { case Condition.EQUAL: criteria.andEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.NOTEQUAL: criteria.andNotEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.GREATER_THAN: criteria.andGreaterThan(condition.getProperty(), condition.getValue()); break; case Condition.GREATER_THAN_OR_EQUAL: criteria.andGreaterThanOrEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.LESS_THAN: criteria.andLessThan(condition.getProperty(), condition.getValue()); break; case Condition.LESS_THAN_OR_EQUAL: criteria.andLessThanOrEqualTo(condition.getProperty(), condition.getValue()); break; case Condition.LIKEC: criteria.andLike(condition.getProperty(), "%" + (String) condition.getValue() + "%"); break; case Condition.LIKER: criteria.andLike(condition.getProperty(), (String) condition.getValue()); break; case Condition.NOTLIKEC: criteria.andNotLike(condition.getProperty(), "%" + (String) condition.getValue() + "%"); break; case Condition.NOTLIKER: criteria.andNotLike(condition.getProperty(), (String) condition.getValue()); break; case Condition.IN: criteria.andIn(condition.getProperty(), condition.getInSet()); break; case Condition.NOTIN: criteria.andNotIn(condition.getProperty(), condition.getNotInSet()); break; case Condition.NULL: criteria.andIsNull(condition.getProperty()); break; case Condition.NOTNULL: criteria.andIsNotNull(condition.getProperty()); break; default: throw new StatusMsgException("???" + condition.getCondition()); } } for (Order order : orders) { if (order.getType().equals(Order.ASC)) { example.orderBy(order.getProperty()).asc(); } else { example.orderBy(order.getProperty()).desc(); } } return this.getMapper().selectByExampleAndRowBounds(example, rowBounds); } }