1.新建java(maven)普通工程
2.配置maven,
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my</groupId>
<artifactId>hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hibernate</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--新加-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!--新加-->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!--新加-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.5.Final</version>
</dependency>
</dependencies>
</project>
3.建表语句:
CREATE TABLE `House` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`address` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
CREATE TABLE `Person` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
4.配置hibernate配置文件hibernate.cfg.xml 在sr下
<?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.url">jdbc:mysql://192.168.139.215:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.password">zouhuiying</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- mapping files -->
<!--house使用xml方式-->
<mapping resource="my/hibernate/House.hbm.xml"/>
<!--person使用java注解方式-->
<mapping class="my.hibernate.Person"/>
</session-factory>
</hibernate-configuration>
5.建与数据库对应的class House
package my.hibernate;
public class House {
private Integer id;
private String name;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "House [id=" + id + ", name=" + name + ", address=" + address
+ "]";
}
}
与House有的配置文件House.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="my.hibernate.House" table="House">
<id name="id" column="id">
<!--mysql自动生成主键的方式-->
<generator class="identity" />
</id>
<property name="name" column="name" />
<property name="address" column="address" />
</class>
</hibernate-mapping>
6.class Person
package my.hibernate;
import javax.persistence.AttributeOverride;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Person")//表名称和类名称相同时可以不添加此注解来说明
public class Person {
@Id
private Integer id;
private String name;
// @AttributeOverride(column = age, name = "AGE");//字段名称和属性名称相同可以不添加此注解来说明
private int age;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}
7.class SessionFactoryUtil 该类的作用是获取session
package my.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class SessionFactoryUtil {
private static org.hibernate.SessionFactory sessionFactory;
private SessionFactoryUtil() {
}
static {
// 使用xml文件或者注解方式加载hibernate配置
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
// 只是用xml文件方式加载hibernate配置
// sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getInstance() {
return sessionFactory;
}
/**
* 打开会话但不绑定到会话上下文中
* @return the session
*/
public Session openSession() {
return sessionFactory.openSession();
}
/**
* 从会话上下文中返回会话,如果上下文中不存在会话示例则先创建一个会话示例并保存到上下文中,然后再返回。
* <br>
* 会话上下文与hibernate配置中的current_session_context_class属性值有关。
* @return the session
*/
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
/**
* 关闭会话工厂
*/
public static void close() {
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;
}
}
8.测试
package test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import my.hibernate.House;
import my.hibernate.Person;
import my.hibernate.SessionFactoryUtil;
import junit.framework.TestCase;
public class HiTest{
@Test
public void testAS(){
Session session = SessionFactoryUtil.getInstance().getCurrentSession();
Transaction tx = session.beginTransaction();
System.out.println("kkkkkkkkkkkkk");
House house = new House();//瞬态
System.out.println("11111111111111k");
house.setName("dd");
house.setAddress("vv");
session.save(house);//持久态
//对持久态对象进行修改
house.setAddress("yj1212");
Person person = new Person();//瞬态
person.setId(2);
person.setAge(24);
person.setName("san");
session.save(person);//持久态
// session.delete(person);//session关闭后此person便会成为托管态对象
tx.commit();//提交事务,提交后会自动关闭session
// session.close();
}
}
分享到:
相关推荐
Spring Hibernate 事务处理 详细说明 Spring Hibernate 事务处理 详细说明
hibernate事务管理机制.dochibernate事务管理机制.doc
Hibernate事务和并发控制 实例,想深入理解Hiberante的朋友,可以去研究一下。
Hibernate事务(源码) 不理解可以下载参考!!!
通过实例你也许会明白hibernate事务,并发及缓存管理的运行原理,如果对这个感兴趣的可以看看
Spring+Hibernate 使用TransactionInterceptor声明式事务配置
在Spring中配置Hibernate事务
了解Hibernate的一个很好的文档,想学Hibernate的朋友可以载来看看。
Hibernate事务和并发控制Hibernate事务和并发控制Hibernate事务和并发控制
NULL 博文链接:https://microjava.iteye.com/blog/525973
Hibernate事务管理.很不错的哦!拿出来和大家一起来学习和分享哦!
1.什么是Hibernate? (1) hibernate一种ORM工具,是连接 java 应用程序 和关系数据库的中间件,这是对JDBC的封装,主要负 责 java 对象的持久化. (2)Hibernate是自动根据xml配置完成对象映射关系, 并根据提供的API...
JDBC和hibernate事务的详解,PPT格式,看一下吧
在spring中配置hibernate事务
hibernate 事务管理注意事项
Spring 1_2的hibernate事务处理,更加详细的说明事务的处理在SPRING中
Hibernate事务处理Hibernate事务处理
hibernate事务,自己写的,通过测试了。 有需要的朋友可以参考一下。
Hibernate事务.doc
Hibernate 事务隔离级别 深入探究 在 Hibernate 中,事务隔离级别是指数据库系统提供的一种机制,以解决并发事务带来的问题。为了确保数据库的可靠性和一致性,Hibernate 提供了四种事务隔离级别,分别是 ...