最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Mybatis的dao層,service層的封裝

配置:

在這里插入圖片描述

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

在這里插入圖片描述

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

創(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;
    /**
     * 分頁查詢記錄
     * @param baseQuery
     * @return
     */
    public List<T> queryForPage(BaseQuery baseQuery) throws DataAccessException;
    /**
     * 查詢記錄不帶分頁情況
     * @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)計記錄數(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);
    }
    /**
     * 分頁查詢
     * @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,"請至少選擇一項記錄!");
        return  baseMapper.deleteBatch(ids);
    }
}

基本的分頁查詢

package com.shsxt.base;
public class BaseQuery {
    /**
     * 分頁頁碼
     */
    private int pageNum=1;
    /**
     * 每頁記錄數(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 {
    /**
     * 表達式結(jié)果真時判斷
     * @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ù)為空時
     * @param object
     * @param msg
     */
    public static void isNull(Object object,String msg){
        if(object==null){
            throw new ParamException(msg);
        }
    }
    /**
     * 參數(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;
    /**
     * 錯誤狀態(tài)碼
     */
    private int errorCode;
    public ParamException() {
    }   
    /**
     * 錯誤消息
     * @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;
    }
}

寫接口時,只需要繼承即可

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層時,也只需要繼承即可

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;
}

寫實現(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é)

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

相關(guān)文章

  • java實現(xiàn)計算器模板及源碼

    java實現(xiàn)計算器模板及源碼

    這篇文章主要為大家詳細介紹了java實現(xiàn)計算器模板及源碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java 壓縮包解壓實現(xiàn)代碼

    Java 壓縮包解壓實現(xiàn)代碼

    Java標準庫(Java SE)提供了對ZIP格式的原生支持,通過java.util.zip包中的類來實現(xiàn)壓縮和解壓功能,本文將重點介紹如何使用Java來解壓ZIP或RAR壓縮包,感興趣的朋友一起看看吧
    2025-05-05
  • Java中Double、Float類型的NaN和Infinity的具體使用

    Java中Double、Float類型的NaN和Infinity的具體使用

    Java在處理浮點數(shù)運算時,提供了NaN和Infinity兩個常量,本文主要介紹了Java中Double、Float類型的NaN和Infinity的具體使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • java實現(xiàn)2048小游戲

    java實現(xiàn)2048小游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)2048小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • mybatis多個區(qū)間處理方式(雙foreach循環(huán))

    mybatis多個區(qū)間處理方式(雙foreach循環(huán))

    這篇文章主要介紹了mybatis多個區(qū)間處理方式(雙foreach循環(huán)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 解決JTable排序問題的方法詳解

    解決JTable排序問題的方法詳解

    本篇文章是對JTable排序問題的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • POI通用導出Excel(.xls,.xlsx)的方法

    POI通用導出Excel(.xls,.xlsx)的方法

    這篇文章主要介紹了POI通用導出Excel(.xls,.xlsx)的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • 如何解決Nacos服務(wù)下線報錯問題

    如何解決Nacos服務(wù)下線報錯問題

    這篇文章主要介紹了如何解決Nacos服務(wù)下線報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java進階學習:網(wǎng)絡(luò)服務(wù)器編程

    Java進階學習:網(wǎng)絡(luò)服務(wù)器編程

    Java進階學習:網(wǎng)絡(luò)服務(wù)器編程...
    2006-12-12
  • mybatis實現(xiàn)遍歷Map的key和value

    mybatis實現(xiàn)遍歷Map的key和value

    這篇文章主要介紹了mybatis實現(xiàn)遍歷Map的key和value方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論

益阳市| 沙雅县| 固安县| 察隅县| 康定县| 石家庄市| 上思县| 沧州市| 枞阳县| 桐城市| 南阳市| 柳州市| 文登市| 闸北区| 安图县| 东乌| 廊坊市| 马关县| 阿图什市| 金华市| 台北县| 开化县| 龙胜| 道孚县| 如东县| 米林县| 太谷县| 靖远县| 赤壁市| 渭源县| 丰县| 湖南省| 石棉县| 喜德县| 涞源县| 东乡县| 成安县| 沂源县| 资源县| 西峡县| 凤冈县|