BaseService

package com.taotao.manager.service;

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

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

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.manager.pojo.BasePojo;

@Service
public abstract class BaseService<T extends BasePojo> {
    /** 由子类实现该方法,返回具体的Mapper的实现类 **/
//    public abstract Mapper<T> getMapper() ;
    /**
     * 使用Spring4.x的新特性,根据泛型类型注入对象
     */
    @Autowired
    private Mapper<T> mapper;
    /**
     * 根据id查询
     * @param id
     * @return
     */
    public T queryById(Long id){
        return this.mapper.selectByPrimaryKey(id);
    }
    
//    查询所有数据
    public List<T> queryAll(){
        return this.mapper.select(null);
    }
    
//  根据条件查询一条数据
  public T queryOne(T t){
      return this.mapper.selectOne(t);
  }
  
//    根据条件查询数据集合   
    public List<T> queryByWhere(T t){
      return this.mapper.selectByExample(t);
    }

//    分页查询
    public PageInfo<T> queryPageList(T t,Integer page,Integer rows){
        PageHelper.startPage(page, rows);
      List<T> list = this.queryByWhere(t);
      return new PageInfo<T>(list);
    }
//    新增数据
    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);
    }
//    批量删除数据
    public Integer deleteByIds(String property,List<Object> ids,Class<T> class1){
        Example example=new Example(class1);
        example.createCriteria().andIn(property, ids);
       return this.mapper.deleteByExample(example);
    }
    
}


BasePojo

package com.taotao.manager.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;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值