|
@ -1,22 +1,7 @@
|
|
|
package com.yihu.ehr.framework.common.dao;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.ehr.framework.constrant.Constants;
|
|
|
import com.yihu.ehr.framework.constrant.ErrorCode;
|
|
|
import com.yihu.ehr.framework.exception.ApiException;
|
|
|
import com.yihu.ehr.framework.model.DataGridResult;
|
|
|
import com.yihu.ehr.framework.util.log.LogService;
|
|
|
import com.yihu.ehr.framework.util.operator.CollectionUtil;
|
|
|
import com.yihu.ehr.framework.util.operator.NumberUtil;
|
|
|
import com.yihu.ehr.framework.util.sql.SqlCreator;
|
|
|
import com.yihu.ehr.framework.util.operator.StringUtil;
|
|
|
import org.hibernate.Criteria;
|
|
|
import org.hibernate.Query;
|
|
|
import org.hibernate.Session;
|
|
|
import org.hibernate.criterion.Order;
|
|
|
import org.hibernate.criterion.Restrictions;
|
|
|
import org.hibernate.jdbc.Work;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.jdbc.core.RowMapper;
|
|
|
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
|
|
@ -25,12 +10,6 @@ import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.Serializable;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
import java.sql.Statement;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@ -59,7 +38,8 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
return hibernateTemplate;
|
|
|
}
|
|
|
|
|
|
public DataGridResult getDataGridResult(String hql, Integer page, Integer rows) {
|
|
|
@Override
|
|
|
public DataGridResult getDataGridResult(String hql, Integer page, Integer rows) throws Exception {
|
|
|
|
|
|
DataGridResult dataGridResult = new DataGridResult();
|
|
|
|
|
@ -85,45 +65,54 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
return dataGridResult;
|
|
|
}
|
|
|
//=========================hibernate start==============================
|
|
|
public void beginTransaction() {
|
|
|
@Override
|
|
|
public void beginTransaction() throws Exception {
|
|
|
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().begin();
|
|
|
}
|
|
|
|
|
|
public void commitTransaction() {
|
|
|
@Override
|
|
|
public void commitTransaction() throws Exception {
|
|
|
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().commit();
|
|
|
}
|
|
|
|
|
|
public void saveEntity(Object entity) {
|
|
|
@Override
|
|
|
public void saveEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.save(entity);
|
|
|
}
|
|
|
|
|
|
public void updateEntity(Object entity) {
|
|
|
@Override
|
|
|
public void updateEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.update(entity);
|
|
|
}
|
|
|
|
|
|
public void saveOrUpdateEntity(Object entity) {
|
|
|
@Override
|
|
|
public void saveOrUpdateEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.saveOrUpdate(entity);
|
|
|
}
|
|
|
|
|
|
public void mergeEntity(Object entity) {
|
|
|
@Override
|
|
|
public void mergeEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.merge(entity);
|
|
|
}
|
|
|
|
|
|
public void deleteEntity(Object entity) {
|
|
|
@Override
|
|
|
public void deleteEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.delete(entity);
|
|
|
}
|
|
|
|
|
|
public <T> void deleteEntity(Class<T> cls, Serializable id) {
|
|
|
@Override
|
|
|
public <T> void deleteEntity(Class<T> cls, Serializable id) throws Exception {
|
|
|
T obj = (T) hibernateTemplate.load(cls, id);
|
|
|
hibernateTemplate.delete(obj);
|
|
|
}
|
|
|
|
|
|
public <T> T getEntity(Class<T> cls, Serializable id) {
|
|
|
@Override
|
|
|
public <T> T getEntity(Class<T> cls, Serializable id) throws Exception {
|
|
|
return (T) hibernateTemplate.get(cls, id);
|
|
|
}
|
|
|
//=========================hibernate end==============================
|
|
@ -137,7 +126,8 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryListBySql("select * from person")
|
|
|
* @author ding
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryListBySql(String sql) {
|
|
|
@Override
|
|
|
public List<Map<String, Object>> queryListBySql(String sql) throws Exception {
|
|
|
List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
|
|
|
return result;
|
|
|
}
|
|
@ -151,7 +141,8 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryListBySql("select * from person where username=?",new Object[]{"admin"})
|
|
|
* @author ding
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryListBySql(String sql, List params) {
|
|
|
@Override
|
|
|
public List<Map<String, Object>> queryListBySql(String sql, List params) throws Exception {
|
|
|
List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql, params.toArray());
|
|
|
return result;
|
|
|
}
|
|
@ -165,8 +156,9 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryListBySql("select * from person", Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@Override
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public <T> List<T> queryListBySql(String sql, Class obj) {
|
|
|
public <T> List<T> queryListBySql(String sql, Class obj) throws Exception {
|
|
|
RowMapper rowMapper = (RowMapper) ParameterizedBeanPropertyRowMapper.newInstance(obj);
|
|
|
List<T> result = this.jdbcTemplate.query(sql, rowMapper);
|
|
|
return result;
|
|
@ -182,8 +174,9 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryListBySql("select * from person where username=?",new Object[]{"admin"},Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@Override
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public <T> List<T> queryListBySql(String sql, List params, Class obj) {
|
|
|
public <T> List<T> queryListBySql(String sql, List params, Class obj) throws Exception {
|
|
|
RowMapper rowMapper = (RowMapper) ParameterizedBeanPropertyRowMapper.newInstance(obj);
|
|
|
List<T> result = this.jdbcTemplate.query(sql, params.toArray(), rowMapper);
|
|
|
return result;
|
|
@ -199,8 +192,9 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryObjBySql("select * from person where username=?",new Object[]{"admin"},Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@Override
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public <T> T queryObjBySql(String sql, List params, Class obj) {
|
|
|
public <T> T queryObjBySql(String sql, List params, Class obj) throws Exception {
|
|
|
RowMapper rowMapper = (RowMapper) ParameterizedBeanPropertyRowMapper.newInstance(obj);
|
|
|
Object result = this.jdbcTemplate.queryForObject(sql, params.toArray(), rowMapper);
|
|
|
return (T) result;
|
|
@ -215,8 +209,9 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryObjBySql("select * from person where username='admin'",Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@Override
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public <T> T queryObjBySql(String sql, Class obj) {
|
|
|
public <T> T queryObjBySql(String sql, Class obj) throws Exception {
|
|
|
RowMapper rowMapper = (RowMapper) ParameterizedBeanPropertyRowMapper.newInstance(obj);
|
|
|
Object result = this.jdbcTemplate.queryForObject(sql, rowMapper);
|
|
|
return (T) result;
|
|
@ -225,7 +220,8 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
/**
|
|
|
* 获取单列值
|
|
|
*/
|
|
|
public <T> T scalarBySql(String sql,Class cls) {
|
|
|
@Override
|
|
|
public <T> T scalarBySql(String sql,Class cls) throws Exception {
|
|
|
Object result = this.jdbcTemplate.queryForObject(sql, cls);
|
|
|
return (T) result;
|
|
|
}
|
|
@ -235,280 +231,10 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 执行SQL语句
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
public void execute(String sql) throws Exception
|
|
|
{
|
|
|
this.jdbcTemplate.execute(sql);
|
|
|
}
|
|
|
|
|
|
public <T> List getEntityList(Class<T> cls, String hql) {
|
|
|
Query query = getCurrentSession().createQuery(hql);
|
|
|
return query.list();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public <T> List getEntityList(Class<T> cls, String condition, String order, Integer limit, Integer offset, ErrorCode errorCode) {
|
|
|
try {
|
|
|
Session session = getCurrentSession();
|
|
|
Criteria criteria = session.createCriteria(cls);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
if (!StringUtil.isEmpty(condition)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(condition);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
if (jsonNode.get(fieldName).isInt()) {
|
|
|
criteria.add(Restrictions.eq(fieldName, jsonNode.get(fieldName).asInt()));
|
|
|
} else {
|
|
|
criteria.add(Restrictions.eq(fieldName, jsonNode.get(fieldName).asText()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!StringUtil.isEmpty(order)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(order);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText().toUpperCase();
|
|
|
if (value.equals(Constants.ASC)) {
|
|
|
criteria.addOrder(Order.asc(fieldName));
|
|
|
} else if (value.equals(Constants.DESC)) {
|
|
|
criteria.addOrder(Order.desc(fieldName));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (limit != null) {
|
|
|
criteria.setMaxResults(limit);
|
|
|
if (offset != null) {
|
|
|
criteria.setFirstResult(offset);
|
|
|
}
|
|
|
}
|
|
|
return criteria.list();
|
|
|
} catch (Exception e) {
|
|
|
throw new ApiException(errorCode);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public <T> List getEntityListByParentId(Class<T> cls, String parentFiledName, Integer patentId, String condition, String order, Integer limit, Integer offset, ErrorCode errorCode) {
|
|
|
try {
|
|
|
Session session = getCurrentSession();
|
|
|
Criteria criteria = session.createCriteria(cls);
|
|
|
criteria.add(Restrictions.eq(parentFiledName, patentId));
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
if (!StringUtil.isEmpty(condition)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(condition);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
if (jsonNode.get(fieldName).isInt()) {
|
|
|
criteria.add(Restrictions.eq(fieldName, jsonNode.get(fieldName).asInt()));
|
|
|
} else {
|
|
|
criteria.add(Restrictions.eq(fieldName, jsonNode.get(fieldName).asText()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!StringUtil.isEmpty(order)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(order);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText().toUpperCase();
|
|
|
if (value.equals(Constants.ASC)) {
|
|
|
criteria.addOrder(Order.asc(fieldName));
|
|
|
} else if (value.equals(Constants.DESC)) {
|
|
|
criteria.addOrder(Order.desc(fieldName));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (limit != null) {
|
|
|
criteria.setMaxResults(limit);
|
|
|
if (offset != null) {
|
|
|
criteria.setFirstResult(offset);
|
|
|
}
|
|
|
}
|
|
|
return criteria.list();
|
|
|
} catch (Exception e) {
|
|
|
throw new ApiException(errorCode);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List getList(Class tClass, String tableName, String condition, String order, Integer limit, Integer offset, ErrorCode errorCode) {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
try {
|
|
|
if (!StringUtil.isEmpty(condition)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(condition);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText();
|
|
|
if (NumberUtil.isInteger(value)) {
|
|
|
sqlCreator.equalCondition(fieldName, Integer.parseInt(value));
|
|
|
} else {
|
|
|
sqlCreator.equalCondition(fieldName, value);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!StringUtil.isEmpty(order)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(order);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText().toUpperCase();
|
|
|
sqlCreator.order(fieldName, value);
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
String sql = sqlCreator.selectData(tableName);
|
|
|
Query query = getQuery(sqlCreator, sql);
|
|
|
if (limit != null) {
|
|
|
query.setMaxResults(limit);
|
|
|
if (offset != null) {
|
|
|
query.setFirstResult(offset * limit);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return query.list();
|
|
|
}
|
|
|
|
|
|
public Integer getDataSetInt(Class tClass, String tableName, String condition) {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
try {
|
|
|
if (!StringUtil.isEmpty(condition)) {
|
|
|
JsonNode jsonNode = objectMapper.readTree(condition);
|
|
|
Iterator<String> fieldNames = jsonNode.fieldNames();
|
|
|
while (fieldNames.hasNext()) {
|
|
|
String fieldName = fieldNames.next();
|
|
|
String value = jsonNode.get(fieldName).asText();
|
|
|
if (NumberUtil.isInteger(value)) {
|
|
|
sqlCreator.equalCondition(fieldName, Integer.parseInt(value));
|
|
|
} else {
|
|
|
sqlCreator.equalCondition(fieldName, value);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
String sql = sqlCreator.countData(tableName);
|
|
|
Query query = getQuery(sqlCreator, sql);
|
|
|
Integer count = Integer.parseInt(StringUtil.toString(query.list().get(0)));
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object get(Class tClass, String tableName, Integer id, ErrorCode errorCode) {
|
|
|
try {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
sqlCreator.equalCondition("id", id);
|
|
|
String sql = sqlCreator.selectData(tableName);
|
|
|
Query query = getQuery(sqlCreator, sql);
|
|
|
return query.uniqueResult();
|
|
|
} catch (Exception e) {
|
|
|
throw new ApiException(errorCode);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Query getQuery(SqlCreator sqlCreator, String sql) {
|
|
|
Query query = getCurrentSession().createSQLQuery(sql);
|
|
|
for (String key : sqlCreator.getKeyValueMap().keySet()) {
|
|
|
Object value = sqlCreator.getKeyValueMap().get(key);
|
|
|
if (value instanceof Collection) {
|
|
|
query.setParameterList(key, (Collection) value);
|
|
|
} else {
|
|
|
query.setParameter(key, value);
|
|
|
}
|
|
|
}
|
|
|
query.setResultTransformer(sqlCreator.getTransformer());
|
|
|
return query;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void insertBatch(final List<String> insertSqlList) {
|
|
|
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
|
|
|
session.doWork(
|
|
|
new Work() {
|
|
|
public void execute(Connection connection) throws SQLException {
|
|
|
|
|
|
if (!CollectionUtil.isEmpty(insertSqlList)) {
|
|
|
Statement stmt = connection.createStatement();
|
|
|
connection.setAutoCommit(false);
|
|
|
for (String sql : insertSqlList) {
|
|
|
stmt.addBatch(sql);
|
|
|
}
|
|
|
try {
|
|
|
stmt.executeBatch();
|
|
|
} catch (Exception ex) {
|
|
|
LogService.getLogger().error("insertBatch-ERROR: ", ex);
|
|
|
}
|
|
|
stmt.close();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
|
|
|
public Session openSession() {
|
|
|
return getHibernateTemplate().getSessionFactory().openSession();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Session getCurrentSession() {
|
|
|
return getHibernateTemplate().getSessionFactory().getCurrentSession();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Integer getMaxId(String tableName) {
|
|
|
String sql = "select max(id) from " + tableName;
|
|
|
Query query = getCurrentSession().createSQLQuery(sql);
|
|
|
Object object = query.uniqueResult();
|
|
|
Integer maxId = object == null ? 1 : Integer.parseInt(object.toString()) + 1;
|
|
|
return maxId;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Query getExeuteQuery(SqlCreator sqlCreator, String sql) {
|
|
|
Query query = getCurrentSession().createSQLQuery(sql);
|
|
|
for (String key : sqlCreator.getKeyValueMap().keySet()) {
|
|
|
Object value = sqlCreator.getKeyValueMap().get(key);
|
|
|
if (value instanceof Collection) {
|
|
|
query.setParameterList(key, (Collection) value);
|
|
|
} else {
|
|
|
query.setParameter(key, value);
|
|
|
}
|
|
|
}
|
|
|
return query;
|
|
|
}
|
|
|
|
|
|
protected void doClose(Session session, Statement stmt, ResultSet rs) {
|
|
|
if (rs != null) {
|
|
|
try {
|
|
|
rs.close();
|
|
|
rs = null;
|
|
|
} catch (Exception ex) {
|
|
|
rs = null;
|
|
|
LogService.getLogger().error("close-ResultSet ", ex);
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (stmt != null) {
|
|
|
try {
|
|
|
stmt.close();
|
|
|
stmt = null;
|
|
|
} catch (Exception ex) {
|
|
|
stmt = null;
|
|
|
LogService.getLogger().error("close-ResultSet ", ex);
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|