项目目录:
代码:
User.java
package com.ayali.pojo;
import sunw.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private String age;
public User(Integer id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
public User() { }
public User(Integer id) {
this.id = id;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return this.age;
}
public void setAge(String age) {
this.age = age;
}
}
User.hbm.xml(自动生成)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.ayali.pojo.User" table="user_3">
<id name="id" type="java.lang.Integer" column="id">
<generator class="native" />
</id>
<property name="name" type="java.lang.String" column="name"
length="20">
</property>
<property name="age" type="java.lang.String" column="age"
length="3">
</property>
</class>
</hibernate-mapping>
UserTest.java
package com.ayali.test;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.ayali.pojo.User;
import com.ayali.util.HibernateUtil;
public class UserTest{
Session session;
/**
* 初始化资源 session
*/
@Before
public void before(){
session = HibernateUtil.getSession();
}
/**
* 释放 session
*/
@After
public void after(){
session.close();
}
/**
* 使用hibernate save() 方法 保存数据
*/
@Test
public void testSave(){
User user = new User();
user.setName("zhangsan");
user.setAge("33");
Transaction trans = session.beginTransaction();
session.save(user);
trans.commit();
}
/**
* 批量 save User
*/
@Test
public void testSaveUsers(){
for(int i=0; i<5; i++){
User user = new User();
user.setName("zhangsan-"+i);
session.save(user);
}
Transaction trans = session.beginTransaction();
trans.commit();
}
/**
* 使用 hibernate update() 更新数据
*/
@Test
public void testUpdate(){
User user = (User) session.load(User.class, 3);
user.setAge("22");
Transaction trans = session.beginTransaction();
session.update(user);
trans.commit();
}
/**
* 使用 hibernate delete() 删除数据
*
* 只能通过 id 删除
*/
@Test
public void testDelete(){
User user = new User();
user.setId(1);
Transaction trans = session.beginTransaction();
session.delete(user);
trans.commit();
}
/**
* 使用 hibernate 查询数据
*/
@Test
public void testSelect(){
//使用 hql 查询
String hql = "from User ";
Query query = HibernateUtil.getSession().createQuery(hql);
List list = query.list();
for(int i=0; i<list.size(); i++){
User user = (User)list.get(i);
System.out.println(user.getName());
}
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!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="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/ayali/pojo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>hibernatetest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>