Java tutorial
/* * Copyright (c) 2016 yunmle.com(?). * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.yunmel.syncretic.core; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.Table; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.yunmel.syncretic.bean.BaseEntity; import com.yunmel.syncretic.component.SpringUtils; import com.yunmel.syncretic.utils.biz.Globle; import com.yunmel.syncretic.utils.commons.StrUtils; import com.yunmel.syncretic.utils.jse.RandomUtils; import com.yunmel.syncretic.utils.jse.ThreadLocalUtils; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example.Criteria; /** * * @description service * * @author xuyq - chainisit@126.com * @since 1.0 - 2016715 * @param <T> */ public abstract class BaseService<T extends BaseEntity> { @Autowired private BaseMapper<T> baseMapper; /** * ??null,?=?and? * * @param <T extend T> */ public List<T> select(T record) { record.set("delFlag", Globle.DEL_FLAG_NORMAL); return baseMapper.select(record); } public List<T> select(T record, String orderSqlStr) { Example example = new Example(record.getClass(), false); Criteria criteria = example.createCriteria(); criteria.andEqualTo("delFlag", Globle.DEL_FLAG_NORMAL); for (Map.Entry<String, Object> entry : record.entrySet()) { if ("".equals(entry.getValue())) continue; criteria.andEqualTo(entry.getKey(), entry.getValue()); } example.setOrderByClause(orderSqlStr); return baseMapper.selectByExample(example); } /** * ??null,?=?and? * * @param <T extend T> */ public int selectCount(T record) { record.set("delFlag", Globle.DEL_FLAG_NORMAL); return baseMapper.selectCount(record); } /** * ?,?? ??,? ??,key?,?Map * * @param <T extend T> */ public T selectByPrimaryKey(Object key) { return baseMapper.selectByPrimaryKey(key); } /** * ??? ?Oracle?,UUID,MysqlINDENTITY() ?,?,???UUID, * * @param <T extend T> */ public int insert(T record) { if (record.containsKey("createDate")) { record.set("createDate", System.currentTimeMillis()); } if (record.containsKey("updateDate")) { record.set("updateDate", System.currentTimeMillis()); } if (record.containsKey("delFlag")) { record.set("delFlag", Globle.DEL_FLAG_NORMAL); } record.setId(RandomUtils.genRandom32Hex()); return baseMapper.insert(record); } /** * ???,???null,?? ?Oracle?,UUID,MysqlINDENTITY() * ?,?,???UUID, * * @param <T extend T> */ public int insertSelective(T record) { if (StringUtils.isBlank(record.getString("createBy"))) { String user = (String) ThreadLocalUtils.get("user"); record.set("createBy", user); } record.set("createDate", System.currentTimeMillis()); record.set("updateDate", System.currentTimeMillis()); record.set("delFlag", Globle.DEL_FLAG_NORMAL); record.setId(RandomUtils.genRandom32Hex()); return baseMapper.insertSelective(record); } /** * ??null,?=?and? * * @param <T extend T> */ public int delete(T key) { return baseMapper.delete(key); } /** * ,??? ??,? ??,key?,?Map * * @param <T extend T> */ public int deleteByPrimaryKey(Object key) { return baseMapper.deleteByPrimaryKey(key); } /** * ?,??? ? * * @param <T extend T> */ public int updateByPrimaryKey(T record) { // SysUser sysUser = SysUserUtils.getCacheLoginUser(); // if(sysUser != null){ // record.set("updateBy",sysUser.getId()+","+ // SysUserUtils.getCacheLoginUser().getName()); // } record.set("updateDate", new Date()); return baseMapper.updateByPrimaryKey(record); } /** * ? ??null? * * @param <T extend T> */ public int updateByPrimaryKeySelective(T record) { // SysUser user = SysUserUtils.getCacheLoginUser(); String user = (String) ThreadLocalUtils.get("user"); record.set("updateBy", user); record.set("updateDate", System.currentTimeMillis()); return baseMapper.updateByPrimaryKeySelective(record); } /** * ?(?delFlag) * * @param bean * @return ? */ public <M extends BaseEntity> int updateDelFlagToDelStatusById(Class<M> bean, String id) { String mapperName = StringUtils.uncapitalize(bean.getSimpleName()) + "Mapper"; Mapper<M> mapper = SpringUtils.getBean(mapperName); M m = null; try { m = bean.newInstance(); m.setId(id); m.set("delFlag", Globle.DEL_FLAG_DELETE); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return mapper.updateByPrimaryKeySelective(m); } /** * ??id?null? * * @param record * @return ? */ public int save(T record) { int count = 0; if (record.get("id") == null) { count = this.insertSelective(record); } else { count = this.updateByPrimaryKeySelective(record); } return count; } /** * ? * * @param pageNum ? * @param pageSize ? * @param record ? * @return */ public PageInfo<T> selectPage(int pageNum, int pageSize, T record) { if (record.containsKey("delFlag")) { record.set("delFlag", Globle.DEL_FLAG_NORMAL); } PageHelper.startPage(pageNum, pageSize); return new PageInfo<T>(baseMapper.select(record)); } /** * @Description:(???) * @param:@param pageNum * @param:@param pageSize * @param:@param record * @param:@param orderSqlStr (:id desc) * @return:PageInfo<T> */ public PageInfo<T> selectPage(int pageNum, int pageSize, T record, String orderSqlStr) { Example example = new Example(record.getClass(), false); Criteria criteria = example.createCriteria(); if (record.containsKey("delFlag")) { criteria.andEqualTo("delFlag", Globle.DEL_FLAG_NORMAL); } for (Map.Entry<String, Object> entry : record.entrySet()) { if (entry.getValue() == null || "".equals(entry.getValue())) continue; criteria.andEqualTo(entry.getKey(), entry.getValue()); } example.setOrderByClause(orderSqlStr); PageHelper.startPage(pageNum, pageSize); List<T> list = baseMapper.selectByExample(example); return new PageInfo<T>(list); } /** * ????(??) * * @param bean class * @param fields * @param values * @return -1? */ public <M extends BaseEntity> int beforeDelete(Class<M> bean, Map<String, Object> params) { String mapperName = StringUtils.uncapitalize(bean.getSimpleName()) + "Mapper"; Mapper<M> mapper = SpringUtils.getBean(mapperName); M m = null; try { m = bean.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } m.setAll(params); int count = mapper.selectCount(m); return count > 0 ? -1 : count; } /** * ??() * * @param id id * @param Field ??? * @param beans class ??class class * @return -1 */ public int beforeDeleteTreeStructure(Object id, String Field, Class<?>... beans) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", id); map.put("checkField", StrUtils.camelhumpToUnderline(Field)); for (int i = 0; i < beans.length; i++) { Class<?> cl = beans[i]; Table table = cl.getAnnotation(Table.class); if (table == null) { throw new RuntimeException("?table"); } String tableName = table.name(); // String tableName = StringConvert.camelhumpToUnderline(cl.getSimpleName()); map.put("t" + i, tableName); } int count = 0; // baseMapper.beforeDeleteTreeStructure(map); return count > 0 ? -1 : count; } /** * * * @param bean * @param checkField * @param value * @param id * @return */ public <M extends BaseEntity> boolean unique(Class<M> bean, String checkField, Object value, Object id) { return unique(bean, checkField, value, id, null); } public <M extends BaseEntity> boolean unique(Class<M> bean, String checkField, Object value, Object id, Map<String, Object> params) { String mapperName = StringUtils.uncapitalize(bean.getSimpleName()) + "Mapper"; Mapper<M> mapper = SpringUtils.getBean(mapperName); M m = null; try { m = bean.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (id != null) { m.set("id", id); } m.set(checkField, value); if (params != null) { m.setAll(params); } // int count = mapper.selectCount(m); if (id != null) { List<M> lists = mapper.select(m); if (lists.size() >= 2) { return false; } else if ((lists.size() == 1)) { return lists.get(0).get("id").toString().equals(id + ""); } else { return true; } } else { int count = mapper.selectCount(m); return count <= 0 ? true : false; } } /** * ,? * * @param bean * @param checkField * @param value * @param id * @return */ public <M extends BaseEntity> boolean uniqueHaveStatus(Class<M> bean, String checkField, Object value, Object id) { Map<String, Object> params = new HashMap<String, Object>(); params.put("status", Globle.SYSTEM_COMMON_ENABLE); return this.unique(bean, checkField, value, id, params); } }