Hibernate_01_初體驗

來源:互聯網
上載者:User

標籤:rri   tab   odi   code   localhost   測試   驗證   new   oid   

Hibernate的一個小例子(慕課網教程整理的代碼):

首先在eclipse--help--Eclipse MarketPlace中搜尋“jboss Tool”

匯入hibernate核心jar包、Junit包。

建立一個java類:

 1 package com.imooc.hibernate; 2  3 import java.util.Date; 4  5 public class Students { 6  7     private int sid; 8     private String sname; 9     private String gender;10 private Date birthday;11 private String address;12 13 public Students(int sid, String sname, String gender, Date birthday, String address) {14 super();15 this.sid = sid;16 this.sname = sname;17 this.gender = gender;18 this.birthday = birthday;19 this.address = address;20  }21 22  @Override23 public String toString() {24 return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday25 + ", address=" + address + "]";26  }27 28 public int getSid() {29 return sid;30  }31 32 public void setSid(int sid) {33 this.sid = sid;34  }35 36 public String getSname() {37 return sname;38  }39 40 public void setSname(String sname) {41 this.sname = sname;42  }43 44 public String getGender() {45 return gender;46  }47 48 public void setGender(String gender) {49 this.gender = gender;50  }51 52 public Date getBirthday() {53 return birthday;54  }55 56 public void setBirthday(Date birthday) {57 this.birthday = birthday;58  }59 60 public String getAddress() {61 return address;62  }63 64 public void setAddress(String address) {65 this.address = address;66  }67 }

 點擊項目名,右鍵new--other

產生類的設定檔:

 

 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 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6     <class name="com.imooc.hibernate.Students" table="STUDENTS"> 7         <id name="sid" type="int"> 8             <column name="SID" /> 9             <generator class="assigned" />10         </id>11         <property name="sname" type="java.lang.String">12             <column name="SNAME" />13         </property>14         <property name="gender" type="java.lang.String">15             <column name="GENDER" />16         </property>17         <property name="birthday" type="java.util.Date">18             <column name="BIRTHDAY" />19         </property>20         <property name="address" type="java.lang.String">21             <column name="ADDRESS" />22         </property>23     </class>24 </hibernate-mapping>

 

設定hibernate設定檔:

 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         <property name="connection.username">root</property> 8         <property name="connection.password">root</property> 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>11         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>12         <!-- 是否將sql語句列印到控制台,編碼階段便於測試,建議設為true -->13         <property name="show_sql">true</property>14         <!-- 是否將sql語句進行排版,建議設為true -->15         <property name="format_sql">true</property>16         <!-- 產生表結構的策略。create是如果之前有表結構,先刪除表結構再重建;17             update:原有的基礎上更新;18             create-drop:先刪除表結構再重建19             validate:與原表進行驗證,如果和原表表結構不同,則不進行建立。20             一般使用create和update21          -->22         <property name="hbm2ddl.auto">create</property>23         24         <mapping resource="com/imooc/hibernate/Students.hbm.xml"/>25     </session-factory>26 </hibernate-configuration>

 寫StudentsTest.java檔案:

 1 package com.icoom.test; 2 import static org.junit.Assert.fail; 3  4 import java.util.Date; 5  6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration;10 import org.hibernate.service.ServiceRegistry;11 import org.hibernate.service.ServiceRegistryBuilder;12 import org.junit.After;13 import org.junit.Before;14 import org.junit.Test;15 16 import com.imooc.hibernate.Students;17 18 public class StudentsTest {19     20     private SessionFactory sessionFactory;21     private Session session;22     private Transaction transaction;23 24     @Before25     public void init() {26         // 1.建立設定物件27         Configuration config = new Configuration().configure();28         // 2.建立服務註冊對象29         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();30         // 3.建立會話工廠對象31         sessionFactory = config.buildSessionFactory(serviceRegistry);32         // 會話對象33         session = sessionFactory.openSession();34         // 開啟事務35         transaction = session.beginTransaction();36     }37     @After38     public void destory() {39         transaction.commit();40         session.close();41         sessionFactory.close();42     }43     @Test44     public void testSaveStudents() {45         // 產生學生對象46         Students s = new Students(1, "老張", "男", new Date(), "山東");47         session.save(s);//儲存對象進入資料庫48     }49 50 }

使用Junit執行。

Hibernate_01_初體驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.