下面 过一遍 Hibernate
这个只大致过一遍
Hibernate 和Mybatis 都为ORM层框架
数据库连接:由 Hibernate 支持的一个或多个配置文件处理。这些文件是 hibernate.properties 和 hibernate.cfg.xml。
类映射设置:这个组件创造了 Java 类和数据库表格之间的联系。
配置对象用于创建 SessionFactory对象 使用配置文件为应哟配置 Hibernate 实例化会话对象 SessionFactory 线程安全 为应用程序所使用
与数据库的物理连接对象,session青量级的,非线程安全
数据库的事物
执行 hql查询
对对象进行检索
选择maven进行下载
https://bintray.com/hibernate/artifacts/hibernate-orm
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-agroal</artifactId> <version>5.3.10.Final</version> <type>pom</type> </dependency>
maven 填入 mysql 驱动
https://search.maven.org/artifact/mysql/mysql-connector-java/8.0.16/jar<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency>
进行如下配置
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <!-- Assume test is the database name --> <property name="hibernate.connection.url">jdbc:mysql://47.94.95.84:3453/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1111</property> <!-- List of XML mapping files --> <mapping resource="Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
目前目录结构如下
会话三态
瞬时状态: 没有与session关联,数据库中没有相关记录
持久状态:对数据库表进行持久化保存
脱管状态:关闭会话,将会进入托管状态
package com.ming.pojo; /** * 新闻对象 * @author ming */ public class News { // 新闻类属性 private Integer id; // 新闻标题 private String title; // 新闻内容 private String content; public void setId(Integer id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setContent(String content) { this.content = content; } public Integer getId() { return id; } public String getTitle() { return title; } public String getContent() { return content; } }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.ming.pojo.Employee" table="EMPLOYEE"> <meta attribute="class-description"> This class contains the employee detail. </meta> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="firstName" column="first_name" type="string"/> <property name="lastName" column="last_name" type="string"/> <property name="salary" column="salary" type="int"/> </class> </hibernate-mapping>
package com.ming; import com.ming.pojo.Employee; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import java.util.Iterator; import java.util.List; /** * @author ming */ public class ManageEmployee { private SessionFactory factory; public ManageEmployee(SessionFactory sessionFactory){ factory = sessionFactory; } public Integer addEmployee(String fname, String lname, int salary){ // 开启一个会话 Session session = factory.openSession(); // 事物 Transaction tx = null; Integer employeeID = null; try{ // 获得一个事物 tx = session.beginTransaction(); // 生成雇员对象 Employee employee = new Employee(fname, lname, salary); // 进行保存操作 回填主键 employeeID = (Integer) session.save(employee); // 提交事物 tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } return employeeID; } /* Method to READ all the employees */ public void listEmployees( ){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); // 使用查询语句,,创建出一个查询 Query 来自于表Employee 返回列表 List employees = session.createQuery("FROM Employee").list(); // 进行迭代操作 for (Iterator iterator = employees.iterator(); iterator.hasNext();){ Employee employee = (Employee) iterator.next(); System.out.print("First Name: " + employee.getFirstName()); System.out.print(" Last Name: " + employee.getLastName()); System.out.println(" Salary: " + employee.getSalary()); } // 提交事物 tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } /* Method to UPDATE salary for an employee */ public void updateEmployee(Integer EmployeeID, int salary ){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); // 根据class获得一个雇员对象 此时进入持久态 Employee employee = (Employee)session.get(Employee.class, EmployeeID); // 进行设置 此时为持久态,数据库会进行保存操作 employee.setSalary( salary ); // 进行更新 写入数据库 session.update(employee); // 提交事物以后,将会关闭会话, 进入托管状态 tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } /* Method to DELETE an employee from the records */ public void deleteEmployee(Integer EmployeeID){ Session session = factory.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); Employee employee = (Employee)session.get(Employee.class, EmployeeID); session.delete(employee); tx.commit(); }catch (HibernateException e) { if (tx!=null) tx.rollback(); e.printStackTrace(); }finally { session.close(); } } }
<%@ page import="org.hibernate.cfg.Configuration" %> <%@ page import="com.ming.ManageEmployee" %> <html> <body> <h2>Hello World!</h2> <% ManageEmployee manageEmployee = null; try{ manageEmployee = new ManageEmployee(new Configuration().configure().buildSessionFactory()); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } ManageEmployee ME = manageEmployee; /* Add few employee records in database */ Integer empID1 = ME.addEmployee("Zara", "Ali", 1000); Integer empID2 = ME.addEmployee("Daisy", "Das", 5000); Integer empID3 = ME.addEmployee("John", "Paul", 10000); /* List down all the employees */ ME.listEmployees(); /* Update employee's records */ ME.updateEmployee(empID1, 5000); /* Delete an employee from the database */ ME.deleteEmployee(empID2); /* List down new list of the employees */ ME.listEmployees(); %> </body> </html>
类sql语句 只要会sql hql就会的
剩下的大概也没有什么。。。。。
基本上已经学过。。。
开工java中最核心的部分 spring! 控制依赖 控制依赖 即 IOC AOP
为什么拖如此之久 因为 最近发烧了。。。。病了