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
1 //1.读取配置 加载hibernate核心配置文件 2 Configuration config=new Configuration(); 3 config.configure("/hibernate.cfg.xml"); 4 //2.创建sessionFactory 5 SessionFactory sessionFactory=config.buildSessionFactory(); 6 //3.打开session 7 //注:次session与jsp/serlvet中的session要区别对待 8 Session session=sessionFactory.openSession(); 9 //4.开启事务 10 Transaction trans = session.beginTransaction(); 11 //5.CRUD 12 //5.1 save 13 Book book=new Book(); 14 // book.setBookname("降龙十八掌"); 15 // book.setPrice("9999"); 16 // book.setBookimage("img/555"); 17 // session.save(book); 18 //5.2 get (查单个) 19 // book.setId(2); 20 // Book b=session.get(Book.class, book.getId()); 21 // System.out.println(b); 22 //5.3 delete(先查再删除) 23 // book.setId(2); 24 // Book b=session.get(Book.class, book.getId()); 25 // if(null!=b) { 26 // session.delete(b);//比如id=99不存在,直接删除就会报错 27 // } 28 //5.4 update(先查再修改) 29 book.setId(3); 30 Book b=session.get(Book.class, book.getId()); 31 if(null!=b) { 32 b.setBookname("999"); 33 b.setBooktype("9999"); 34 session.update(b); 35 } 36 //6.提交或者回滚事务 37 trans.commit(); 38 //7.关闭session 39 session.close(); 40 41 } 42 43 }
完成;