hibernate3.2之helloword

 

http://nchc.dl.sourceforge.net/project/hibernate/hibernate3/3.2.5.ga/hibernate-3.2.5.ga.zip  //hibernate lib 下载

需要

1.hibernate3.jar

2./lib/*.jar  所有的依赖包

 

hibernate 的三种状态以及三种状态的关系
hibernate3.2之helloword_第1张图片


hibernate3.2之helloword_第2张图片

 


hibernate3.2之helloword_第3张图片

hiberate curd的一些关系


hibernate3.2之helloword_第4张图片

 

 

hibernate.cfg.xml  //hibernate 配置

 

<!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:///easier</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<property name="hbm2ddl.auto">update</property>
		<property name="show_sql">true</property>
		<mapping  resource="hbm/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 User.hbm.xml  //mapper 映射

 

<?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.easier.hibernate.domain">
	<!-- 表名如果是关键字
		1.改表名
		2.加反引号 (数字键1前面那个键)
	 -->
	<class name="User" table="`users`"  >
		<comment>Users may bid for or sell auction items.</comment>
		
		<id name="id">
			<generator class="native"/>
		</id>
		<!-- 字段如果是关键字
		1.Colume改字段名
		2.也加反引号 (数字键1前面那个键)
	 -->
		<property name="name" length="10" not-null="true" type="java.lang.String"/>
		<property name="birthday" not-null="true" type="java.util.Date"  />
	</class>
	
</hibernate-mapping>

 

user.java //domain

 

/**
 * 
 */
package cn.easier.hibernate.domain;

import java.io.Serializable;
import java.util.Date;

/**
 * @author 
 * 
 */
 public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private Date birthday;

	public User() {
	}

	/**
	 * @param id
	 * @param name
	 * @param birthday
	 */
	public User(String name, Date birthday) {
		this.name = name;
		this.birthday = birthday;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer 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;
	}

}

 UserTest.java  //junit 测试类

 

package cn.easier.hibernate.junit;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.BeforeClass;
import org.junit.Test;

import cn.easier.hibernate.domain.User;

/**
 * 
 */

/**
 * @author  
 * 
 */
public class UserTest {

	// 如果hibernate.cfg.xml不在classes或bin目录下,configure()里面就要写文件路径的全名称;默认就是在classes或bin目录下;
	private static Configuration cf = new Configuration().configure();
	private static SessionFactory sf = null;
	private static Session session = null;

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		sf = cf.buildSessionFactory();

	}

	@Test
	public void save() {

		User user = new User();
		user.setName("zhangsan");
		user.setBirthday(new Date());
		session = sf.openSession();
		Transaction ts = session.getTransaction();
		ts.begin();
		session.save(user);
		ts.commit();
		session.close();
		sf.close();
	}

	@Test
	public void delete() {
		Integer delete_id = 3;
		session = sf.openSession();
		Transaction ts = session.beginTransaction();
		// 第一种
		User user = new User("aa", new Date()); // user 的其它属性一定要赋值,不然会报错
		user.setId(delete_id);
		session.delete(user);

		// 第二种
		// Query query = session.createQuery("delete User u where  u.id=:id");
		// delete User u where u.id=:id :id[命名参数]表示起个别名叫id
		// query.setInteger("id", delete_id); 赋值
		
		// Query query = session.createQuery("delete User u where  u.id=?");
		// query.setParameter(0, delete_id);  //索引是从0开始
		// query.executeUpdate();

		ts.commit();
		session.close();
		sf.close();
	}

	@Test
	public void update() {
		Integer query_id = 1;
		session = sf.openSession();
		User user = new User("lisi1", new Date());
		user.setId(query_id);
		Transaction ts = session.beginTransaction();
		session.update(user);
		ts.commit();
		session.close();
		sf.close();
	}

	@Test
	public void get() {
		session = sf.openSession();
		Class<User> clazz = User.class;
		User user = (User) session.get(clazz, 1);
		if (user != null) {

			System.out.println("name:" + user.getName());
		}
		session.close();
		sf.close();
	}
	@Test public void hqlGet(){
		session = sf.openSession();
		Query query=session.createQuery("select count(*) from User");
		
		/*org.hibernate.hql.QueryExecutionRequestException: 
		 * Not supported for select queries [select count(*) from cn.easier.hibernate.domain.User]
		 * query.executeUpdate()  只能执行update 或delete
		 * */
		System.out.println("count:" + query.uniqueResult());
		session.close();
		sf.close();
		
	}
	@Test public void criteria(){
		session = sf.openSession();
		Criteria c=session.createCriteria(User.class);
		//c.add(Restrictions.idEq(1));  查询id=1的数据
		
		System.out.println("count:" + c.setProjection(Projections.rowCount()).uniqueResult());
		session.close();
		sf.close();
		
	}

}

你可能感兴趣的:(hibernate3)