hibernate入门
生活随笔
收集整理的這篇文章主要介紹了
hibernate入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
什么是hibernate?
Hibernate是一個開放源代碼的對象關系映射框架,它對JDBC進行了非常輕量級的對象封裝,使得java程序員可以隨心所欲的使用對象編程思維來操縱數據庫。 Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程序使用,也可以在Servlet/JSP的Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成數據持久化的重任。
ORM框架/持久層框架;
ORM(Object Relational Mapping):對象關系映射。
對象與關系型數據間之間的映射管理框架
優勢:跨數據庫的無縫移植(SqlServer、Oracle、MySql);
首先配置文件:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.ing</groupId> 5 <artifactId>T_hibernate</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>T_hibernate Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <maven.compiler.source>1.8</maven.compiler.source> 14 <maven.compiler.target>1.8</maven.compiler.target> 15 <junit.version>4.12</junit.version> 16 <servlet.version>4.0.0</servlet.version> 17 18 <!-- 版本5.1.0 --> 19 <hibernate.version>5.3.0.Final</hibernate.version> 20 <mysql.driver.version>5.1.46</mysql.driver.version> 21 </properties> 22 <dependencies> 23 <dependency> 24 <groupId>junit</groupId> 25 <artifactId>junit</artifactId> 26 <version>${junit.version}</version> 27 <scope>test</scope> 28 </dependency> 29 30 <dependency> 31 <groupId>javax.servlet</groupId> 32 <artifactId>javax.servlet-api</artifactId> 33 <version>${servlet.version}</version> 34 <scope>provided</scope> 35 </dependency> 36 37 <dependency> 38 <groupId>org.hibernate</groupId> 39 <artifactId>hibernate-core</artifactId> 40 <version>${hibernate.version}</version> 41 </dependency> 42 43 <dependency> 44 <groupId>mysql</groupId> 45 <artifactId>mysql-connector-java</artifactId> 46 <version>${mysql.driver.version}</version> 47 </dependency> 48 </dependencies> 49 <build> 50 <finalName>T_hibernate</finalName> 51 52 <plugins> 53 <plugin> 54 <groupId>org.apache.maven.plugins</groupId> 55 <artifactId>maven-compiler-plugin</artifactId> 56 <version>3.7.0</version> 57 <configuration> 58 <source>${maven.compiler.source}</source> 59 <target>${maven.compiler.target}</target> 60 <encoding>${project.build.sourceEncoding}</encoding> 61 </configuration> 62 </plugin> 63 </plugins> 64 </build> 65 </project>添加hibernate(5.2.12.Final)支持(手動添加)
1 添加hibernate相關依賴
2 在resource目錄下添加hibernate.cfg.xml(核心配置文件)
1 <hibernate-configuration> 2 <session-factory> 3 <!-- 1.數據庫相關 --> 4 <!-- connection.username|connection.password|connection.url|connection.driver_class|dialect --> 5 <!-- 數據庫賬號 --> 6 <property name="connection.username">root</property> 7 <!-- 數據庫密碼 --> 8 <property name="connection.password">123</property> 9 <!-- 數據庫連接URL --> 10 <!-- 注意特殊字符轉義 --> 11 <property name="connection.url"><!-- 注意字符轉換--!> 12 jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=UTF-8 13 </property> 14 <!--數據庫連接驅動 --> 15 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 16 <!-- 數據庫方言 --> 17 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 18 <!-- 配置本地事務(No CurrentSessionContext configured!) --> 19 <property name="hibernate.current_session_context_class">thread</property> 20 21 <!-- 2. 調試相關 --> 22 <property name="show_sql">true</property> 23 <property name="format_sql">true</property> 24 25 <!-- 3. 添加實體映射文件 --> 26 <mapping resource="entity/Book.hbm.xml" /> 27 </session-factory> 28 </hibernate-configuration>
3.在開發階段再創建實體類和實體映射文件
創建實體類
1 public class Book implements Serializable { 2 private Integer id; 3 private String bookname; 4 private String price; 5 private String booktype; 6 private String bookimage; 7 8 public Integer getId() { 9 return id; 10 } 11 public void setId(Integer id) { 12 this.id = id; 13 } 14 public String getBookname() { 15 return bookname; 16 } 17 public void setBookname(String bookname) { 18 this.bookname = bookname; 19 } 20 21 22 public String getPrice() { 23 return price; 24 } 25 public void setPrice(String price) { 26 this.price = price; 27 } 28 public String getBooktype() { 29 return booktype; 30 } 31 public void setBooktype(String booktype) { 32 this.booktype = booktype; 33 } 34 public String getBookimage() { 35 return bookimage; 36 } 37 public void setBookimage(String bookimage) { 38 this.bookimage = bookimage; 39 } 40 public Book() { 41 super(); 42 // TODO Auto-generated constructor stub 43 } 44 45 46 public Book(Integer id, String bookname, String price, String booktype, String bookimage) { 47 super(); 48 this.id = id; 49 this.bookname = bookname; 50 this.price = price; 51 this.booktype = booktype; 52 this.bookimage = bookimage; 53 } 54 @Override 55 public String toString() { 56 return "Book [id=" + id + ", bookname=" + bookname + ", price=" + price + ", booktype=" + booktype 57 + ", bookimage=" + bookimage + "]"; 58 } 59 60 }實體映射文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="entity.Book" table="t_book3"> 7 <id name="id" type="java.lang.Integer" column="book_id"> 8 <generator class="increment" /> 9 </id> 10 <property name="bookname" type="java.lang.String" column="book_name"> 11 </property> 12 <property name="price" type="java.lang.String" column="book_price"> 13 </property> 14 <property name="booktype" type="java.lang.String" column="book_category_id"> 15 </property> 16 <property name="bookimage" type="java.lang.String" column="book_image"> 17 </property> 18 </class> 19 20 </hibernate-mapping>1 CRUD操作步驟:
1 讀取配置
2 創建SessionFactory
3 打開Session
4 開啟事務
5 CURD
6 提交事務/回滾事務
7 關閉Session
完成;
轉載于:https://www.cnblogs.com/AluoKa/p/11181980.html
總結
以上是生活随笔為你收集整理的hibernate入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: R语言包在linux上的安装等知识
- 下一篇: java BlockingQueue 用