1.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="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">7654321</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping resource="hibernatedemo/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.工具類
package hibernatedemo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Util {
static Session getSession(){
Configuration cfg = new Configuration();
//讀取配置文件
cfg.configure();
//得到session
Session session = cfg.buildSessionFactory().openSession();
return session;
}
}
3.實體類
package hibernatedemo;
import java.sql.Date;
public class Student {
private Integer id;
private String name;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
4.Student.hbm.xml配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernatedemo.Student" table="student">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<property name="birthday" type="java.sql.Date">
<column name="birthday"></column>
</property>
</class>
</hibernate-mapping>
5.實現類
package hibernatedemo;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateAdd {
Session session = null;//會話session
Transaction transaction = null;//事務
public void add(Student stu){
//開啓會話
session = Util.getSession();
transaction=session.beginTransaction();
try {
//開啓事務
transaction.begin();
//插入數據
session.save(stu);
//提交事務
transaction.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//關閉連接
session.close();
}
}
public List<Student> findAll(){
List<Student> list = new ArrayList<Student>();
session = Util.getSession();
transaction=session.beginTransaction();
try {
//開啓事務
transaction.begin();
//定義sql語句
String sql ="from Student";
Query query = session.createQuery(sql);
//將數據封裝到list中
list = query.list();
transaction.commit();
} catch (Exception e) {
// TODO: handle exception
}finally{
session.close();
}
return list;
}
}
6.結構圖