Mybatis的dao层,service层的封装方式

 更新时间:2023年07月11日 08:37:30   作者:_东极  
这篇文章主要介绍了Mybatis的dao层,service层的封装方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Mybatis的dao层,service层的封装

配置:

在这里插入图片描述

分别创建四个包

在这里插入图片描述

使用插件自动生成对应的类和bean对象

创建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;
    /**
     * 查询总记录数
     * @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;
}

创建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);
    }
    /**
     * 根据参数统计记录数
     * @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;
    /**
     * 每页记录数
     */
    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 {
    /**
     * 表达式结果真时判断
     * @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("参数异常");
        }
    }   
    /**
     * 参数为空时
     * @param object
     * @param msg
     */
    public static void isNull(Object object,String msg){
        if(object==null){
            throw new ParamException(msg);
        }
    }
    /**
     * 参数不空时
     * @param object
     * @param msg
     */
    public static void notNull(Object object,String msg){
        if(object!=null){
            throw new ParamException(msg);
        }
    }
}

参数异常类

package com.shsxt.base;
/**
 * 参数异常类
 * @author Administrator
 *
 */
public class ParamException extends RuntimeException{
    /**
     * 
     */
    private static final long serialVersionUID = -5962296753554846774L;
    /**
     * 错误状态码
     */
    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;
}

写实现类

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java实现计算器模板及源码

    java实现计算器模板及源码

    这篇文章主要为大家详细介绍了java实现计算器模板及源码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Java 压缩包解压实现代码

    Java 压缩包解压实现代码

    Java标准库(Java SE)提供了对ZIP格式的原生支持,通过java.util.zip包中的类来实现压缩和解压功能,本文将重点介绍如何使用Java来解压ZIP或RAR压缩包,感兴趣的朋友一起看看吧
    2025-05-05
  • Java中Double、Float类型的NaN和Infinity的具体使用

    Java中Double、Float类型的NaN和Infinity的具体使用

    Java在处理浮点数运算时,提供了NaN和Infinity两个常量,本文主要介绍了Java中Double、Float类型的NaN和Infinity的具体使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • java实现2048小游戏

    java实现2048小游戏

    这篇文章主要为大家详细介绍了java实现2048小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-02-02
  • mybatis多个区间处理方式(双foreach循环)

    mybatis多个区间处理方式(双foreach循环)

    这篇文章主要介绍了mybatis多个区间处理方式(双foreach循环),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    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服务下线报错问题

    如何解决Nacos服务下线报错问题

    这篇文章主要介绍了如何解决Nacos服务下线报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Java进阶学习:网络服务器编程

    Java进阶学习:网络服务器编程

    Java进阶学习:网络服务器编程...
    2006-12-12
  • mybatis实现遍历Map的key和value

    mybatis实现遍历Map的key和value

    这篇文章主要介绍了mybatis实现遍历Map的key和value方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论