Hibernate正向工程(实体类-->数据库)

1,新建实体类News.java

 1 package com.hanqi.dao;
 2 
 3 import java.util.Date;
 4 
 5 public class News {
 6 
 7     private Integer id;
 8     private String title;
 9     private String contant;
10     private Date createdate;
11     
12     public Integer getId() {
13         return id;
14     }
15     public void setId(Integer id) {
16         this.id = id;
17     }
18     public String getTitle() {
19         return title;
20     }
21     public void setTitle(String title) {
22         this.title = title;
23     }
24     public String getContant() {
25         return contant;
26     }
27     public void setContant(String contant) {
28         this.contant = contant;
29     }
30     
31     /**
32      * @return the createdate
33      */
34     public Date getCreatedate() {
35         return createdate;
36     }
37     /**
38      * @param createdate the createdate to set
39      */
40     public void setCreatedate(Date createdate) {
41         this.createdate = createdate;
42     }
43     @Override
44     public String toString() {
45         return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]";
46     }
47 }

2,利用hibernate生成实体类对应的配置文件,(右键-新建-其他-hbm.xml)News.hbm.xml

 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-1-12 23:36:52 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping>
 6     <class name="com.hanqi.dao.News" table="NEWS">
 7         <id name="id" type="java.lang.Integer">
 8             <column name="ID" />
 9             <generator class="native" /><!-- 注意此处主键生成方法常用native -->
10         </id>
11         <property name="title" type="java.lang.String">
12             <column name="TITLE" />
13         </property>
14         <property name="contant" type="java.lang.String">
15             <column name="CONTANT" />
16         </property>
17         <property name="createdate" type="java.util.Date">
18             <column name="CREATEDATE" />
19         </property>
20     </class>
21 </hibernate-mapping>

3,将hbm.xml添加到hibernate配置文件hibernate.cfg.xml中

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7     
 8         <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
 9         <!-- 数据库连接 -->
10         <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
11         <property name="hibernate.connection.password">test</property>
12         <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
13         <property name="hibernate.connection.username">test</property>
14         <!-- 用户方案 -->
15         <property name="hibernate.default_schema">TEST</property>
16         <!-- 数据库方言 -->
17         <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
18         <!-- sql语句/调试 -->
19         <property name="hibernate.show_sql">true</property>
20         <property name="hibernate.format_sql">true</property>
21          <!--  自动建表方式 -->
22         <property name="hibernate.hbm2ddl.auto">update</property>
23         
24         <property name="hibernate.search.autoregister_listeners">false</property>
25         <property name="hibernate.validator.apply_to_ddl">false</property>
26         <!-- 映射文件 -->
27         <mapping resource="com/hanqi/dao/News.hbm.xml" />
28         
29     </session-factory>
30 </hibernate-configuration>

 

4,新建JUnit Test Case类TestNews.java进行测试

 1 package com.hanqi.dao;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import java.util.Date;
 6 
 7 import org.hibernate.Session;
 8 import org.hibernate.SessionFactory;
 9 import org.hibernate.Transaction;
10 import org.hibernate.boot.registry.StandardServiceRegistry;
11 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
12 import org.hibernate.cfg.Configuration;
13 import org.junit.Test;
14 import org.hibernate.service.*;
15 
16 public class TestNews {
17 
18     @Test
19     public void test() {
20         System.out.println("测试");
21         //1-构建配置类
22         Configuration cfgrn = new Configuration().configure();
23         
24         //2-构建配置工厂类
25         ServiceRegistry srg = new StandardServiceRegistryBuilder().
26                 applySettings(cfgrn.getProperties()).build();
27         
28         //3-构建会话工厂对象,比较耗资源
29         SessionFactory sf = cfgrn.buildSessionFactory(srg);
30         
31         //4-构建会话对象
32         Session se = sf.openSession();
33         
34         //5-开始事务
35         Transaction tr = se.beginTransaction();
36         
37         //6-执行操作
38 /*        News news = new News();
39         
40         news.setTitle("标题");
41         news.setContant("这是内容");
42         news.setCreatedate(new Date());
43         
44         se.save(news);//插入
45 */        
46         News news2 = (News)se.get(News.class, 1);
47         
48         System.out.println(news2);//查询输出
49         
50         //7-提交事务
51         tr.commit();
52         
53         //8-关闭会话
54         se.close();
55         
56         //9-关闭会话工厂
57         sf.close();
58         
59     }
60 
61 }

 

posted @ 2016-01-13 09:33  dirgo  阅读(710)  评论(0编辑  收藏  举报