急求简单的SSH(struts2.1+spring3.0+hibernate3.3)连MySQL数据库的增删改查的源码,求高手解答
3个回答
展开全部
import java.io.File;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import com.webcom.DB.HibernateSessionFactory;
import com.webcom.tools.Pager;
import com.webcom.tools.Time_util;
import com.webcom.vo.WebMessage;
import config.WebConfig;
/**
* Data access object (DAO) for domain model
* @author 张成刚
*/
public class BaseHibernateDAO{
private WebMessage mes = null;
private boolean bl = false;
private String message="保存信息操作成功";
public Time_util getTimeUtil(){
return new Time_util();
}
/*
* 获取当前Session
* */
protected Session getSession() {
return HibernateSessionFactory.getSession();
}
//测试s
public static void main(String[] args) {
// for(BsnsNews b:(List<BsnsNews>)new BaseDAO().getSession().createQuery("from BsnsNews").list())
// System.out.println(b.getNewsTitle());
}
//根据sql查询
protected List getReturnQuery(String hsql){
Transaction tx = getSession().beginTransaction();
tx.begin();
List list = getSession().createQuery(hsql).list();
tx.commit();
return list;
}
/*保存对像*/
protected WebMessage save(Object item) {
mes = new WebMessage();
Transaction tx = getSession().beginTransaction();
try {
tx.begin();
getSession().save(item);
tx.commit();
bl=true;
message= "保存信息成功";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
bl=false;
message= "保存操作失败:"+e.getMessage();
}
mes.setRes(bl);
mes.setErrorMessage(message);
return mes;
}
/*根据主建查询记录*/
protected Object find(Serializable id, Class clazz) {
Object obj = getSession().get(clazz, id);
return obj;
}
/*根据某属性查询对像*/
protected Object findByPro(String property, Object value, Class clazz) {
List list = getSession().createCriteria(clazz).add(
Expression.eq(property, value)).list();
if (!list.isEmpty())
return list.get(0);
return null;
}
/*删除对像*/
protected WebMessage delete(Serializable id, Class clazz) {
mes = new WebMessage();
Transaction tx = getSession().beginTransaction();
try {
tx.begin();
getSession().delete(this.find(id, clazz));
tx.commit();
bl = true;
message = "删除信息成功";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
bl = false;
e.getLocalizedMessage();//出错详细信息
message= "删除失败:"+e.getMessage();
}
mes.setRes(bl);
mes.setErrorMessage(message);
return mes;
}
/*更新对象*/
protected WebMessage update(Object item) {
mes = new WebMessage();
Transaction tx = getSession().beginTransaction();
try {
tx.begin();
getSession().update(item);
tx.commit();
bl = true;
message = "更新信息成功";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
bl = false;
message= "更新失败:"+e.getMessage();
}finally{
tx= null;
}
mes.setRes(bl);
mes.setErrorMessage(message);
return mes;
}
//分页用工具
/*获取分页起始记录*/
protected int getFirst(int page, int pageSize) {
return (page - 1) * pageSize;
}
/*获取分页最大记录*/
protected int getMax(int pageSize) {
return pageSize;
}
/*获得当前请求页*/
protected int getNowPage(String _page, String _pageSize, int counts) {
int page = 1;
int pageSize = this.getNowPageSize(_pageSize);
if (_page != null && !_page.equals(""))
page = Integer.parseInt(_page);
int first = (page - 1) * pageSize;
if (first >= counts && page > 1) {
page -= 1;
}
return page;
}
/*
* 获取当前请求每页显示数
* */
protected int getNowPageSize(String _pageSize) {
int pageSize = 10;
if (_pageSize != null && !_pageSize.equals(""))
pageSize = Integer.parseInt(_pageSize);
return pageSize;
}
/*分页设置*/
protected Pager setPager(int page, int pageSize, int counts, List results) {
Pager pg = new Pager();
pg.setPage(page); //当前请求页
pg.setPageSize(pageSize); //每页显示数据量
pg.setPageCont(counts); //总条数
pg.setPageList(results);//返回数据
return pg;
}
/**/
protected Pager getPagerData(String hsql, String _page, String _pageSize) {
Session se = getSession();
Transaction tx = getSession().beginTransaction();tx.begin();
//设置sql语句
Query qy = se.createQuery(hsql);
//分离sql语句
String flSQL = (qy.getQueryString().split("from"))[1].trim();
//查询总条数
Query qy_count = se.createQuery("select count(*) from " + flSQL);
Integer counts = ((Number) qy_count.uniqueResult()).intValue();
//初始化分页数据
int pageSize = getNowPageSize(_pageSize);
int page = getNowPage(_page, _pageSize, counts);
int first = getFirst(page, pageSize);
int max = getMax(pageSize);
//结果集
List results = qy.setFirstResult(first).setMaxResults(max).list();
//设置分页类
Pager pg = setPager(page, pageSize, counts, results);
tx.commit();
return pg;
}
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import com.webcom.DB.HibernateSessionFactory;
import com.webcom.tools.Pager;
import com.webcom.tools.Time_util;
import com.webcom.vo.WebMessage;
import config.WebConfig;
/**
* Data access object (DAO) for domain model
* @author 张成刚
*/
public class BaseHibernateDAO{
private WebMessage mes = null;
private boolean bl = false;
private String message="保存信息操作成功";
public Time_util getTimeUtil(){
return new Time_util();
}
/*
* 获取当前Session
* */
protected Session getSession() {
return HibernateSessionFactory.getSession();
}
//测试s
public static void main(String[] args) {
// for(BsnsNews b:(List<BsnsNews>)new BaseDAO().getSession().createQuery("from BsnsNews").list())
// System.out.println(b.getNewsTitle());
}
//根据sql查询
protected List getReturnQuery(String hsql){
Transaction tx = getSession().beginTransaction();
tx.begin();
List list = getSession().createQuery(hsql).list();
tx.commit();
return list;
}
/*保存对像*/
protected WebMessage save(Object item) {
mes = new WebMessage();
Transaction tx = getSession().beginTransaction();
try {
tx.begin();
getSession().save(item);
tx.commit();
bl=true;
message= "保存信息成功";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
bl=false;
message= "保存操作失败:"+e.getMessage();
}
mes.setRes(bl);
mes.setErrorMessage(message);
return mes;
}
/*根据主建查询记录*/
protected Object find(Serializable id, Class clazz) {
Object obj = getSession().get(clazz, id);
return obj;
}
/*根据某属性查询对像*/
protected Object findByPro(String property, Object value, Class clazz) {
List list = getSession().createCriteria(clazz).add(
Expression.eq(property, value)).list();
if (!list.isEmpty())
return list.get(0);
return null;
}
/*删除对像*/
protected WebMessage delete(Serializable id, Class clazz) {
mes = new WebMessage();
Transaction tx = getSession().beginTransaction();
try {
tx.begin();
getSession().delete(this.find(id, clazz));
tx.commit();
bl = true;
message = "删除信息成功";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
bl = false;
e.getLocalizedMessage();//出错详细信息
message= "删除失败:"+e.getMessage();
}
mes.setRes(bl);
mes.setErrorMessage(message);
return mes;
}
/*更新对象*/
protected WebMessage update(Object item) {
mes = new WebMessage();
Transaction tx = getSession().beginTransaction();
try {
tx.begin();
getSession().update(item);
tx.commit();
bl = true;
message = "更新信息成功";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
bl = false;
message= "更新失败:"+e.getMessage();
}finally{
tx= null;
}
mes.setRes(bl);
mes.setErrorMessage(message);
return mes;
}
//分页用工具
/*获取分页起始记录*/
protected int getFirst(int page, int pageSize) {
return (page - 1) * pageSize;
}
/*获取分页最大记录*/
protected int getMax(int pageSize) {
return pageSize;
}
/*获得当前请求页*/
protected int getNowPage(String _page, String _pageSize, int counts) {
int page = 1;
int pageSize = this.getNowPageSize(_pageSize);
if (_page != null && !_page.equals(""))
page = Integer.parseInt(_page);
int first = (page - 1) * pageSize;
if (first >= counts && page > 1) {
page -= 1;
}
return page;
}
/*
* 获取当前请求每页显示数
* */
protected int getNowPageSize(String _pageSize) {
int pageSize = 10;
if (_pageSize != null && !_pageSize.equals(""))
pageSize = Integer.parseInt(_pageSize);
return pageSize;
}
/*分页设置*/
protected Pager setPager(int page, int pageSize, int counts, List results) {
Pager pg = new Pager();
pg.setPage(page); //当前请求页
pg.setPageSize(pageSize); //每页显示数据量
pg.setPageCont(counts); //总条数
pg.setPageList(results);//返回数据
return pg;
}
/**/
protected Pager getPagerData(String hsql, String _page, String _pageSize) {
Session se = getSession();
Transaction tx = getSession().beginTransaction();tx.begin();
//设置sql语句
Query qy = se.createQuery(hsql);
//分离sql语句
String flSQL = (qy.getQueryString().split("from"))[1].trim();
//查询总条数
Query qy_count = se.createQuery("select count(*) from " + flSQL);
Integer counts = ((Number) qy_count.uniqueResult()).intValue();
//初始化分页数据
int pageSize = getNowPageSize(_pageSize);
int page = getNowPage(_page, _pageSize, counts);
int first = getFirst(page, pageSize);
int max = getMax(pageSize);
//结果集
List results = qy.setFirstResult(first).setMaxResults(max).list();
//设置分页类
Pager pg = setPager(page, pageSize, counts, results);
tx.commit();
return pg;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询