通用mapper、分页助手的BaseService

依赖:

2.3.4
		3.7.5
		0.9.1


			
				com.github.pagehelper
				pagehelper
				${pagehelper-version}
			
			
				com.github.jsqlparser
				jsqlparser
				${jsqlparser-version}
			
			
			
				com.github.abel533
				mapper
				${mapper-version}
			

mybatis-config.xml:




	
	
		
	
	
	
	
    
    
        
        
        
        
    
    
		    
		    
		    
		    
	  




BasePojo:

package com.taotao.manage.pojo;

import java.util.Date;

public abstract class BasePojo {
    
    private Date created;
    private Date updated;
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public Date getUpdated() {
        return updated;
    }
    public void setUpdated(Date updated) {
        this.updated = updated;
    }
    
    

}

BaseService:

package com.taotao.manage.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.manage.pojo.BasePojo;

public abstract class BaseService {
    /**
     * 由子类实现该方法,返回具体的Mapper的实现类
     */
//    public abstract Mapper getMapper();
    /**
     * 这样写是因为使用Spring4.x的新特性,根据泛型类型注入对象
     */
    @Autowired
    public Mapper mapper;
    

    public Mapper getMapper() {
        return mapper;
    }

    /**
     * 根据id查询数据
     * 
     * @param id
     * @return
     */
    public T queryById(Long id) {
        return this.mapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有数据
     * 
     * @return
     */
    public List queryAll() {
        return this.mapper.select(null);
    }

    /**
     * 根据条件查询数据集合
     * 
     * @param t
     * @return
     */
    public List queryByWhere(T t) {
        return this.mapper.select(t);
    }

    /**
     * 根据条件查询一条数据
     */
    public T queryOne(T t) {
        return this.mapper.selectOne(t);
    }

    /**
     * 分页查询数据
     */
    public PageInfo queryPageListBywhere(T t, Integer page, Integer rows) {
        PageHelper.startPage(page, rows, true);// 设置分页参数
        // 查询数据
        List lists = this.queryByWhere(t);
        return new PageInfo(lists);
    }
    /**
     * 自定义查询条件,分页查询
     * @param example
     * @param page
     * @param rows
     * @return
     */
    public PageInfo queryPageListByExample(Example example, Integer page, Integer rows) {
        PageHelper.startPage(page, rows, true);// 设置分页参数
        // 查询数据
        List lists = this.mapper.selectByExample(example);
        return new PageInfo(lists);
    }

    /**
     * 新增数据
     */
    public Integer save(T t) {
        t.setCreated(new Date());
        t.setUpdated(t.getCreated());
        return this.mapper.insert(t);
    }
    /**
     * 新增数据,使用不为null的字段
     */
    public Integer saveSelective(T t) {
        t.setCreated(new Date());
        t.setUpdated(t.getCreated());
        return this.mapper.insertSelective(t);
    }
    /**
     * 更新数据
     */
    public Integer update(T t) {
        t.setUpdated(new Date());
        return this.mapper.updateByPrimaryKey(t);
    }
    /**
     * 更新数据,使用不为null的字段
     */
    public Integer updateSelective(T t) {
        t.setUpdated(new Date());
        return this.mapper.updateByPrimaryKeySelective(t);
    }
    /**
     * 根据id删除一条记录
     */
    public Integer deleteById(Long id){
        return this.mapper.deleteByPrimaryKey(id);
    }
    /**
     * 根据ids批量删除
     */
    public Integer deleteByIds(List ids,String property,Class clazz){
        Example example=new Example(clazz);
        example.createCriteria().andIn(property, ids);
        return this.mapper.deleteByExample(example);
    }
}
 
  

你可能感兴趣的:(java)