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(); } }