|
@ -22,7 +22,6 @@ 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 org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.Serializable;
|
|
@ -45,7 +44,6 @@ import java.util.Map;
|
|
|
* @version 1.0
|
|
|
* @created 2015.07.09 17:08
|
|
|
*/
|
|
|
@Transactional
|
|
|
@Repository("sqlGeneralDAO")
|
|
|
public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
@Resource(name = "jdbcTemplate")
|
|
@ -87,45 +85,45 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
return dataGridResult;
|
|
|
}
|
|
|
//=========================hibernate start==============================
|
|
|
public void beginTransaction() {
|
|
|
public void beginTransaction() throws Exception {
|
|
|
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().begin();
|
|
|
}
|
|
|
|
|
|
public void commitTransaction() {
|
|
|
public void commitTransaction() throws Exception {
|
|
|
hibernateTemplate.getSessionFactory().getCurrentSession().getTransaction().commit();
|
|
|
}
|
|
|
|
|
|
public void saveEntity(Object entity) {
|
|
|
public void saveEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.save(entity);
|
|
|
}
|
|
|
|
|
|
public void updateEntity(Object entity) {
|
|
|
public void updateEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.update(entity);
|
|
|
}
|
|
|
|
|
|
public void saveOrUpdateEntity(Object entity) {
|
|
|
public void saveOrUpdateEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.saveOrUpdate(entity);
|
|
|
}
|
|
|
|
|
|
public void mergeEntity(Object entity) {
|
|
|
public void mergeEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.merge(entity);
|
|
|
}
|
|
|
|
|
|
public void deleteEntity(Object entity) {
|
|
|
public void deleteEntity(Object entity) throws Exception {
|
|
|
if (entity == null) return;
|
|
|
hibernateTemplate.delete(entity);
|
|
|
}
|
|
|
|
|
|
public <T> void deleteEntity(Class<T> cls, Serializable id) {
|
|
|
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) {
|
|
|
public <T> T getEntity(Class<T> cls, Serializable id) throws Exception {
|
|
|
return (T) hibernateTemplate.get(cls, id);
|
|
|
}
|
|
|
//=========================hibernate end==============================
|
|
@ -139,7 +137,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* 例如: queryListBySql("select * from person")
|
|
|
* @author ding
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryListBySql(String sql) {
|
|
|
public List<Map<String, Object>> queryListBySql(String sql) throws Exception {
|
|
|
List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
|
|
|
return result;
|
|
|
}
|
|
@ -153,7 +151,7 @@ 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) {
|
|
|
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;
|
|
|
}
|
|
@ -168,7 +166,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* @author ding
|
|
|
*/
|
|
|
@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;
|
|
@ -185,7 +183,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* @author ding
|
|
|
*/
|
|
|
@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;
|
|
@ -202,7 +200,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* @author ding
|
|
|
*/
|
|
|
@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;
|
|
@ -218,7 +216,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
* @author ding
|
|
|
*/
|
|
|
@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;
|
|
@ -227,7 +225,7 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
/**
|
|
|
* 获取单列值
|
|
|
*/
|
|
|
public <T> T scalarBySql(String sql,Class cls) {
|
|
|
public <T> T scalarBySql(String sql,Class cls) throws Exception {
|
|
|
Object result = this.jdbcTemplate.queryForObject(sql, cls);
|
|
|
return (T) result;
|
|
|
}
|
|
@ -242,6 +240,9 @@ public class SQLGeneralDAO implements XSQLGeneralDAO {
|
|
|
this.jdbcTemplate.execute(sql);
|
|
|
}
|
|
|
|
|
|
|
|
|
/******************** sql构造器 *****************************************************/
|
|
|
|
|
|
public <T> List getEntityList(Class<T> cls, String hql) {
|
|
|
Query query = getCurrentSession().createQuery(hql);
|
|
|
return query.list();
|