hibernate 学习笔记1
今天開始學習hibernate,碰到的幾個問題在這里記錄一下。
1>導入hibernate5.2.10的hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required 文件夾下的jar包,及mysql數據庫驅動。
?
2>配置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://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=UTF8</property> 10 <property name="connection.username">root</property> 11 <property name="connection.password"></property> 12 <!-- 每次從數據庫中取出并放到JDBC的Statement中的記錄條數。Fetch Size設的越大,讀數據庫的次數越少,速度越快,Fetch 13 Size越小,讀數據庫的次數越多,速度越慢 --> 14 <property name="jdbc.fetch_size">50 </property> 15 <!--批量插入,刪除和更新時每次操作的記錄數。Batch Size越大,批量操作的向數據庫發送Sql的次數越少,速度就越快,同樣耗用內存就越大 --> 16 <property name="jdbc.batch_size">23 </property> 17 <!-- SQL 方言--> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 <!-- Enable Hibernate's automatic session context management --> 20 <property name="current_session_context_class">thread</property> 21 <!-- 在控制臺輸出sql語句 --> 22 <property name="show_sql">true</property> 23 <!-- 在啟動時根據配置更新數據庫 --> 24 <property name="hbm2ddl.auto">update</property> 25 <mapping resource="com/cnblogs/hibernate_first/User.hbm.xml" /><!-- 注冊我們的實體映射類 --> 26 </session-factory> 27 </hibernate-configuration>以上配置需要注意幾個問題:
a建立數據庫的時候一定要指定數據庫編碼,如:create database hibernate_first DEFAULT CHARACTER SET UTF8及數據庫url:
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=UTF8</property> 否則可能產生持久化的時候出現的亂碼情況 如:b?<mapping resource="com/cnblogs/hibernate_first/User.hbm.xml" /><!-- 注冊我們的實體映射類 --> 注冊實體映射類注意帶包名,其中“.”用“/”代替。
c如果報錯前言中不允許有內容,可能是在xml文件的開頭有不必要的空格,記得檢查。
備注:因為是剛剛學習hibernate 配置中的很多property 可以查閱相關資料,以上配置沒有過多列出。
3>建立實體類及配置映射文件,因為是剛剛學習,所以配置的很簡單,后續進一步學習將進行整改。
package com.cnblogs.hibernate_first;import java.util.Date;public class User {private String id;private String name;private String password;private Date createDate;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getCreateDate() {return createDate;}public void setCreateDate(Date createDate) {this.createDate = createDate;}} <?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 package="com.cnblogs.hibernate_first"><class name="User" table="user"><id name="id" column="id"><generator class="uuid"></generator></id><property name="name" column="name" /><property name="password" column="password" /><property name="createDate" column="createDate" /></class> </hibernate-mapping>以上配置需要注意以下問題:
a?<hibernate-mapping package="com.cnblogs.hibernate_first"> 記得指定包名。要不報錯找不到映射文件。
4>測試
1 public class Test1 { 2 public static void main(String[] args) { 3 //讀取配置文件 4 Configuration cfg = new Configuration().configure(); 5 //建立SessionFactory 6 SessionFactory factory = cfg.buildSessionFactory(); 7 Session session = null; 8 Transaction transation = null; 9 try { 10 //打開Session 11 session = factory.openSession(); 12 //開啟事務 13 transation = session.beginTransaction(); 14 User user = new User(); 15 user.setName("王五"); 16 user.setPassword("123"); 17 user.setCreateDate(new Date()); 18 session.save(user); 19 //提交事務 20 transation.commit(); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 //回滾 24 transation.rollback(); 25 }finally{ 26 //關閉Session 27 if(session != null){ 28 if(session.isOpen()){ 29 session.close(); 30 } 31 } 32 } 33 } 34 }提交結果如下圖:
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/Juli/p/7685573.html
總結
以上是生活随笔為你收集整理的hibernate 学习笔记1的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Spring Cloud基础教程
 - 下一篇: Shell--cut用法