Mybatis的dao層,service層的封裝方式
更新時間:2023年07月11日 08:37:30 作者:_東極
這篇文章主要介紹了Mybatis的dao層,service層的封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Mybatis的dao層,service層的封裝
配置:
分別創(chuà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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring?Boot?集成?Quartz并使用Cron?表達式實現(xiàn)定時任務
本篇文章介紹了如何在?Spring?Boot?中集成?Quartz?進行定時任務調(diào)度,并通過?Cron?表達式?控制任務執(zhí)行時間,Quartz?提供了更強大的任務調(diào)度能力,比?@Scheduled?注解更靈活,適用于復雜的定時任務需求2025-04-04MyEclipse去除網(wǎng)上復制下來的代碼帶有的行號(正則去除行號)
這篇文章主要介紹了MyEclipse去除網(wǎng)上復制下來的代碼帶有的行號(正則去除行號)的相關資料,需要的朋友可以參考下2017-10-10RocketMQ生產(chǎn)者一個應用不能發(fā)送多個NameServer消息解決
這篇文章主要為大家介紹了RocketMQ生產(chǎn)者一個應用不能發(fā)送多個NameServer消息原因及解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Sentinel流控規(guī)則實現(xiàn)限流保護詳解
這篇文章主要介紹了Sentinel流控規(guī)則實現(xiàn)限流保護,Sentinel是一個分布式系統(tǒng)的流量控制組件,它可以實現(xiàn)限流,流控,降級等功能,提高系統(tǒng)的穩(wěn)定性和可靠性,感興趣想要詳細了解可以參考下文2023-05-05Java設計模式之代理模式_動力節(jié)點Java學院整理
這篇文章主要介紹了Java設計模式之代理模式,本文詳細的介紹了什么事代理模式和相關的類和接口,有興趣的可以了解一下2017-08-08