⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdao.java

📁 bbs论坛 采用java的Web开发strtus、hibernate
💻 JAVA
字号:
package com.wish.bbs.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import com.wish.bbs.factory.HibernateSessionFactory;
import com.wish.bbs.pojo.User;

public class UserDAO {
	Session session=null;
	public void getSession(){
		this.session=new Configuration().configure().buildSessionFactory().openSession();
	}
	
	public void save(User subject){
		this.session=HibernateSessionFactory.getSession();
		this.session.save(subject);
		this.session.beginTransaction().commit();
		HibernateSessionFactory.closeSession();
	}
	
	public void delete(String id) {
		this.session=HibernateSessionFactory.getSession();
		String hql = "DELETE FROM User as u WHERE u.id=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, id);
		q.executeUpdate();
		this.session.beginTransaction().commit();
		HibernateSessionFactory.closeSession();
	}
	
	public List getAllSubject(){
		this.session=HibernateSessionFactory.getSession();
		List list=null;
		String hql="select new Subject(s.sid,s.name,s.description,s.createdate,s.photo,s.status) From Subject as s";
		Query q=this.session.createQuery(hql);
		list=q.list();
		HibernateSessionFactory.closeSession();
		return list;
	}
	
	public User queryByName(String name) {
		this.session=HibernateSessionFactory.getSession();
		User p = null;
		String hql = "select new User(p.id,p.username,p.pwd,p.nickname,p.sex,p.birthday,p.email,p.phone,p.description,p.count,p.status,p.registerdate,p.role) FROM User AS p WHERE p.username=?";
		Query q = this.session.createQuery(hql);
		System.out.println("test");
		q.setString(0, name);
		List all = q.list();
		if (all.size() > 0) {
			p = (User) all.get(0);
		}
		HibernateSessionFactory.closeSession();
		return p;
	}

	public void update(User user){
		this.session=HibernateSessionFactory.getSession();
		this.session.update(user);
		this.session.beginTransaction().commit();
		HibernateSessionFactory.closeSession();
	}
	/*   public User(username,pwd,nickname,sex,birthday,email,phone,description,count,status,registerdate,role) {
 
	 * Subject(sid,name,description,createdate,photo,status) {
	 * public void update(Person per) {
		this.session.update(per);
		this.session.beginTransaction().commit();
	}

	public void delete(String id) {
		String hql = "DELETE FROM Person WHERE id=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, id);
		q.executeUpdate();
		this.session.beginTransaction().commit();
	}

	public Person queryById(String id) {
		Person p = null;
		String hql = "FROM Person AS p WHERE p.id=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, id);
		List all = q.list();
		if (all.size() > 0) {
			p = (Person) all.get(0);
		}
		return p;
	}
*/
	public void closeSession(){
		this.session.close();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -