Mybatis的dao层,service层的封装

配置:
Mybatis的dao层,service层的封装_第1张图片
分别创建四个包
Mybatis的dao层,service层的封装_第2张图片
使用插件自动生成对应的类和bean对象

创建BaseMapper

package com.shsxt.base;

import org.springframework.dao.DataAccessException;

import java.util.List;
import java.util.Map;

public interface BaseMapper {
    /**
     * 添加记录不返回主键
     * @param entity
     * @return
     * @throws DataAccessException
     */
    public int insert(T entity) throws DataAccessException;
    /**
     * 
     * @param entities
     * @return
     * @throws DataAccessException
     */
    public int insertBatch(List 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 queryForPage(BaseQuery baseQuery) throws DataAccessException;
    /**
     * 查询记录不带分页情况
     * @param map
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List 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 {
    @Autowired
    public BaseMapper  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 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 queryForPage(BaseQuery baseQuery)throws Exception{
        PageHelper.startPage(baseQuery.getPageNum(),baseQuery.getPageSize());
        List list= baseMapper.queryForPage(baseQuery);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;        
    }
    
    
    
    
    /**
     * 
     * @param map
     * @return
     * @throws Exception
     */
    @SuppressWarnings("rawtypes")
    public List 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{

}

写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{
    @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);
    }
}

你可能感兴趣的:(J2EE)