Hibernate是一个开放源代码的对象关系映射框架
1.对JDBC进行了非常轻量级的对象封装
2.将POJO与数据库表建立映射关系
3.是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行
po类Student.java
package beans.po;
public class Student {
private String stuId;
private String stuName;
public String getStuId() {
return stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String toString() {
return "Student [stuId=" + stuId + ", stuName=" + stuName + "]";
}
}
Factory类HibernateSessionFactory.java
package beans.factory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
dao层 StudentDao.java
package beans.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import beans.factory.HibernateSessionFactory;
import beans.po.Student;
public class StudentDao {
private Session session;
private Transaction tr;
public StudentDao(){
session = HibernateSessionFactory.getSession();
tr =session.beginTransaction();
}
public void close(){
try {
if(tr!=null){
tr.commit();
}
if(session!=null){
session.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//增
public boolean insertStudent(Student student){
boolean result = false;
try {
session.save(student);
result = true;
} catch (Exception e) {
e.printStackTrace();
tr.rollback();
} finally{
close();
}
return result;
}
//删
public boolean deleteStudent(Student student){
boolean result = false;
try {
session.delete(student);
result = true;
} catch (Exception e) {
e.printStackTrace();
tr.commit();
} finally{
close();
}
return result;
}
//改
public boolean updateStudent(Student student){
boolean result = false;
try {
session.delete(student);
result = true;
} catch (Exception e) {
e.printStackTrace();
tr.commit();
} finally{
close();
}
return result;
}
//更新表数据
public int executeUpdate(String sql){
int result = -1;
try {
result = session.createSQLQuery(sql).executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally{
close();
}
return result;
}
//查询数据
public List executeQuery(String sql){
List<Object> list =null;
try {
list=session.createQuery(sql).list();
} catch (Exception e) {
e.printStackTrace();
} finally{
close();
}
return list;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.po.Student" table="student" catalog="test">
<id name="stuId" type="java.lang.String">
<column name="stu_id" length="15" />
<generator class="assigned" />
</id>
<property name="stuName" type="java.lang.String">
<column name="stu_name" length="30" />
</property>
</class>
</hibernate-mapping>
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.username">root</property>
<property name="connection.password">mysql</property>
<property name="connection.url">jdbc:mysql://localhost:3308/test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">lg</property>
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>