ssm框架下基于TKmybatis构建单表通用service

这是对TKmybatis的进一步简单封装,实现service层的单表的各类基本操作,以下方法基本可以满足日常的单表操作:

1 service通用接口

import java.util.List;

public interface BaseService {

	int insert(T entity);

	/**
	 * 添加非空值,null不会被保存,使用数据库默认值
	 * 
	 * @param entity
	 * @return
	 */
	int insertSelective(T entity);

	int updateByPrimaryKey(T entity);

	int updateByPrimaryKeySelective(T entity);

	int deleteByPrimaryKey(Object primaryKey);

	int deleteByPrimaryKeys(List primarykeys);

	/**
	 * 根据指定字段值删除,判断条件为等号
	 * 
	 * @param field
	 * @param value
	 * @return
	 */
	int deleteByField(String field, String value);

	int deleteByFields(String[] fields, String[] values);

	/**
	 * 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
	 * 
	 * @param entity
	 * @return
	 */
	T selectOne(T entity);

	T selectByPrimaryKey(Object primaryKey);

	/**
	 * 单表分页查询
	 * 
	 * 
	 * selectPage(0,0) return all
	 * 
* * @param pageNum * @param pageSize * @return */ List selectPage(int pageNum, int pageSize); /** * 根据指定字段值模糊分页查询 * * @param field * @param value * @param pageNum * @param pageSize * @return */ List selectByField(String field, String value, int pageNum, int pageSize); List selectByFields(String[] fields, String[] values, int pageNum, int pageSize); /** * 根据指定字段值分页查询 * * @param field * @param value * @param pageNum * @param pageSize * @return */ List selectByEqField(String field, String value, int pageNum, int pageSize); List selectByEqFields(String[] fields, String[] values, int pageNum, int pageSize); }

2 service接口实现类

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xxx.ssm.common.utils.StringUtil;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

public class AbstractService implements BaseService {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	protected Mapper baseMapper;

	private Class clazz = null;

	protected Class getEntityClass() {
		if (clazz == null) {
			clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		}
		return clazz;
	}

	@Override
	public int insert(T entity) {
		return baseMapper.insert(entity);
	}

	@Override
	public int insertSelective(T entity) {
		return baseMapper.insertSelective(entity);
	}

	@Override
	public int updateByPrimaryKey(T entity) {
		return baseMapper.updateByPrimaryKey(entity);
	}

	@Override
	public int updateByPrimaryKeySelective(T entity) {
		return baseMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int deleteByPrimaryKey(Object primaryKey) {
		return baseMapper.deleteByPrimaryKey(primaryKey);
	}

	@Override
	public int deleteByPrimaryKeys(List primaryKeys) {
		int resulrt = 0;
		for (Object primaryKey : primaryKeys) {
			resulrt += baseMapper.deleteByPrimaryKey(primaryKey);
		}
		return resulrt;
	}

	@Override
	public int deleteByField(String field, String value) {
		return deleteByFields(new String[] { field }, new String[] { value });
	}

	@Override
	public int deleteByFields(String[] fields, String[] values) {
		if (null == fields || null == values || fields.length == 0 || fields.length != values.length) {
			return 0;
		}
		Example example = new Example(getEntityClass());
		Criteria criteria = example.createCriteria();
		for (int i = 0; i < fields.length; i++) {
			criteria.andEqualTo(fields[i], values[i]);
		}
		return baseMapper.deleteByExample(example);
	}

	@Override
	public T selectOne(T entity) {
		return baseMapper.selectOne(entity);
	}

	@Override
	public T selectByPrimaryKey(Object primaryKey) {
		return baseMapper.selectByPrimaryKey(primaryKey);
	}

	@Override
	public List selectPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return baseMapper.selectAll();
	}

	@Override
	public List selectByField(String field, String value, int pageNum, int pageSize) {
		return selectByFields(new String[] { field }, new String[] { value }, pageNum, pageSize);
	}

	@Override
	public List selectByFields(String[] fields, String[] values, int pageNum, int pageSize) {
		if (null == fields || null == values || fields.length == 0 || fields.length != values.length) {
			return null;
		}
		Example example = new Example(getEntityClass());
		for (int i = 0; i < fields.length; i++) {
			String keyword = StringUtil.keywords(values[i]);
			example.or().andLike(fields[i], keyword);
		}
		PageHelper.startPage(pageNum, pageSize);
		return baseMapper.selectByExample(example);
	}

	@Override
	public List selectByEqField(String field, String value, int pageNum, int pageSize) {
		return selectByEqFields(new String[] { field }, new String[] { value }, pageNum, pageSize);
	}

	@Override
	public List selectByEqFields(String[] fields, String[] values, int pageNum, int pageSize) {
		if (null == fields || null == values || fields.length == 0 || fields.length != values.length) {
			return null;
		}
		Example example = new Example(getEntityClass());
		Criteria criteria = example.createCriteria();
		for (int i = 0; i < fields.length; i++) {
			criteria.andEqualTo(fields[i], values[i]);
		}
		PageHelper.startPage(pageNum, pageSize);
		return baseMapper.selectByExample(example);
	}

} 
    


你可能感兴趣的:(ssm框架下基于TKmybatis构建单表通用service)