標籤:
1、工程目錄結構如下
2、引入需要的jar包,如。
3、建立持久化類User對應資料庫中的user表
package com.hibernate.設定檔.pojo;import java.sql.Date;public class User { private Integer id; private String username; private String password; private Date update_time; public User() { super(); } public User( String username, String password, Date update_time) { super(); this.username = username; this.password = password; this.update_time = update_time; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getUpdate_time() { return update_time; } public void setUpdate_time(Date update_time) { this.update_time = update_time; } }View Code
4、建立持久化類對應的設定檔User.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.hibernate.設定檔.pojo"> <class name="User" table="user" dynamic-insert="true"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <!-- 指定主鍵的產生方式, native: 使用資料庫本地方式 --> <generator class="native" /> </id> <property name="username" type="java.lang.String" column="username"/> <property name="password" type="java.lang.String" column="password"/> <property name="update_time" type="date"> <column name="update_time" /> </property> </class></hibernate-mapping>
View Code
5、建立hibernate的設定檔hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 配置串連資料庫的基本資料 --> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property> <!-- 配置hibernate的基本資料 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 執行操作時是否在控制台列印 SQL --> <property name="show_sql">true</property> <!-- 是否對 SQL 進行格式化 --> <property name="format_sql">true</property> <!-- 指定自動產生資料表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定關聯的.hbm.xml檔案 --> <mapping resource="com/hibernate/設定檔/pojo/User.hbm.xml"/> </session-factory></hibernate-configuration>
View Code
6、建立SessionFactoryUtil,單例模式,用於產生SessionFactory對象
package com.hibernate.設定檔.util;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class SessionFactoryUtil { private SessionFactoryUtil(){} private static SessionFactory sessionFactory = null; public static SessionFactory getSessionFactory() { if(sessionFactory == null){ /** * 1、 Configuration 類負責管理 Hibernate 的配置資訊。包括如下內容: Hibernate * 啟動並執行底層資訊:資料庫的URL、使用者名稱、密碼、JDBC驅動類,資料庫Dialect,資料庫連接池等(對應 * hibernate.cfg.xml 檔案)。 持久化類與資料表的映射關係(*.hbm.xml 檔案) */ Configuration configeration = new Configuration().configure(); /** * 2、 建立一個 ServiceRegistry 對象: hibernate 4.x 新添加的對象 hibernate * 的任何配置和服務都需要在該對象中註冊後才能有效. */ ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configeration.getProperties()) .buildServiceRegistry(); /** * 3、建立SessionFactory對象 */ sessionFactory = configeration.buildSessionFactory(serviceRegistry); } return sessionFactory; }}View Code
7、測試代碼
package com.hibernate.設定檔.service.impl;import java.sql.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import com.hibernate.設定檔.pojo.User;import com.hibernate.設定檔.service.HibernateService;import com.hibernate.設定檔.util.SessionFactoryUtil;public class HibernateServiceImpl implements HibernateService { public void hibernateTest(){ //1、建立SessionFactory對象 SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory(); //2、建立session Session session = sessionFactory.openSession(); //3、開啟事務 Transaction tran = session.beginTransaction(); //4、執行持久化操作 User user = new User("lvyf","123456",new Date(new java.util.Date().getTime())); session.save(user); //5、提交事務 tran.commit(); //6、關閉session session.close(); //7、關閉sessionFactory sessionFactory.close(); } public static void main(String[] args) { HibernateServiceImpl hs = new HibernateServiceImpl(); hs.hibernateTest(); }}
hibernate操作資料庫例子