Java tutorial
/* * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.jms.notify.dao.impl; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.jms.notify.dao.BaseDao; import com.jms.notify.entity.BaseEntity; import com.jms.notify.exception.BizException; import com.jms.notify.page.PageBean; import com.jms.notify.page.PageParam; /** * ?. * @company?? www.roncoo.com. */ public abstract class BaseDaoImpl<T extends BaseEntity> extends SqlSessionDaoSupport implements BaseDao<T> { protected static final Log LOG = LogFactory.getLog(BaseDaoImpl.class); public static final String SQL_INSERT = "insert"; public static final String SQL_BATCH_INSERT = "batchInsert"; public static final String SQL_UPDATE_BY_ID = "updateByPrimaryKey"; public static final String SQL_BATCH_UPDATE_BY_IDS = "batchUpdateByIds"; public static final String SQL_BATCH_UPDATE_BY_COLUMN = "batchUpdateByColumn"; public static final String SQL_SELECT_BY_ID = "selectByPrimaryKey"; public static final String SQL_LIST_BY_COLUMN = "listByColumn"; public static final String SQL_COUNT_BY_COLUMN = "getCountByColumn"; public static final String SQL_DELETE_BY_ID = "deleteByPrimaryKey"; public static final String SQL_BATCH_DELETE_BY_IDS = "batchDeleteByIds"; public static final String SQL_BATCH_DELETE_BY_COLUMN = "batchDeleteByColumn"; public static final String SQL_LIST_PAGE = "listPage"; public static final String SQL_LIST_BY = "listBy"; public static final String SQL_LIST_PAGE_COUNT = "listPageCount"; public static final String SQL_COUNT_BY_PAGE_PARAM = "countByPageParam"; // ??? /** * SqlSessionTemplate(?SpringSqlSessionTemplate?). * ?sessionTemplate???. */ @Autowired private SqlSessionTemplate sessionTemplate; public SqlSessionTemplate getSessionTemplate() { return sessionTemplate; } public void setSessionTemplate(SqlSessionTemplate sessionTemplate) { this.sessionTemplate = sessionTemplate; } public SqlSession getSqlSession() { return super.getSqlSession(); } /** * ????. */ public int insert(T entity) { int result = sessionTemplate.insert(getStatement(SQL_INSERT), entity); if (result <= 0) { throw BizException.DB_INSERT_RESULT_0.newInstance("??,insert0.{%s}", getStatement(SQL_INSERT)); } return result; } /** * ???. */ public int insert(List<T> list) { if (list.isEmpty() || list.size() <= 0) { return 0; } int result = sessionTemplate.insert(getStatement(SQL_BATCH_INSERT), list); if (result <= 0) { throw BizException.DB_INSERT_RESULT_0.newInstance("??,batchInsert0.{%s}", getStatement(SQL_BATCH_INSERT)); } return result; } /** * ?id???. */ public int update(T entity) { entity.setEditTime(new Date()); int result = sessionTemplate.update(getStatement(SQL_UPDATE_BY_ID), entity); if (result <= 0) { throw BizException.DB_UPDATE_RESULT_0.newInstance("??,updateByPrimaryKey0.{%s}", getStatement(SQL_UPDATE_BY_ID)); } return result; } /** * ?id??. */ public int update(List<T> list) { if (list.isEmpty() || list.size() <= 0) { return 0; } int result = sessionTemplate.update(getStatement(SQL_BATCH_UPDATE_BY_IDS), list); if (result <= 0) { throw BizException.DB_UPDATE_RESULT_0.newInstance("??,batchUpdateByIds0.{%s}", getStatement(SQL_BATCH_UPDATE_BY_IDS)); } return result; } /** * ?column??. */ public int update(Map<String, Object> paramMap) { if (paramMap == null) { return 0; } int result = sessionTemplate.update(getStatement(SQL_BATCH_UPDATE_BY_COLUMN), paramMap); if (result <= 0) { throw BizException.DB_UPDATE_RESULT_0.newInstance("??,batchUpdateByColumn0.{%s}", getStatement(SQL_BATCH_UPDATE_BY_COLUMN)); } return result; } /** * ?id?. */ public T getById(String id) { return sessionTemplate.selectOne(getStatement(SQL_SELECT_BY_ID), id); } /** * ?column?. */ public T getByColumn(Map<String, Object> paramMap) { if (paramMap == null) { return null; } return sessionTemplate.selectOne(getStatement(SQL_LIST_BY_COLUMN), paramMap); } /** * ?? getBy: selectOne <br/> * * @param paramMap * @return */ public T getBy(Map<String, Object> paramMap) { if (paramMap == null) { return null; } return sessionTemplate.selectOne(getStatement(SQL_LIST_BY), paramMap); } /** * ???. */ public List<T> listBy(Map<String, Object> paramMap) { if (paramMap == null) { return null; } return sessionTemplate.selectList(getStatement(SQL_LIST_BY), paramMap); } /** * ?column?. */ public List<T> listByColumn(Map<String, Object> paramMap) { if (paramMap == null) { return null; } return sessionTemplate.selectList(getStatement(SQL_LIST_BY_COLUMN), paramMap); } /** * ?column. */ public Long getCountByColumn(Map<String, Object> paramMap) { if (paramMap == null) { return null; } return sessionTemplate.selectOne(getStatement(SQL_COUNT_BY_COLUMN), paramMap); } /** * ?id?. */ public int delete(String id) { return (int) sessionTemplate.delete(getStatement(SQL_DELETE_BY_ID), id); } /** * ?id??. */ public int delete(List<T> list) { if (list.isEmpty() || list.size() <= 0) { return 0; } else { return (int) sessionTemplate.delete(getStatement(SQL_BATCH_DELETE_BY_IDS), list); } } /** * ?column??. */ public int delete(Map<String, Object> paramMap) { if (paramMap == null) { return 0; } else { return (int) sessionTemplate.delete(getStatement(SQL_BATCH_DELETE_BY_COLUMN), paramMap); } } /** * ? . */ public PageBean listPage(PageParam pageParam, Map<String, Object> paramMap) { if (paramMap == null) { paramMap = new HashMap<String, Object>(); } // Long totalCount = sessionTemplate.selectOne(getStatement(SQL_LIST_PAGE_COUNT), paramMap); // ? int currentPage = PageBean.checkCurrentPage(totalCount.intValue(), pageParam.getNumPerPage(), pageParam.getPageNum()); pageParam.setPageNum(currentPage); // ?? // ??numPerPage?? int numPerPage = PageBean.checkNumPerPage(pageParam.getNumPerPage()); // ? pageParam.setNumPerPage(numPerPage); // ? // ????SQL? paramMap.put("pageFirst", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage()); paramMap.put("pageSize", pageParam.getNumPerPage()); paramMap.put("startRowNum", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage()); paramMap.put("endRowNum", pageParam.getPageNum() * pageParam.getNumPerPage()); // ?? List<Object> list = sessionTemplate.selectList(getStatement(SQL_LIST_PAGE), paramMap); Object isCount = paramMap.get("isCount"); // ????1:? if (isCount != null && "1".equals(isCount.toString())) { Map<String, Object> countResultMap = sessionTemplate.selectOne(getStatement(SQL_COUNT_BY_PAGE_PARAM), paramMap); return new PageBean(pageParam.getPageNum(), pageParam.getNumPerPage(), totalCount.intValue(), list, countResultMap); } else { // return new PageBean(pageParam.getPageNum(), pageParam.getNumPerPage(), totalCount.intValue(), list); } } /** * ?Mapper??. ?? Along 2016-1-8 * * @?@param sqlId * @?@return * @returnString * @throws */ public String getStatement(String sqlId) { String name = this.getClass().getName(); // ?StringBuilder?StringBuffer,? StringBuilder sb = new StringBuilder(); sb.append(name).append(".").append(sqlId); return sb.toString(); } }