在spring中使用通用Mapper,抽象出通用的业务类BaseService<T>

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fanfanzk1314/article/details/70258048
 
这里写一下spring中使用Mapper的方案,这也是项目中的最优方案; 
由于spring4增加了对泛型注入的支持,这个特性对于Mybatis的通用Mapper来说,非常的有用,可以直接在service中写Mapper<T>


,通过BaseService<T> 来实现通用的Service; 
Spring 抽象出Service方法– BaseService
public abstract class BaseService<T> {
    // public abstract Mapper<T> getMapper();


    //注入Mapper<T>
    @Autowired
    private Mapper<T> mapper;
    /**
     * 根据id查询数据
     * 
     * @param id
     * @return
     */
    public T queryById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }


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


    /**
     * 根据条件查询一条数据,如果有多条数据会抛出异常
     * 
     * @param record
     * @return
     */
    public T queryOne(T record) {
        return mapper.selectOne(record);
    }


    /**
     * 根据条件查询数据列表
     * 
     * @param record
     * @return
     */
    public List<T> queryListByWhere(T record) {
        return mapper.select(record);
    }


    /**
     * 分页查询
     * 
     * @param page
     * @param rows
     * @param record
     * @return
     */
    public PageInfo<T> queryPageListByWhere(Integer page, Integer rows, T record) {
        // 设置分页条件
        PageHelper.startPage(page, rows);
        List<T> list = this.queryListByWhere(record);
        return new PageInfo<T>(list);
    }


    /**
     * 新增数据,返回成功的条数
     * 
     * @param record
     * @return
     */
    public Integer save(T record) {
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
            return mapper.insert(record);
        }


    /**
     * 新增数据,使用不为null的字段,返回成功的条数
     * 
     * @param record
     * @return
     */
    public Integer saveSelective(T record) {
    record.setCreated(new Date());
           record.setUpdated(record.getCreated());
            return mapper.insertSelective(record);
        }


    /**
     * 修改数据,返回成功的条数
     * 
     * @param record
     * @return
     */
    public Integer update(T record) {
        return mapper.updateByPrimaryKey(record);
    }


    /**
     * 修改数据,使用不为null的字段,返回成功的条数
     * 
     * @param record
     * @return
     */
    public Integer updateSelective(T record) {
    record.setUpdated(new Date());
            return mapper.updateByPrimaryKeySelective(record);
        }


    /**
     * 根据id删除数据
     * 
     * @param id
     * @return
     */
    public Integer deleteById(Long id) {
    record.setUpdated(new Date());
            return mapper.deleteByPrimaryKey(id);
        }


    /**
     * 批量删除
     * @param clazz
     * @param property
     * @param values
     * @return
     */
    public Integer deleteByIds(Class<T> clazz, String property, List<Object> values) {
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, values);
        return mapper.deleteByExample(example);
    }


    /**
     * 根据条件做删除
     * 
     * @param record
     * @return
     */
    public Integer deleteByWhere(T record) {
        return mapper.delete(record);
    }


}


用ItemCartService继承BaseService来实现业务需求:


@Service
public class ItemCartService extends BaseService<ItemCart>{
    //这里可以没有任何业务代码,当然也可以根据业务需求,写出相应的代码


}


接下来看如何在Controller中调用Service:
@Autowired
ItemCartService itemCartService;


@RequestMapping(method=RequestMethod.GET)
public ResponseEntity<List<ItemCart>> queryItemCart(@RequestParam(value="id" ,defaultValue="0")
                 Long parentId) {
    try{
        //构造查询条件
        ItemCart record=new ItemCart();
        record.setParentId(parentId);
        List<ItemCart> itemCarts=itemCartService.queryListByWhere(record);


        if(itemCarts == null || itemCarts.isEmpty()){
            return ResponseEntity.status(HttpStatus.Not_FOUND).body(null);
        }
        return ResponseEntity.ok(itemCarts);
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.staus(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}


猜你喜欢

转载自blog.csdn.net/fanfanzk1314/article/details/70258048