一、什么是hibernate
ORM框架/持久层框架
全称为object reference mapping
通过管理对象来改变数据库中的数据
通过管理对象来操作数据库
优势:跨数据库的无缝移植
二、hibernate项目建立的步骤
2.1 添加hibernate相关依赖
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sjy</groupId>
<artifactId>hibernate</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hibernate Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<servlet.version>4.0.0</servlet.version>
<hibernate.version>5.3.0.Final</hibernate.version>
<mysql.driver.version>8.0.19</mysql.driver.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
</dependencies>
<build>
<finalName>hibernate</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2、在resource目录下添加hibernate.cfg.xml(核心配置文件)
2.2.1、添加dtd支持
2.2.2、添加hibernate的配置
2.2.2.1、数据库相关(connection.username|connection.password|connection.url|connection.driver_class|dialect)
2.2.2.2 调试相关(show_sql|format_sql)
<?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>
<!-- 1. 数据库相关 -->
<!-- <property name="connection.username">root</property> -->
<!-- <property name="connection.password">123456</property> -->
<!-- <property name="connection.url">jdbc:mysql://47.100.191.44:3308/lx?useUnicode=true&characterEncoding=UTF-8</property> -->
<!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/www?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> -->
<!-- 配置本地事务(No CurrentSessionContext configured!) -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 2. 调试相关 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 配置映射文件 -->
<mapping resource="com/sjy/entity/User.hbm.xml" />
<!-- 主键生成策略 -->
<!-- <mapping resource="com/zking/two/entity/Student.hbm.xml" /> -->
<!-- <mapping resource="com/zking/two/entity/Worker.hbm.xml" /> -->
<!-- 一对多 -->
<!-- <mapping resource="com/zking/four/entity/Order.hbm.xml" /> -->
<!-- <mapping resource="com/zking/four/entity/OrderItem.hbm.xml" /> -->
<!-- 一对多的自关联 -->
<!-- <mapping resource="com/zking/five/entity/TreeNode.hbm.xml" /> -->
<!-- 多对多 -->
<!-- <mapping resource="com/zking/five/entity/Category.hbm.xml" /> -->
<!-- <mapping resource="com/zking/five/entity/Book.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
2.3 在开发阶段再创建实体类和实体映射文件(*.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="com.sjy.entity.User" table="t_hibernate_user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment" />
</id>
<property name="userName" type="java.lang.String" column="user_name">
</property>
<property name="userPwd" type="java.lang.String" column="user_pwd">
</property>
<property name="realName" type="java.lang.String" column="real_name">
</property>
<property name="sex" type="java.lang.String" column="sex">
</property>
<property name="birthday" type="java.sql.Date" column="birthday">
</property>
<property insert="false" update="false" name="createDatetime"
type="java.sql.Timestamp" column="create_datetime">
</property>
<property name="remark" type="java.lang.String" column="remark">
</property>
</class>
</hibernate-mapping>
小结:hibernate.cfg.xml(1)/*.hbm.xml(N)
实体映射文件一定要加到核心配置文件
*.hbm.xml文件的各属性意思如下:
name:类的全限定名
table:类对应的表
id标签:
name:类属性 type:类属性对应的java类型 column:数据库列段
property标签:
name:类属性 type:类属性对应的java类型 column:数据库列段
可以理解为:id标签是特殊的property标签
编码步骤:
1.对框架核心配置文件进行建模
2.获取sessionFactory工厂
3.获取session会话
4.开启事务(查询不需要)
5.session操作对象
6.提交事务(查询不需要事务)
7.关闭session
查询
package com.sjy.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ListDemo {
public static void main(String[] args) {
Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
List list = session.createQuery("from User").list();
for (Object object : list) {
System.out.println(object);
}
session.close();
}
}
增加
package com.sjy.test;
import java.sql.Date;
import java.sql.Timestamp;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.sjy.entity.User;
public class AddDemo {
public static void main(String[] args) {
Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user=new User();
user.setBirthday(new Date(System.currentTimeMillis()));
user.setId(88);
user.setSex("男");
user.setUserPwd("111222");
user.setcreateDatetime(new Timestamp(System.currentTimeMillis()));
user.setRealName("sjy");
user.setUserName("aaaaa");
session.save(user);
transaction.commit();
session.close();
}
}
增加时id设置的是88,但是插入数据库的数据是15
原因是generator标签的class为increment (自增)
不自增可改为assigned
主键生成器要求
assigned:数据类型不限、保存前必须赋值
identity:数字,无需赋值
sequence:数字,无需赋值,默认使用hibernate_sequence这个序列
也可通过sequence/sequence_name参数赋值
increment:数字,无需赋值
修改
package com.sjy.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.sjy.entity.User;
public class EditDemo {
public static void main(String[] args) {
Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user = session.get(User.class, 7);
user.setUserName("牛马");
System.out.println(user);
transaction.commit();
session.close();
}
}
数据库:
删除
删除有两种方法
方法一:
User user = session.get(User.class, 7);
session.delete(user);
方法二:
User user=new User();
user.setId(6);
session.delete(user);