hibernate 入门案例

1,创建工程,导入jar

ojdbc14.jar 数据库驱动包,我用的是oracle数据库,根据实际的数据库选择驱动包

 

 创建java类,并利用bibernate插件完成orm映射,创建hbm.xml  cfg.xml

调整下表名称和主键生成方式

创建测试代码

 

 

 

 1 package com.lxl.it.vo;
 2 
 3 public class Student {
 4 private String name;
 5 private int age;
 6 private int id;
 7 public String getName() {
 8     return name;
 9 }
10 public void setName(String name) {
11     this.name = name;
12 }
13 public int getAge() {
14     return age;
15 }
16 public void setAge(int age) {
17     this.age = age;
18 }
19 public int getId() {
20     return id;
21 }
22 public void setId(int id) {
23     this.id = id;
24 }
25 @Override
26 public String toString() {
27     return "Student [name=" + name + ", age=" + age + ", id=" + id + "]";
28 }
29 
30 }
java类
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3                                    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2016-9-5 14:36:05 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping>
 6  <class name="com.lxl.it.vo.Student" table="STUDENT">
 7   <id name="id" type="int">
 8    <column name="ID"/>
 9     <generator class="sequence">
10                 <param name="sequence">SEQ_LXL_TEST</param>
11     </generator>
12   </id>
13   <property generated="never" lazy="false" name="name" type="java.lang.String">
14    <column name="NAME"/>
15   </property>
16   <property generated="never" lazy="false" name="age" type="int">
17    <column name="AGE"/>
18   </property>
19  </class>
20 </hibernate-mapping>
类的映射文件
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 8         <property name="hibernate.connection.password">bspdev</property>
 9         <property name="hibernate.connection.url">jdbc:oracle:thin:@10.202.6.37:1521:bspdev1</property>
10         <property name="hibernate.connection.username">bsp</property>
11         <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
12     <!-- 执行操作时是否在控制台打印 SQL -->
13   <property name="show_sql">true</property>
14   <!-- 是否对 SQL 进行格式化 -->
15   <property name="format_sql">true</property>
16       <!-- 生成数据表的策略 -->
17         <property name="hbm2ddl.auto">update</property>
18   <!-- 指定自动生成数据表的策略 -->
19   <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
20   <!-- 指定关联的 .hbm.xml 文件 -->
21   <mapping resource="com/atguigu/hibernate/helloworld/News.hbm.xml"/>
22      <mapping resource="com/lxl/it/vo/Student.hbm.xml"/>
23  </session-factory>
24 </hibernate-configuration>
hibernate主配置文件
 1 package com.lxl.it.vo;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.cfg.Configuration;
 7 import org.hibernate.service.ServiceRegistry;
 8 import org.hibernate.service.ServiceRegistryBuilder;
 9 import org.junit.After;
10 import org.junit.Before;
11 import org.junit.Test;
12 
13 public class StudentTest {
14     private SessionFactory sessionFactory;
15     private Session session;
16     private Transaction transaction;
17 
18     @Before
19     public void init() {
20         Configuration configuration = new Configuration().configure();
21         ServiceRegistry serivceRegistry = new ServiceRegistryBuilder()
22                 .applySettings(configuration.getProperties())
23                 .buildServiceRegistry();
24         sessionFactory=configuration.buildSessionFactory(serivceRegistry);
25         session =sessionFactory.openSession();
26         transaction=session.beginTransaction();
27     }
28     @After
29     public void destory(){
30         transaction.commit();
31         session.close();
32         sessionFactory.close();
33     }
34     @Test
35     public void testSessionCache(){
36         Student s=(Student)session.get(Student.class, 1);
37         System.out.println(s);
38         Student s2=(Student)session.get(Student.class, 1);
39         System.out.println(s);
40     }
41 
42     @Test
43     public void test() {
44         Configuration configuration = new Configuration().configure();
45         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
46                 .applySettings(configuration.getProperties())
47                 .buildServiceRegistry();
48         SessionFactory sessionFactory = configuration
49                 .buildSessionFactory(serviceRegistry);
50         Session session = sessionFactory.openSession();
51         Transaction transcation = session.beginTransaction();
52         Student s = new Student();
53         s.setAge(10);
54         s.setName("lxl");
55         session.save(s);
56         transcation.commit();
57         session.close();
58         sessionFactory.close();
59     }
60 }
测试类

 

posted @ 2016-09-05 15:05  lwx64397  阅读(152)  评论(0编辑  收藏  举报