/* * Copyright 2009 by primedata Corporation. * , * * All rights reserved. * * This software is the confidential and proprietary information of * primedata Corporation ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with primedata. */ package com.primeton.dams.utils.template.impl; import com.primeton.dams.utils.template.CoreSQLParser; import com.primeton.dams.utils.template.DOResultTransformer; import com.primeton.dams.utils.template.HibernateStatementSessionFactoryBean; import com.primeton.dams.utils.template.IDAOService; import com.primeton.dams.utils.template.Page; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.hibernate.SQLQuery; import org.hibernate.internal.QueryImpl; import org.hibernate.transform.ResultTransformer; import org.hibernate.transform.Transformers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.LockModeType; import javax.persistence.NoResultException; import javax.persistence.NonUniqueResultException; import javax.persistence.PersistenceContext; import javax.persistence.Query; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * find开头的方法是HQL方法,query开头的是SQL方法。 * @author user * @version 1.0 2007-1-27 * * @author zhaopx * @version 7.1 * @date 2020-08-25 */ @Slf4j @Repository(value = "daoService") public class DAOServiceImpl implements IDAOService { /** * LOG */ private static Logger logger = LoggerFactory.getLogger(DAOServiceImpl.class); @Autowired JdbcTemplate jdbcTemplate; @PersistenceContext EntityManager entityManager; @Override public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public DAOServiceImpl() { super(); } /** Get HQL from xxxSql.xml file*/ @Override public String getSQL(String name) { return HibernateStatementSessionFactoryBean.getStatementText(name); } /** Get dynamic HQL from xxxSql.xml file, voOrMap is a VO or a map.*/ @Override public String getSQL(String name, Object voOrMap) { return HibernateStatementSessionFactoryBean.getStatementText(name, voOrMap); } /** Get dynamic Params from xxxSql.xml file, voOrMap is a VO or a map.*/ @Override public Object[] getParam(String name, Object voOrMap) { return HibernateStatementSessionFactoryBean.getStatementParam(name, voOrMap); } /** Get dynamic Params from xxxSql.xml file, voOrMap is a VO or a map.*/ @Override public Map getMapParam(String name, Object voOrMap) { return HibernateStatementSessionFactoryBean.getStatementParamMap(name, voOrMap); } //Persist the given transient instance @Override public void add(Object vo) throws DataAccessException { entityManager.persist(vo); } //Update the given persistent instance @Override public void update(Object vo) throws DataAccessException { entityManager.merge(vo); } @Override public void merge(Object entity) throws DataAccessException { entityManager.merge(entity); } //Save or update the given persistent instance, according to its id @Override public void addOrUpdate(Object vo) throws DataAccessException { entityManager.persist(vo); } @Override public void addAll(Collection vos) throws DataAccessException { for (Object vo : vos) { entityManager.persist(vo); } } //Return the persistent instance of the given entity class with the given identifier, //or null if not found @Override public Object get(Class entityClass, Serializable id) { return entityManager.find(entityClass, id); } //Return the persistent instance of the given entity class with the given identifier, //or null if not found @Override public Object get(Class entityClass, Serializable id, LockModeType lockMode) { return entityManager.find(entityClass, id, lockMode); } //Delete the given persistent instance, according to its id @Override public void delete(Class entityClass, Serializable id) throws DataAccessException { entityManager.remove(get(entityClass, id)); } //Delete the given persistent instance @Override public void delete(Object entity) throws DataAccessException { entityManager.remove(entity); } //Delete all given persistent instances @Override public void deleteAll(Collection entities) throws DataAccessException { for (Object entity : entities) { entityManager.remove(entity); } } //Update/delete all objects according to the given query @Override public int execute(String updateSql) { return entityManager.createNativeQuery(updateSql).executeUpdate(); } //Update/delete all objects according to the given query @Override public int execute(String updateSql, Object[] params) { final javax.persistence.Query nativeQuery = entityManager.createNativeQuery(updateSql); for (int i=1; params!=null && i params, Class entityClass) { final String sql = getSQL(id, params); if(StringUtils.isBlank(sql)) { throw new IllegalArgumentException("undefined statement id: " + id); } final Object[] sqlParams = (params == null ? null : getParam(id, params)); final Query nativeQuery = entityManager.createNativeQuery(sql); if (entityClass == null) { nativeQuery.unwrap(org.hibernate.SQLQuery.class) .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); } else { nativeQuery.unwrap(org.hibernate.SQLQuery.class) .setResultTransformer(new DOResultTransformer(entityClass)); } return query(nativeQuery, sqlParams, entityClass, null, null); } /** * 执行 statement id 的查询,返回 ResultTransformer 的转换结果, 并应用分页 * @param id * @param params * @param entityClass * @param firstResult * @param maxResult * @return */ @Override public List queryById(String id, Map params, Class entityClass, final int firstResult, final int maxResult) { final String sql = getSQL(id, params); if(StringUtils.isBlank(sql)) { throw new IllegalArgumentException("undefined statement id: " + id); } final Object[] sqlParams = (params == null ? null : getParam(id, params)); final Query nativeQuery = entityManager.createNativeQuery(sql); if (entityClass == null) { nativeQuery.unwrap(org.hibernate.SQLQuery.class) .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); } else { nativeQuery.unwrap(org.hibernate.SQLQuery.class) .setResultTransformer(new DOResultTransformer(entityClass)); } return query(nativeQuery, sqlParams, entityClass, firstResult, maxResult); } private List query(javax.persistence.Query query, Object[] params, Class entityClass, final Integer firstResult, final Integer maxResult) { for (int i=1; params!=null && i * @return */ public T queryForObject(String sql, Object[] params, @NonNull Class entityClass) { final javax.persistence.Query query1 = entityManager.createNativeQuery(sql); for (int i=1; params!=null && i params) { final String sql = getSQL(id, params); if(StringUtils.isBlank(sql)) { throw new IllegalArgumentException("undefined statement id: " + id); } final Object[] sqlParams = (params == null ? null : getParam(id, params)); SQLQuery query = entityManager.createNativeQuery(sql).unwrap(SQLQuery.class); if (sqlParams != null && sqlParams.length > 0) { for (int i=1; i 0) { for (int i=1; i 0) { list = query.list(); } page.setCurrRecords(list.size()); page.setQueryResult(list); page.setTotalRecs(totalCount); return list; } @Override public List queryPageMap(String sql, Object[] params, Page page) { int totalCount = this.countSQL(sql, params); List list = new ArrayList(); if (totalCount > 0) { final javax.persistence.Query nativeQuery = entityManager.createNativeQuery(sql); SQLQuery query = nativeQuery.unwrap(SQLQuery.class); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); query.setMaxResults(page.getPageSize()); query.setFirstResult(page.getStartIndex()); return this.queryForMap(nativeQuery, params); } page.setCurrRecords(list.size()); page.setQueryResult(list); page.setTotalRecs(totalCount); return list; } /* * (non-Javadoc) * @see com.primedata.primedatamm.core.dao.IDAOService * #queryPageEntity(java.lang.String, java.lang.Object[], * com.primedata.primedatamm.core.common.Page, EntityList) */ @Override public List queryPageEntity(String sql, Object[] params, Page page, @NonNull Class entityClass) { int totalCount = this.countSQL(sql, params); List list = new ArrayList(); if (totalCount > 0) { final javax.persistence.Query nativeQuery = entityManager.createNativeQuery(sql); SQLQuery query = nativeQuery.unwrap(SQLQuery.class); for (int i=1; params!=null && i map = new HashMap(); map.put("sequence", sequence); return queryForLong(getSQL("getSQNextValue", map), getParam("getSQNextValue", map)); } @Override public Long getSQCurrValue(String sequence) { Map map = new HashMap(); map.put("sequence", sequence); return queryForLong(getSQL("getSQCurrValue", map), getParam("getSQCurrValue", map)); } }