package com;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A data access object (DAO) providing persistence and search support for
* Student entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see com.Student
* @author MyEclipse Persistence Tools
*/
public class StudentDAO extends BaseHibernateDAO {
private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
// property constants
public static final String NO = "no";
public static final String NAME = "name";
public void save(Student transientInstance) {
log.debug("saving Student instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Student persistentInstance) {
log.debug("deleting Student instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Student findById(java.lang.Integer id) {
log.debug("getting Student instance with id: " + id);
try {
Student instance = (Student) getSession().get("com.Student", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Student instance) {
log.debug("finding Student instance by example");
try {
List results = getSession().createCriteria("com.Student").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Student instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Student as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByNo(Object no) {
return findByProperty(NO, no);
}
public List findByName(Object name) {
return findByProperty(NAME, name);
}
public List findAll() {
log.debug("finding all Student instances");
try {
String queryString = "from Student";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Student merge(Student detachedInstance) {
log.debug("merging Student instance");
try {
Student result = (Student) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Student instance) {
log.debug("attaching dirty Student instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Student instance) {
log.debug("attaching clean Student instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void delete(String id) {
System.out.println("执行删除操作");
// int i=Integer.parseInt(id);
Session session = HibernateSessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
String hql = "delete Student s where s.id="+id;
Query query = session.createQuery(hql);
//注意类型
// query.setParameter("id", i);
query.executeUpdate();
tx.commit();
HibernateSessionFactory.closeCurrentSession();
System.out.println("删除成功!");
}
public List GetStudent(String id)
{
Session session = HibernateSessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
String hql = "select s from Student as s where s.id="+id;
Query query = session.createQuery(hql);
List list = query.list();
tx.commit();
HibernateSessionFactory.closeCurrentSession();
return list;
}
public void updateStudent(String id,String no,String name)
{
Session session = HibernateSessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
// String hql = "update Student set name='"+name+"',no='"+no+"' where id="+id;
//不同的写法
String hql1="update Student as st set st.name=?,st.no=? where st.id=?";
Query query = session.createQuery(hql1);
query.setString(0, name);
query.setString(1, no);
query.setString(2, id);
query.executeUpdate();
tx.commit();
HibernateSessionFactory.closeCurrentSession();
System.out.println("更新成功!");
}
public void insertStudent(String no,String name)
{
Session session = HibernateSessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
Student user = new Student();
user.setName(name);
user.setNo(no);
session.save(user);
tx.commit();
HibernateSessionFactory.closeCurrentSession();
System.out.println("新增成功!");
}
public List findPagedAll(int currentPage, int pageSize) {
log.debug("分页查找");
try {
if (currentPage == 0) {
currentPage = 1;
}
String queryString = "from Student";
Query queryObject = getSession().createQuery(queryString);
queryObject.setFirstResult((currentPage - 1) * pageSize);
queryObject.setMaxResults(pageSize);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public int getUserTotalCount() {
Query q = getSession().createQuery("select count(*) from Student");
List cc = q.list();
Integer a = Integer.parseInt(cc.get(0).toString());
return a.intValue();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
hibweb.rar (53个子文件)
hibweb
.mystrutsdata 262B
.myhibernatedata 562B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
src
com
AbstractStudent.java 830B
StudentDAO.java 7KB
Student.java 676B
Student.hbm.xml 800B
yourcompany
struts
ApplicationResources.properties 89B
form
ChangeForm.java 1KB
AddForm.java 1KB
action
AddAction.java 1KB
ChangeAction.java 1KB
HibernateSessionFactory.java 4KB
Pagemodel.java 933B
BaseHibernateDAO.java 300B
IBaseHibernateDAO.java 214B
hibernate.cfg.xml 868B
.project 2KB
WebRoot
1.jsp 3KB
META-INF
MANIFEST.MF 36B
WEB-INF
struts-nested.tld 70KB
validator-rules.xml 13KB
struts-bean.tld 9KB
struts-html.tld 72KB
struts-logic.tld 14KB
struts-tiles.tld 8KB
web.xml 1KB
.struts-config.mex 1KB
classes
com
Student.hbm.xml 800B
yourcompany
struts
ApplicationResources.properties 89B
form
AddForm.class 1KB
ChangeForm.class 2KB
action
AddAction.class 1KB
ChangeAction.class 2KB
BaseHibernateDAO.class 454B
AbstractStudent.class 1KB
Student.class 1KB
IBaseHibernateDAO.class 168B
Pagemodel.class 778B
HibernateSessionFactory.class 3KB
StudentDAO.class 8KB
hibernate.cfg.xml 868B
struts-config.xml 1KB
lib
mysql-connector-java-5.1.7-bin.jar 693KB
3.jsp 1KB
del.jsp 982B
add.jsp 1KB
index.jsp 2KB
change.jsp 2KB
.mymetadata 291B
.classpath 780B
.myeclipse
共 53 条
- 1
wujifang
- 粉丝: 5
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页