欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis的dao層,service層的封裝方式

 更新時(shí)間:2023年07月11日 08:37:30   作者:_東極  
這篇文章主要介紹了Mybatis的dao層,service層的封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis的dao層,service層的封裝

配置:

在這里插入圖片描述

分別創(chuàng)建四個(gè)包

在這里插入圖片描述

使用插件自動(dòng)生成對(duì)應(yīng)的類和bean對(duì)象

創(chuàng)建BaseMapper

package com.shsxt.base;
import org.springframework.dao.DataAccessException;
import java.util.List;
import java.util.Map;
public interface BaseMapper<T> {
    /**
     * 添加記錄不返回主鍵
     * @param entity
     * @return
     * @throws DataAccessException
     */
    public int insert(T entity) throws DataAccessException;
    /**
     * 
     * @param entities
     * @return
     * @throws DataAccessException
     */
    public int insertBatch(List<T> entities) throws DataAccessException;
    /**
     * 查詢總記錄數(shù)
     * @param map
     * @return
     */
    @SuppressWarnings("rawtypes")
    public int queryCountByParams(Map map) throws DataAccessException;
    /**
     * 查詢記錄 通過id
     * @param id
     * @return
     */
    public T queryById(Integer id) throws DataAccessException;
    /**
     * 分頁(yè)查詢記錄
     * @param baseQuery
     * @return
     */
    public List<T> queryForPage(BaseQuery baseQuery) throws DataAccessException;
    /**
     * 查詢記錄不帶分頁(yè)情況
     * @param map
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List<T> queryByParams(Map map) throws DataAccessException;
    /**
     * 更新記錄
     * @param entity
     * @return
     */
    public int update(T entity) throws DataAccessException;
    /**
     * 批量更新
     * @param map
     * @return
     * @throws DataAccessException
     */
    public int updateBatch(Map map) throws DataAccessException;
    /**
     * 刪除記錄
     * @param id
     * @return
     */
    public int delete(Integer id) throws DataAccessException;
    /**
     * 批量刪除
     * @param ids
     * @return
     */
    public int deleteBatch(int[] ids) throws DataAccessException;
}

創(chuàng)建BaseService

package com.shsxt.base;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
public abstract class BaseService<T> {
    @Autowired
    public BaseMapper <T> baseMapper;
    /**
     * 添加記錄
     * @param entity
     * @return
     * @throws Exception
     */
    public int insert(T entity) throws Exception{
        int result= baseMapper.insert(entity);
        return result;
    }
    /**
     * 批量添加記錄
     * @param entities
     * @return
     * @throws Exception
     */
    public int insertBatch(List<T> entities) throws Exception{
        return baseMapper.insertBatch(entities);
    }
    /**
     * 根據(jù)參數(shù)統(tǒng)計(jì)記錄數(shù)
     * @param map
     * @return
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    public int queryCountByParams(Map map)throws Exception{
        return baseMapper.queryCountByParams(map);
    }
    /**
     * 查詢記錄通過id
     * @param id
     * @return
     * @throws Exception
     */
    public T queryById(Integer id)throws Exception{
        AssertUtil.isNull(id, "記錄id非空!");
        return baseMapper.queryById(id);
    }
    /**
     * 分頁(yè)查詢
     * @param baseQuery
     * @return
     * @throws Exception
     */
    public PageInfo<T> queryForPage(BaseQuery baseQuery)throws Exception{
        PageHelper.startPage(baseQuery.getPageNum(),baseQuery.getPageSize());
        List<T> list= baseMapper.queryForPage(baseQuery);
        PageInfo<T> pageInfo=new PageInfo<T>(list);
        return pageInfo;        
    }
    /**
     * 
     * @param map
     * @return
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    public List<T> queryByParams(Map map)throws Exception{  
        return baseMapper.queryByParams(map);  
    }
    /**
     * 查詢記錄
     * @param entity
     * @return
     * @throws Exception
     */
    public int update(T entity)throws Exception{
        return baseMapper.update(entity);
    }
    /**
     * 批量更新
     * @param map
     * @return
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    public int updateBatch(Map map) throws Exception{
        return baseMapper.updateBatch(map);
    }
    /**
     * 刪除記錄
     * @param id
     * @return
     * @throws Exception
     */
    public int delete(Integer id) throws Exception{
        // 判斷 空
        AssertUtil.isNull(id, "記錄id非空!");
        AssertUtil.isNull(queryById(id), "待刪除的記錄不存在!");
        return  baseMapper.delete(id);
    }
    /**
     * 批量刪除
     * @param ids
     * @return
     */
    public int deleteBatch(int[] ids) throws Exception{
        AssertUtil.isNull(ids.length==0,"請(qǐng)至少選擇一項(xiàng)記錄!");
        return  baseMapper.deleteBatch(ids);
    }
}

基本的分頁(yè)查詢

package com.shsxt.base;
public class BaseQuery {
    /**
     * 分頁(yè)頁(yè)碼
     */
    private int pageNum=1;
    /**
     * 每頁(yè)記錄數(shù)
     */
    private int pageSize=10;
    public int getPageNum() {
        return pageNum;
    }
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}

斷言類

package com.shsxt.base;
public class AssertUtil {
    /**
     * 表達(dá)式結(jié)果真時(shí)判斷
     * @param msg
     */
    public static void isTrue(Boolean expression,String msg){
        if(expression){
            throw new ParamException(msg);
        }   
    }
    public static void isTure(Boolean expression){
        if(expression){
            throw new ParamException("參數(shù)異常");
        }
    }   
    /**
     * 參數(shù)為空時(shí)
     * @param object
     * @param msg
     */
    public static void isNull(Object object,String msg){
        if(object==null){
            throw new ParamException(msg);
        }
    }
    /**
     * 參數(shù)不空時(shí)
     * @param object
     * @param msg
     */
    public static void notNull(Object object,String msg){
        if(object!=null){
            throw new ParamException(msg);
        }
    }
}

參數(shù)異常類

package com.shsxt.base;
/**
 * 參數(shù)異常類
 * @author Administrator
 *
 */
public class ParamException extends RuntimeException{
    /**
     * 
     */
    private static final long serialVersionUID = -5962296753554846774L;
    /**
     * 錯(cuò)誤狀態(tài)碼
     */
    private int errorCode;
    public ParamException() {
    }   
    /**
     * 錯(cuò)誤消息
     * @param msg
     */
    public ParamException(String msg) {
        super(msg);
    }
    public ParamException(int errorCode,String msg){
        super(msg);
        this.errorCode=errorCode;
    }
    public int getErrorCode() {
        return errorCode;		
    }
    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }
}

寫接口時(shí),只需要繼承即可

package com.shsxt.dao;
import com.shsxt.base.BaseMapper;
import com.shsxt.po.Account;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountMapper extends BaseMapper<Account>{
}

寫service層時(shí),也只需要繼承即可

package com.shsxt.service;
import com.shsxt.base.BaseService;
import com.shsxt.dao.AccountMapper;
import com.shsxt.po.Account;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class AccountService extends BaseService<Account>{
    @Resource
    private AccountMapper accountMapper;
}

寫實(shí)現(xiàn)類

package com.shsxt.service;
import com.shsxt.po.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"} )
public class AccountServiceTest {
    @Resource
    private AccountService accountService;
    @Test
    public void test1() throws Exception {
        Account account =accountService.queryById(8);
        System.out.println(account);
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論