抽取通用開發BaseService

我們在平時的開發中,很多模塊的功能都類似,所以通用類的抽取可以提高代碼的重用性,加快開發效率,且降低出錯效率。對一個項目中類似的多個模塊進行抽取,提取出他們相同功能的模塊利用反射機制來實現不同模塊的調用,所有通用類在開發過程中很重要。以下是對業務層的CRUD進行抽取封裝爲通用類

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<T extends BasePojo> {

    @Autowired
    private Mapper<T> mapper;

    // public abstract Mapper<T> getMapper();

    /**
     * 根據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 = mapper.select(record);
        // 返回分頁信息
        return new PageInfo<T>(list);
    }

    /**
     * 添加信息
     * 
     * @param t
     * @return 返回成功條數
     */
    public Integer save(T t) {
        // 設置創建時間和更新時間
        t.setCreated(new Date());
        t.setUpdated(t.getCreated());
        return mapper.insert(t);
    }

    /**
     * 添加信息(使用不爲空字段)
     * 
     * @param t
     * @return 返回成功條數
     */
    public Integer saveSelective(T t) {
        // 設置創建時間和更新時間
        t.setCreated(new Date());
        t.setUpdated(t.getCreated());
        return mapper.insertSelective(t);
    }

    /**
     * 修改信息
     * 
     * @param t
     * @return 返回成功條數
     */
    public Integer update(T t) {
        // 設置更新時間
        t.setUpdated(new Date());
        return mapper.updateByPrimaryKey(t);
    }

    /**
     * 修改信息(使用不爲空字段)
     * 
     * @param t
     * @return 返回成功條數
     */
    public Integer updateSelective(T t) {
        // 設置更新試講
        t.setUpdated(new Date());
        // 設置創建時間字段爲空,強制永遠不被跟新
        t.setCreated(null);
        return mapper.updateByPrimaryKeySelective(t);
    }

    /**
     * 根據主鍵id刪除
     * 
     * @param id
     * @return 返回成功條數
     */
    public Integer deleteById(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量刪除
     * 
     * @param values
     * @param property
     * @param clazz
     * @return
     */
    public Integer deleteByIds(List<Object> values, String property, Class<T> clazz) {
        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);
    }

}

以上代碼是在ssm環境框架基礎上加入了通用mapper+分頁工具編寫的通用service層的對一個用戶實體CRUD操作,本操作中用到了spring4的新特性——泛型注入
該類需要業務層繼承後傳入泛型可以直接使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章