公共Service层、Dao层整理
篇一介绍了Spring与Mybatis的整合,整合成功后的下一步动作就是整理公共操作Mybatis的方法。整合过程中关于jar包版本需要注意,特别是mybatis-spring.jar的版本,1.2之前的版本与之后的版本在SqlSessionDaoSupport.class有所改动,远吗会说明使用
BaseDao:
package com.core.dao; import java.util.List; import com.core.dao.common.BaseResponse; import com.core.dao.common.PageData; /** * Dao层公共包 * @author Liugp * */ public interface BaseDao<T> { /** * 查询单条数据 * @param id * @return */ T selectById(String id); /** * 查询多条数据,不分页 * @param obj * @return */ List<T> selectList(Object obj); /** * 自定义列表查询,不分页 * @param statement * @param obj * @return */ List<T> selectList(String statement,Object obj); /** * 查询多条数据,分页 * @param page * @param obj * @return */ BaseResponse selectListPage(PageData page,Object obj); /** * 自定义列表查询,分页 * @param page * @param statement * @param obj * @return */ BaseResponse selectListPage(PageData page,String statement,Object obj); /** * 新增数据 * @param entity * @return :插入成功,返回新增的 */ int insert(T entity); /** * 批量插入:数据表的主键id是UUID生成的,自增长的会提示id不为空,报错 * @param list * @return */ int insert(List<T> list); /** * 修改数据 * @param entity * @return */ int update(T entity); /** * 批量修改数据 * @param list * @return */ int update(List<T> list); /** * 单条删除 * @param id * @return */ int delete(String id); /** * 批量删除 * @param ids * @return */ int delete(String[] ids); }
BaseDaoImpl:mybatis-spring 版本说明,1.2之前支持自动加载SqlSessionTemplate,1.2之后无法
package com.core.dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.support.SqlSessionDaoSupport; import org.springframework.beans.factory.annotation.Autowired; import com.core.dao.common.BaseResponse; import com.core.dao.common.BasicExecuteEnum; import com.core.dao.common.PageData; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; /** * 公共方法实现类 * @author shb * */ public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T> { @Override public T selectById(String id) { return getSqlSession().selectOne(getStatement(BasicExecuteEnum.SELECT_BYID.id), id); } @Override public List<T> selectList(Object obj) { return getSqlSession().selectList(getStatement(BasicExecuteEnum.SELECT_LIST.id), obj); } @Override public List<T> selectList(String statement, Object obj) { return getSqlSession().selectList(getStatement(statement), obj); } @Override public BaseResponse selectListPage(PageData page, Object obj) { BaseResponse response = new BaseResponse(); PageHelper.startPage(page.getPage(), page.getNumber(), true); List<Object> list = getSqlSession().selectList(getStatement(BasicExecuteEnum.SELECT_LIST.id), obj); response.setObj(list); response.setPageInfo(new PageInfo<Object>(list)); return response; } @Override public BaseResponse selectListPage(PageData page, String statement, Object obj) { BaseResponse response = new BaseResponse(); PageHelper.startPage(page.getPage(), page.getNumber(), true); List<Object> list = getSqlSession().selectList(getStatement(statement), obj); response.setObj(list); response.setPageInfo(new PageInfo<Object>(list)); return response; } @Override public int insert(T entity) { return getSqlSession().insert(getStatement(BasicExecuteEnum.INSERT.id), entity); } @Override public int insert(List<T> list) { return getSqlSession().insert(getStatement(BasicExecuteEnum.INSERT_BATCH.id), list); } @Override public int update(T entity) { return getSqlSession().update(getStatement(BasicExecuteEnum.UPDATE.id), entity); } @Override public int update(List<T> list) { int size = 0; for(T obj:list){ int length = getSqlSession().update(getStatement(BasicExecuteEnum.UPDATE.id),obj); if(length != 0) size++; } return size; } @Override public int delete(String id) { return getSqlSession().delete(getStatement(BasicExecuteEnum.INSERT.id), id); } @Override public int delete(String[] ids) { int size = 0; for(String id:ids){ int length = getSqlSession().delete(getStatement(BasicExecuteEnum.INSERT.id), id); if(length != 0 ) size++; } return size; } /** * 注入SqlSessionTemplate实例(要求Spring中进行SqlSessionTemplate的配置).<br/> * 可以调用getSqlSession()完成数据库操作. */ // public SqlSessionTemplate sqlSessionTemplate; // @Autowired // protected SqlSessionFactory sqlSessionFactory; // public SqlSessionTemplate getSessionTemplate() { // return getSqlSession(); // } @Autowired public void setSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { super.setSqlSessionTemplate(sqlSessionTemplate); } public SqlSession getSqlSession() { return super.getSqlSession(); } // public SqlSessionFactory getSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // return sqlSessionFactory; // } // // @Override // public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // this.sqlSessionFactory = sqlSessionFactory; // } /** * 获取类的全路径,拼接 sqlId的Mapper的访问路径 * 公共方法无法获取指定的类全路径,即无法使用namespace的完全限定名 * @param sqlId * @return */ public String getStatement(String sqlId) { String name = this.getClass().getName(); StringBuffer sb = new StringBuffer().append(name).append(".").append(sqlId); return sb.toString(); } }
package com.core.dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.support.SqlSessionDaoSupport; import org.springframework.beans.factory.annotation.Autowired; import com.core.dao.common.BaseResponse; import com.core.dao.common.BasicExecuteEnum; import com.core.dao.common.PageData; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; /** * 公共方法实现类 * @author shb * */ public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T> { @Override public T selectById(String id) { return getSqlSession().selectOne(getStatement(BasicExecuteEnum.SELECT_BYID.id), id); } @Override public List<T> selectList(Object obj) { return getSqlSession().selectList(getStatement(BasicExecuteEnum.SELECT_LIST.id), obj); } @Override public List<T> selectList(String statement, Object obj) { return getSqlSession().selectList(getStatement(statement), obj); } @Override public BaseResponse selectListPage(PageData page, Object obj) { BaseResponse response = new BaseResponse(); PageHelper.startPage(page.getPage(), page.getNumber(), true); List<Object> list = getSqlSession().selectList(getStatement(BasicExecuteEnum.SELECT_LIST.id), obj); response.setObj(list); response.setPageInfo(new PageInfo<Object>(list)); return response; } @Override public BaseResponse selectListPage(PageData page, String statement, Object obj) { BaseResponse response = new BaseResponse(); PageHelper.startPage(page.getPage(), page.getNumber(), true); List<Object> list = getSqlSession().selectList(getStatement(statement), obj); response.setObj(list); response.setPageInfo(new PageInfo<Object>(list)); return response; } @Override public int insert(T entity) { return getSqlSession().insert(getStatement(BasicExecuteEnum.INSERT.id), entity); } @Override public int insert(List<T> list) { return getSqlSession().insert(getStatement(BasicExecuteEnum.INSERT_BATCH.id), list); } @Override public int update(T entity) { return getSqlSession().update(getStatement(BasicExecuteEnum.UPDATE.id), entity); } @Override public int update(List<T> list) { int size = 0; for(T obj:list){ int length = getSqlSession().update(getStatement(BasicExecuteEnum.UPDATE.id),obj); if(length != 0) size++; } return size; } @Override public int delete(String id) { return getSqlSession().delete(getStatement(BasicExecuteEnum.INSERT.id), id); } @Override public int delete(String[] ids) { int size = 0; for(String id:ids){ int length = getSqlSession().delete(getStatement(BasicExecuteEnum.INSERT.id), id); if(length != 0 ) size++; } return size; } /** * 注入SqlSessionTemplate实例(要求Spring中进行SqlSessionTemplate的配置).<br/> * 可以调用getSqlSession()完成数据库操作. */ // public SqlSessionTemplate sqlSessionTemplate; // @Autowired // protected SqlSessionFactory sqlSessionFactory; // public SqlSessionTemplate getSessionTemplate() { // return getSqlSession(); // } @Autowired public void setSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { super.setSqlSessionTemplate(sqlSessionTemplate); } public SqlSession getSqlSession() { return super.getSqlSession(); } // public SqlSessionFactory getSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // return sqlSessionFactory; // } // // @Override // public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // this.sqlSessionFactory = sqlSessionFactory; // } /** * 获取类的全路径,拼接 sqlId的Mapper的访问路径 * 公共方法无法获取指定的类全路径,即无法使用namespace的完全限定名 * @param sqlId * @return */ public String getStatement(String sqlId) { String name = this.getClass().getName(); StringBuffer sb = new StringBuffer().append(name).append(".").append(sqlId); return sb.toString(); } }
来源:https://www.cnblogs.com/xylgp/p/5923494.html