|
@ -1,518 +0,0 @@
|
|
|
package com.yihu.ehr.framework.common.dao;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
|
|
import com.yihu.ehr.framework.constrant.Constants;
|
|
|
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.operator.StringUtil;
|
|
|
import com.yihu.ehr.framework.util.sql.SqlCreator;
|
|
|
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;
|
|
|
import org.springframework.orm.hibernate4.HibernateTemplate;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.transaction.Transactional;
|
|
|
import java.io.IOException;
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 基础DAO。目的是为了简化在各个业务类访问数据库的时候, 不需要再通过注入 sessionFactory, 即以下代码:
|
|
|
* <p>
|
|
|
* Session session = sessionFactory.getuCurrentSession();
|
|
|
* session.doSomething(Object);
|
|
|
*
|
|
|
* @author Sand
|
|
|
* @version 1.0
|
|
|
* @created 2015.07.09 17:08
|
|
|
*/
|
|
|
@Transactional
|
|
|
@Repository("sqlGeneralDAO")
|
|
|
public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
@Resource(name = "jdbcTemplate")
|
|
|
protected JdbcTemplate jdbcTemplate;
|
|
|
@Resource(name = "hibernateTemplate")
|
|
|
protected HibernateTemplate hibernateTemplate;
|
|
|
|
|
|
public JdbcTemplate getJdbcTemplate() {
|
|
|
return jdbcTemplate;
|
|
|
}
|
|
|
|
|
|
public HibernateTemplate getHibernateTemplate() {
|
|
|
return hibernateTemplate;
|
|
|
}
|
|
|
|
|
|
public DataGridResult getDataGridResult(String hql, Integer page, Integer rows) {
|
|
|
|
|
|
DataGridResult dataGridResult = new DataGridResult();
|
|
|
|
|
|
//"FROM Users as s where s.name=:name and s.password =:password
|
|
|
Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(hql);
|
|
|
|
|
|
if(page!=null&&rows!=null)
|
|
|
{
|
|
|
dataGridResult.setPageSize(rows);
|
|
|
dataGridResult.setCurrPage(page);
|
|
|
//设置大小
|
|
|
dataGridResult.setTotalCount(query.list().size());
|
|
|
//设置分页后在查一次数据
|
|
|
query.setMaxResults(rows);
|
|
|
query.setFirstResult((page - 1) * rows);
|
|
|
dataGridResult.setDetailModelList(query.list());
|
|
|
}
|
|
|
else{
|
|
|
List list = query.list();
|
|
|
dataGridResult.setDetailModelList(list);
|
|
|
dataGridResult.setTotalCount(list.size());
|
|
|
}
|
|
|
return dataGridResult;
|
|
|
}
|
|
|
//=========================hibernate start==============================
|
|
|
public void beginTransaction() throws Exception {
|
|
|
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().begin();
|
|
|
}
|
|
|
|
|
|
public void commitTransaction() throws Exception {
|
|
|
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().commit();
|
|
|
}
|
|
|
|
|
|
public void saveEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.save(entity);
|
|
|
}
|
|
|
|
|
|
public void updateEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.update(entity);
|
|
|
}
|
|
|
|
|
|
public void saveOrUpdateEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.saveOrUpdate(entity);
|
|
|
}
|
|
|
|
|
|
public void mergeEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.merge(entity);
|
|
|
}
|
|
|
|
|
|
public void deleteEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.delete(entity);
|
|
|
}
|
|
|
|
|
|
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) throws Exception {
|
|
|
return (T) hibernateTemplate.get(cls, id);
|
|
|
}
|
|
|
//=========================hibernate end==============================
|
|
|
|
|
|
/****************************jdbcTemplate操作数据库*************************/
|
|
|
/**
|
|
|
* 根据sql语句查询List
|
|
|
*
|
|
|
* @param sql
|
|
|
* @return List<Map对象>
|
|
|
* 例如: queryListBySql("select * from person")
|
|
|
* @author ding
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryListBySql(String sql) throws Exception {
|
|
|
List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据sql语句、参数值查询List
|
|
|
*
|
|
|
* @param sql
|
|
|
* @param params
|
|
|
* @return List<Map对象>
|
|
|
* 例如: queryListBySql("select * from person where username=?",new Object[]{"admin"})
|
|
|
* @author ding
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据sql语句、Class实例查询List
|
|
|
*
|
|
|
* @param sql
|
|
|
* @param obj
|
|
|
* @return List<VO对象>
|
|
|
* 例如: queryListBySql("select * from person", Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据sql语句、参数值、Class实例查询List
|
|
|
*
|
|
|
* @param sql
|
|
|
* @param params
|
|
|
* @param obj
|
|
|
* @return List<VO对象>
|
|
|
* 例如: queryListBySql("select * from person where username=?",new Object[]{"admin"},Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据sql语句、参数值、Class实例查询Object
|
|
|
*
|
|
|
* @param sql
|
|
|
* @param params
|
|
|
* @param obj
|
|
|
* @return VO对象
|
|
|
* 例如: queryObjBySql("select * from person where username=?",new Object[]{"admin"},Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据sql语句、参数值查询Object
|
|
|
*
|
|
|
* @param sql
|
|
|
* @param obj
|
|
|
* @return VO对象
|
|
|
* 例如: queryObjBySql("select * from person where username='admin'",Person.class)
|
|
|
* @author ding
|
|
|
*/
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取单列值
|
|
|
*/
|
|
|
public <T> T scalarBySql(String sql,Class cls) throws Exception {
|
|
|
Object result = this.jdbcTemplate.queryForObject(sql, cls);
|
|
|
return (T) result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 执行SQL语句
|
|
|
* @return
|
|
|
*/
|
|
|
public void execute(String sql) throws Exception
|
|
|
{
|
|
|
this.jdbcTemplate.execute(sql);
|
|
|
}
|
|
|
|
|
|
|
|
|
/******************** 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) throws IOException {
|
|
|
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 - 1) * limit);
|
|
|
}
|
|
|
}
|
|
|
return criteria.list();
|
|
|
}
|
|
|
|
|
|
public <T> List getEntityListByParentId(Class<T> cls, String parentFiledName, Integer patentId, String condition, String order, Integer limit, Integer offset) throws IOException {
|
|
|
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 - 1) * limit);
|
|
|
}
|
|
|
}
|
|
|
return criteria.list();
|
|
|
}
|
|
|
|
|
|
public List getList(Class tClass, String tableName, String condition, String order, Integer limit, Integer offset) {
|
|
|
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 (jsonNode.get(fieldName).getNodeType().equals(JsonNodeType.NUMBER)) {
|
|
|
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 List getListByIdList(Class tClass, String tableName, String fieldName, List<Integer> idList) {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
try {
|
|
|
sqlCreator.inCondition(fieldName, idList);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
String sql = sqlCreator.selectData(tableName);
|
|
|
Query query = getQuery(sqlCreator, sql);
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
public Object get(Class tClass, String tableName, Integer id) {
|
|
|
SqlCreator sqlCreator = new SqlCreator(tClass);
|
|
|
sqlCreator.equalCondition("id", id);
|
|
|
String sql = sqlCreator.selectData(tableName);
|
|
|
Query query = getQuery(sqlCreator, sql);
|
|
|
return query.uniqueResult();
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|