hibernate增刪差改

never123450發表於2014-05-25
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
	package="cn.xwy.domain">

	<class name="User" >
		<id name="id">
			<generator class="native"/>
		</id>
		
		<property name="name" unique="true"/>
		<property name="birthday"/>
	</class>
	
</hibernate-mapping>


<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
	<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="connection.url">jdbc:mysql:///test</property>
	<property name="connection.username">root</property>
	<property name="connection.password">root</property>
	
	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	
	<property name="hbm2ddl.auto">update</property>
	<property name="show_sql">true</property>
	
	<mapping resource="cn/xwy/domain/User.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>

HibernateUtil.java

package cn.xwy.dao;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public final class HibernateUtil {
	private static SessionFactory sessionFactory ;
	
	private HibernateUtil(){
		
	}
	static{
		Configuration cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
	}
}

user.java

package cn.xwy.domain;

import java.util.Date;

public class User {

	private int id;
	private String name;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

UserDao.java

package cn.xwy.dao;

import cn.xwy.domain.User;

public interface UserDao {
	public void saveUser(User user);
	
	public User findUserByName(String name);
	
	public User findUserById(int id);
	
	public void updateUser(User user);
	
	public void remove(User user);
}

UserDaohiberbateImpl.java

package cn.xwy.dao.impl;



import org.apache.tools.ant.taskdefs.Tstamp;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import cn.xwy.dao.HibernateUtil;
import cn.xwy.dao.UserDao;
import cn.xwy.domain.User;

public class UserDaohiberbateImpl implements UserDao{

	@Override
	public void saveUser(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.save(user);
			tx.commit();
		} finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public User findUserByName(String name) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			Criteria c = s.createCriteria(User.class);
			c.add(Restrictions.eq("name",name));
			User user = (User)c.uniqueResult();
			return user;
		} finally{
			if(s!=null)
				s.close();
		}
	}
	
	public User findUserByName1(String name) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from User as user where user.name=:n";
			Query q = s.createQuery(hql);
			q.setString("n",name);
			User user = (User)q.uniqueResult();
			return user;
		} finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public User findUserById(int id) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			User user = (User) s.get(User.class,id);
			return user;
		} finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void updateUser(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction();
			s.update(user);
			tx.commit();
		} finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void remove(User user) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx =  s.beginTransaction();
			s.delete(user);
			tx.commit();
		} finally{
			if(s!=null)
				s.close();
		}
		
	}

}

DaoTest.java

package cn.xwy;

import java.util.Date;

import cn.xwy.dao.UserDao;
import cn.xwy.dao.impl.UserDaohiberbateImpl;
import cn.xwy.domain.User;

public class DaoTest {

	public static void main(String[] args){
		UserDao dao = new UserDaohiberbateImpl();
		User user = new User();
		user.setName("name");
		user.setBirthday(new Date());
		System.out.println("1111");
		dao.saveUser(user);
		
		user.setName("new name");
		System.out.println("2222");
		dao.updateUser(user);
		
		User u = dao.findUserByName(user.getName());
		System.out.println("3333");
		dao.remove(u);
	}
}


相關文章