Hibernate入門案例(Maven專案)
阿新 • • 發佈:2018-11-06
Hibernate的相關jar包:
案例:
1. 匯入相關的依賴 <dependencies> <!--匯入hibernate需要的依賴--> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.6</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.0.7.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jta_1.1_spec</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> </dependency> <!--mysql連線依賴--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <!--junit單元測試依賴--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> 2. 根據表結構建立domain/pojo下的model類(實體類) * 表結構 : FieldType id int(11) username varchar(32) birthday datetime sex char(1) address varchar(256) * 實體類User public class User { private Integer id; private String username; private Date birthday; private String sex; private String address; 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } 3. 建立model的對映檔案 * UserModel.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'> <hibernate-mapping> <!-- 類與表的對映製作在class元素上 --> <!-- name:全路徑類名 --> <!-- table:表名 --> <class name="com.qin.domain.User" table="user"> <!-- 主鍵的對映製作在id元素上 --> <!-- name:物件中用於作為主鍵的屬性名 --> <!-- colomn:表中主鍵欄位名 --> <!-- 如果name與column值相同,可以省略column --> <id name="id" column="id"> <!-- 將generator元素class屬性設定為"assigned" 手動生成,必須給id --> <generator class="identity" /> <!--**要注意Hibernate主鍵生成策略**--> </id> <!-- 屬性與欄位的對映製作在property元素上 --> <!-- name:類中的屬性名 --> <!-- column:表中的欄位名 --> <!-- 如果name與column值相同,可以省略column --> <property name="username" /> <property name="birthday" /> <property name="sex" /> <property name="address"/> </class> </hibernate-mapping> 4. 配置Hibernate的配置檔案 * hibernate.cfg.xml. 放到classpath下. <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 資料庫連線的配置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/mybatis</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 可選配置 --> <!--是否支援方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--執行CURD時是否列印sql 語句 --> <property name="show_sql">true</property> <!-- 資源註冊 --> <mapping resource="com/qin/domain/UserModel.hbm.xml"/> </session-factory> </hibernate-configuration> 5. 測試 @Test public void hbnTest(){ //1.建立要測試的物件 User user = new User(); user.setUsername("hibernateTest"); user.setSex("男"); user.setBirthday(new Date()); user.setAddress("北京"); //2.載入配置資訊 Configuration conf = new Configuration().configure(); //3.基於配置資訊,建立SessionFactory物件 SessionFactory sessionFactory = conf.buildSessionFactory(); //4.開啟一個與資料庫相關的session物件 Session session = sessionFactory.openSession(); //5.開啟事務,基於session得到 Transaction transaction = session.beginTransaction(); //6.通過session儲存資料 session.save(user); //7.提交事務 transaction.commit(); //8.操作完畢,關閉session連線物件 session.close(); }