天天看點

Hibernate5.1.0的hello word

建立一個JavaProject(不一定非要web工程)

在工程裡面建立一個檔案夾lib,用來存放jar包

Hibernate5.1.0的hello word
找到Hibernate的下載下傳檔案,解壓後找到required檔案夾,這是需要的jar包
Hibernate5.1.0的hello word

添加到咱們建好的lib檔案夾裡面

Hibernate下載下傳位址http://hibernate.org/orm/downloads/

還需要的就是jdbc驅動包    mysql-connector-java-5.1.37-bin.jar

下載下傳位址http://www.mysql.com/products/connector/

Hibernate5.1.0的hello word
Hibernate5.1.0的hello word
Hibernate5.1.0的hello word
Hibernate5.1.0的hello word
現在寫一個Java POJO類,也就是簡單的getter,setter類啦
Hibernate5.1.0的hello word
Hibernate5.1.0的hello word
然後在Java類所在的包建立一個XXX.hbm.xml檔案(這個需要Hibernate插件的支援,關于插件我有一篇有介紹)
Hibernate5.1.0的hello word
Hibernate5.1.0的hello word
再在src檔案夾裡建一個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         <!-- 配置連接配接資料庫的基本資訊 -->
 9         <property name="connection.username">資料庫使用者名</property>
10         <property name="connection.password">資料庫密碼</property>
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/資料庫名</property>
13         
14         <!-- 配置Hibernate基本資訊 -->
15         <!-- Hibernate所使用得資料庫方言 -->
16         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
17         
18         <!-- 執行操作時是否在控制台列印sql -->
19         <property name="show_sql">true</property>
20         
21         <!-- 是否對sql進行格式化 -->
22         <property name="format_sql">true</property>
23         
24         <!-- 指定自動生成資料表的政策 -->
25         <property name="hbm2ddl.auto">update</property>
26         
27         <!-- 指定關聯的.hbm.xml檔案 -->
28         <mapping resource="com/biubiu/domain/UserInfo.hbm.xml"/>
29         <mapping class="com.biubiu.domain.UserInfo"/>
30         
31     </session-factory>
32 </hibernate-configuration>      

最後寫一個測試類,測試方法為:

1   @Test
 2     public void test() {
 3         //1.建立一個SessionFactory對象
 4         SessionFactory seFactory = null;
 5         final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
 6                 .configure() // configures settings from hibernate.cfg.xml
 7                 .build();
 8         try{
 9             seFactory = new MetadataSources( registry ).buildMetadata()
10                        .buildSessionFactory();
11         } catch(Exception e){
12             StandardServiceRegistryBuilder.destroy( registry );
13         }
14         
15         
16         //2.建立一個Session對象
17         
18         Session session = seFactory.openSession();
19         
20         //3.開啟事務
21         session.beginTransaction();
22         //4.執行操作
23         
24         session.save(new UserInfo("[email protected]","測試",
25                 "icuicu","0","img","tianmao"));
26         
27         /*
28         //!!!!寫hql語句,from 類名(區分大小寫)
29         String hql = "from UserInfo";
30         @SuppressWarnings("unchecked")
31         List<UserInfo> list = session.createQuery(hql).list();
32         for(UserInfo u : list){
33             System.out.println(u);
34         }
35         */
36         //5.送出事務
37         session.getTransaction().commit();
38         //6.關閉Session
39         session.close();
40         //7.關閉SessionFactory
41         seFactory.close();
42         
43     }      

一個是存資料,另外一個是取資料,注意注釋起來了

小LUA

面對敵人的嚴刑逼供,我一個字也沒說,而是一五一十寫了下來。