spring整合mybatis(不基于MapperFactoryBean)

网友投稿 675 2022-09-10

spring整合mybatis(不基于MapperFactoryBean)

spring整合mybatis(不基于MapperFactoryBean)

application.xml中配置:

BaseDao.java(继承SqlSessionDaoSupport)

public class BaseDao extends SqlSessionDaoSupport implements IBaseDao{//SqlMapClientDaoSupport // mapper.xml中的namespace private String namespace = ""; // sqlmap.xml定义文件中对应的sqlid public static final String SQLID_INSERT = "insert"; public static final String SQLID_INSERT_BATCH = "insertBatch"; public static final String SQLID_UPDATE = "update"; public static final String SQLID_UPDATE_PARAM = "updateParam"; public static final String SQLID_UPDATE_BATCH = "updateBatch"; public static final String SQLID_DELETE = "delete"; public static final String SQLID_DELETE_PARAM = "deleteParam"; public static final String SQLID_DELETE_BATCH = "deleteBatch"; public static final String SQLID_TRUNCATE = "truncate"; public static final String SQLID_SELECT = "select"; public static final String SQLID_SELECT_PK = "selectPk"; public static final String SQLID_SELECT_PARAM = "selectParam"; public static final String SQLID_SELECT_FK = "selectFk"; public static final String SQLID_COUNT = "count"; public static final String SQLID_COUNT_PARAM = "countParam"; public String getNamespace() { Class clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; String clazzName = clazz.getName(); namespace = clazzName; return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } @Override public int insert(T entity) { int rows = 0; try { rows = getSqlSession().insert(getNamespace() + "." + SQLID_INSERT,entity); } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int update(T entity) { int rows = 0; try { rows = getSqlSession().update(namespace + "." + SQLID_UPDATE, entity); } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int updateParam(Map param) { int rows = 0; try { rows = getSqlSession().update(namespace + "." + SQLID_UPDATE_PARAM, param); } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int delete(PK primaryKey) { int rows = 0; try { rows = getSqlSession().delete(namespace + "." + SQLID_DELETE,primaryKey); } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int deleteParam(Map param) { int rows = 0; try { rows = getSqlSession().delete(namespace + "." + SQLID_DELETE_PARAM,param); } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int truncate() { int rows = 0; try { rows = getSqlSession().delete(namespace + "." + SQLID_TRUNCATE); } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int count() { int result = 0; try { result = getSqlSession().selectOne(namespace + "." + SQLID_COUNT); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public int count(Object param) { int result = 0; try { result = getSqlSession().selectOne(namespace + "." + SQLID_COUNT_PARAM,param); } catch (Exception e) { e.printStackTrace(); } return result; } @Override public T get(PK primaryKey) { try { return getSqlSession().selectOne(getNamespace() + "." + SQLID_SELECT_PK,primaryKey); } catch (Exception e) { e.printStackTrace(); return null; } } @Override public List select() { try { return getSqlSession().selectList(namespace + "." + SQLID_SELECT); } catch (Exception e) { e.printStackTrace(); return null; } } @Override public List selectParam(Map param) { try { return getSqlSession().selectList(namespace + "." + SQLID_SELECT_PARAM,param); } catch (Exception e) { e.printStackTrace(); return null; } } @Override public PagingResult selectPagination(PageEntity pageEntity) { try { int page = pageEntity.getPage() == null || "".equals(pageEntity.getPage()) ? 1 : pageEntity.getPage(); //默认为第一页 int size = pageEntity.getSize() == null || "".equals(pageEntity.getSize()) ? 15 : pageEntity.getSize();; //默认每页15个 RowBounds rowBounds = new RowBounds((page-1)*size, size); Map param = pageEntity.getParams(); if (param != null) { param.put("orderColumn", pageEntity.getOrderColumn()); param.put("orderTurn", pageEntity.getOrderTurn()); }else { param = new HashMap(); param.put("orderColumn", pageEntity.getOrderColumn()); param.put("orderTurn", pageEntity.getOrderTurn()); } List resultList = getSqlSession().selectList(namespace + "." + SQLID_SELECT_PARAM,param,rowBounds); int totalSize = count(pageEntity.getParams()); PagingResult pagingResult = new PagingResult(); pagingResult.setCurrentPage(page); pagingResult.setTotalSize(totalSize); pagingResult.setResultList(resultList); return pagingResult; } catch (Exception e) { e.printStackTrace(); return null; } } @Override public int batchInsert(List list) { try { return getSqlSession().insert(namespace + "." + SQLID_INSERT_BATCH,list); } catch (Exception e) { e.printStackTrace(); return 0; } } @Override public int batchUpdate(List list) { int rows = 0; try { for (T t : list) { rows = rows + getSqlSession().update(namespace + "." + SQLID_UPDATE, t); } } catch (Exception e) { e.printStackTrace(); } return rows; } @Override public int batchDelete(List list) { try { return getSqlSession().delete(namespace + "." + SQLID_DELETE_BATCH,list); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 日志打印 * @param sqlId * @param param */ public void printLog(String sqlId,Object param){ Configuration configuration = getSqlSession().getConfiguration(); //sqlId为配置文件中的sqlid MappedStatement mappedStatement = configuration.getMappedStatement(sqlId); //param为传入到sql语句中的参数 BoundSql boundSql = mappedStatement.getBoundSql(param); //得到sql语句 String sql = boundSql.getSql().trim(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("info-sql: "+sdf.format(new Date())+" "+sql); } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python基础-匿名函数和内置函数(python 匿名函数)
下一篇:Windows 10 - 常用命令&工具
相关文章

 发表评论

暂时没有评论,来抢沙发吧~