- Intellj 1.4
- JDBC4
- Hibernate jar (少其中一個可能都會出錯)
內容參考至:
Step1.檔案結構目錄
Step2.設置hibernate.cfg.xml
Step3.設置User.hbm.xml
- <hibernate-configuration>
- <session-factory>
- <!-- 顯示實際操作資料庫時的SQL -->
- <property name="show_sql">true</property>
- <!-- 將顯示的SQL排版,方便觀看 -->
- <property name="format_sql">true</property>
- <!-- SQL方言,這邊設定的是SQLServer -->
- <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
- <!-- JDBC驅動程式 -->
- <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
- <!-- JDBC URL -->
- <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb</property>
- <!-- 資料庫使用者 -->
- <property name="connection.username">sa</property>
- <!-- 資料庫密碼 -->
- <property name="connection.password">578989</property>
- <!-- 物件與資料庫表格映射文件 -->
- <mapping resource="User.hbm.xml">
- </mapping></session-factory>
- </hibernate-configuration>
Step3.設置User.hbm.xml
-重點注意事項
1.資料庫每個欄位都要記得設定到<property>
2.其中id是個特殊的屬性,Hibernate會使用它來作為主鍵識別,您可以定義主鍵產生的方式,這是在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屬性為所映射的物件,而table為所映射的表格;-->
- <class name="Hibernate.UserInformationEntity" table="user_information">
- <id name="id" column="id"> <!--<id>中 column屬性指定了表格欄位,而 type屬性指定了User實例的中的id之型態。-->
- <generator class="native"/> <!--<id>中主鍵的產生方式在這邊設定為"native",表示主鍵的生成方式由Hibernate根據資料庫Dialect 的定義來決定-->
- <!--<generator class="identity"/>-->
- <!--identity-->
- </id>
- <!--<property>標籤中的column與type都各自指明了表格中欄位與物件中屬性的對應。-->
- <property name="account" column="account"/>
- <property name="name" column="name"/>
- <property name="age" column="age"/>
- <property name="create_time" column="create_time"/>
- </class>
- </hibernate-mapping>
Step4.建立實體,與表格對應
-重點注意事項
1.資料庫欄位『型態是甚麼』,實體就要設一樣,否則會出錯
ex.資料庫:age int not null 實體:int age;
-重點注意事項
1.也可直接寫在main class之中,一個應用程式當中通常只需要一個SessionFactory實例, 為了方便整個應用取得同一個SessionFactory實例, 可以撰寫一個HibernateUtil類別
2.要如何寫在main class可參考: http://blog.csdn.net/zs234/article/details/9108533
Sept5.設定SessionFactory實例,連線用
public class UserInformationEntity { int id =0; String account; String name; int age = 0; Date create_time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreate_time() { return create_time; } public void setCreate_time(Date create_time) { this.create_time = create_time; } }
-重點注意事項
1.也可直接寫在main class之中,一個應用程式當中通常只需要一個SessionFactory實例, 為了方便整個應用取得同一個SessionFactory實例, 可以撰寫一個HibernateUtil類別
2.要如何寫在main class可參考: http://blog.csdn.net/zs234/article/details/9108533
Sept6.馬上來試用Hibernate吧
- package Hibernate;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- /**
- * Created by ytchen on 2016/7/22.
- */
- public class HibernateUtil {
- private static SessionFactory sessionFactory;
- static {
- try {
- sessionFactory = new Configuration().configure()
- .buildSessionFactory();
- } catch (Throwable ex) {
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public static void shutdown() {
- getSessionFactory().close();
- }
- package Hibernate;
- import org.hibernate.Transaction;
- import org.hibernate.Session;
- import java.util.Date;
- /**
- * Created by ytchen on 2016/7/22.
- */
- public class HibernateDemo {
- public static void main(String[] args) {
- UserInformationEntity user = new UserInformationEntity();
- user.setAccount("Ian");
- user.setName("Ian");
- user.setAge(20);
- user.setCreate_time(new Date());
- // 開啟Session,相當於開啟JDBC的Connection
- Session session = HibernateUtil.getSessionFactory().openSession();
- // Transaction表示一組會話操作
- Transaction tx= session.beginTransaction();
- // 將物件映射至資料庫表格中儲存
- session.save(user);
- tx.commit();
- session.close();
- System.out.println("新增資料OK!請用Sqlserver觀看結果!");
- HibernateUtil.shutdown();
- }
- }