import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class BaseData {
private static final Log log = LogFactory.getLog(BaseData.class);
Session session;
Transaction tx;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
/**
* 执行HQL语句查询
* @param hql
* @return
* @throws Exception
*/
public List queryByHql(String hql)throws Exception{
List list = null;
try {
list = session.createQuery(hql).list();
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return list;
}
/**
* 执行SQL语句查询
* @param hql
* @return
* @throws Exception
*/
public List queryBySql(String sql)throws Exception{
List list = null;
try {
list = session.createSQLQuery(sql).list();
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return list;
}
/**
* 查询所有记录
* @param obj
* @return
* @throws Exception
*/
public List queryAll(Class cls)throws Exception{
List list = null;
try {
list = session.createCriteria(cls).list();
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return list;
}
/**
* 通过ID查询
* @param cls
* @param id
* @return
* @throws Exception
*/
public Object queryById(Class cls, Serializable id)throws Exception{
Object obj = null;
try {
obj = session.get(cls,id);
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return obj;
}
/**
* 通过参数查询
* @param hql
* @param params
* @return
* @throws Exception
*/
public Object get(String hql, Serializable params) throws Exception {
Object obj = null;
try {
obj = session.load(hql, params);
}catch (Exception e) {
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭Session
session.close();
}
return obj;
}
//保存对象
public void saveData(Object obj) throws Exception {
try {
// 开始事务
tx = session.beginTransaction();
// 保存对象
session.save(obj);
//提交事务
commitDate();
}catch (Exception e) {
//事物回滚
rollbackDate();
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
//更新对象
public void updateData(Object obj) throws Exception {
try {
// 开始事务
tx = session.beginTransaction();
// 更新对象
session.update(obj);
//提交事务
commitDate();
}catch (Exception e) {
//事物回滚
rollbackDate();
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
//删除对象
public void deleteData(Object obj) throws Exception {
try {
// 开始事务
tx = session.beginTransaction();
// 删除对象
session.delete(obj);
//提交事务
commitDate();
}catch (Exception e) {
//事物回滚
rollbackDate();
log.error("系统加载数据时出错!");
log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
/**
* 多事务操作
* @throws Exception
*/
public void operatorData(List list)throws Exception{
try {
// 开始事务
tx = session.beginTransaction();
HashMap map = (HashMap)list.get(0);
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
if(key.indexOf("insert")!=-1){
session.save(entry.getValue());
}
else if(key.indexOf("update")!=-1){
session.update(entry.getValue());
}
else if(key.indexOf("delete")!=-1){
session.delete(entry.getValue());
}
}
//提交事物
commitDate();
}catch(Exception e){
//事物回滚
e.printStackTrace();
rollbackDate();
log.error("系统加载数据时出错!");
//log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
/**
* 多表操作事务操作
* @throws Exception
*/
public void operatorData(List insertlist,List updateList,List delList)throws Exception{
try {
// 开始事务
tx = session.beginTransaction();
if(insertlist != null && insertlist.size()>0){
for(int i =0; i<insertlist.size();i++){
session.save(insertlist.get(i));
}
}
if(updateList != null && updateList.size()>0){
for(int i =0; i<updateList.size();i++){
session.update(updateList.get(i));
}
}
if(delList != null && delList.size()>0){
for(int i =0; i<delList.size();i++){
session.delete(delList.get(i));
}
}
//提交事物
commitDate();
}catch(Exception e){
//事物回滚
e.printStackTrace();
rollbackDate();
log.error("系统加载数据时出错!");
//log.error(e.getStackTrace());
throw new Exception();
}finally{
//关闭session
closeSession();
}
}
//提交事务
public void commitDate() throws Exception{
tx.commit();
}
//回滚事务
public void rollbackDate() throws Exception{
tx.rollback();
}
//关闭session
public void closeSession() throws Exception{
session.close();
}
}
分享到:
相关推荐
最新 struts2 spring3 hibernate4 环境增删查改web开发 在开发环境:eclipse4+tomcat7+struts2+spring3+hibernate4+SQL2005 增删查改web开发简单示例,所括所有的包。下载后修改applicationContext.xml连接数据库的...
struts hibernate spring 整合,的例子有增删查改
Struts+Hibernate案例(实现增删查改)适合初学者 Struts+Hibernate案例(实现增删查改)适合初学者
hibernate4在j2ee环境下增删查改以及配置
jsf+hibernate+javabean完成对表的增删查改
Myeclipse自动生成Hibernate配置并对数据库进行增删查改测试 我也是新手,刚开始学习WEB编程,把自己的学习过程记录下来,供大家学习,避免重走我的弯路。
整合Spring MVC+Spring+Hibernate实现简单的增删查改,适合新手学习用
完整的Struts2+Spring2+Hibernate3增删查案例,包含库文件,需要增加一个ojdbc14.jar就可以运行。 Struts2 Spring2 Hibernate3
这里是hibernate增删查改和采用freemarker实现动态sql,hql
Flex + hibernate+sturts +lcd 增删查改1 还有个jar包 在后面叫 Flex + hibernate+sturts +lcd 增删查改2
纯干货,什么方法都有jdbctemp/hibernateTempl全部都有
基于hibernate和struts1的增删查改
hibernate双向一对多增删查改的例子
SSH增删查改完整示例,数据库齐全。导入后修改“applicationContext.xml”里的数据源配置即可运行。 Struts2+spring2.5+hibernate+mysql
Flex + hibernate+sturts +lcd 增删查改2(jar)
对struts2+hibernate+spring在MyEclipse下进行了整合,然后对oracle数据库实现了增删查改。
本程序是实现SSH实现增删查改,Struts2,Hibernate,Spring,Mysql。
搭建spring mvc +mybatis框架,利用MySQL数据库,实现对数据库表的增删查改操作
关于struts2 + hibernate +spring 的 一对一增删查改
因为上传时文件不能大于15MB,所以我删掉了一些jar包,要是出错了,请自己添加jar包,但是源码是不会出错的。