Mybatis的dao层,service层的封装方式
作者:_东极
这篇文章主要介绍了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); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。