Mybatis的dao層,service層的封裝方式
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)文章
Spring?Boot?集成?Quartz并使用Cron?表達(dá)式實(shí)現(xiàn)定時(shí)任務(wù)
本篇文章介紹了如何在?Spring?Boot?中集成?Quartz?進(jìn)行定時(shí)任務(wù)調(diào)度,并通過?Cron?表達(dá)式?控制任務(wù)執(zhí)行時(shí)間,Quartz?提供了更強(qiáng)大的任務(wù)調(diào)度能力,比?@Scheduled?注解更靈活,適用于復(fù)雜的定時(shí)任務(wù)需求2025-04-04Java中List集合對(duì)象去重及按屬性去重的8種方法
這篇文章主要介紹了Java中List集合對(duì)象去重及按屬性去重的8種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一地的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(hào)(正則去除行號(hào))
這篇文章主要介紹了MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(hào)(正則去除行號(hào))的相關(guān)資料,需要的朋友可以參考下2017-10-10使用Java8進(jìn)行分組(多個(gè)字段的組合分組)
本文主要介紹了使用Java8進(jìn)行分組(多個(gè)字段的組合分組),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07RocketMQ生產(chǎn)者一個(gè)應(yīng)用不能發(fā)送多個(gè)NameServer消息解決
這篇文章主要為大家介紹了RocketMQ生產(chǎn)者一個(gè)應(yīng)用不能發(fā)送多個(gè)NameServer消息原因及解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Sentinel流控規(guī)則實(shí)現(xiàn)限流保護(hù)詳解
這篇文章主要介紹了Sentinel流控規(guī)則實(shí)現(xiàn)限流保護(hù),Sentinel是一個(gè)分布式系統(tǒng)的流量控制組件,它可以實(shí)現(xiàn)限流,流控,降級(jí)等功能,提高系統(tǒng)的穩(wěn)定性和可靠性,感興趣想要詳細(xì)了解可以參考下文2023-05-05Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格
這篇文章主要為大家詳細(xì)介紹了Java利用POI實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Java設(shè)計(jì)模式之代理模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java設(shè)計(jì)模式之代理模式,本文詳細(xì)的介紹了什么事代理模式和相關(guān)的類和接口,有興趣的可以了解一下2017-08-08