一.什么是hibernate?
ORM框架/持久层框架 jdbc的一个框架
object reference mapping 对象关系映射
通过管理对象来改变数据库中的数据
通过管理对象来操作数据库
优势:跨数据库的无缝移植
二. 如何在项目中添加hibernate支持(手动添加)
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.zy</groupId>
<artifactId>maven-3</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-3 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>maven-3</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.在resource目录下添加hibernate.cfg.xml(核心配置文件)
①添加DTD支持
②添加Hibernate的配置
③ 数据库相关
④ 调试相关(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. 数据库相关 5.7-->
<!-- <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> -->
<!-- 1. 数据库相关 8.0-->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/mybatis_ssm?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/zy/one/entity/User.hbm.xml" />
<!-- 主键生成策略 -->
<!-- <mapping resource="com/zy/two/entity/Student.hbm.xml" /> -->
<!-- <mapping resource="com/zy/two/entity/Worker.hbm.xml" /> -->
<!-- 一对多 -->
<mapping resource="com/zy/two/entity/Order.hbm.xml" />
<mapping resource="com/zy/two/entity/OrderItem.hbm.xml" />
<!-- 一对多的自关联 -->
<!-- <mapping resource="com/zy/five/entity/TreeNode.hbm.xml" /> -->
<!-- 多对多 -->
<!-- <mapping resource="com/zy/five/entity/Category.hbm.xml" /> -->
<!-- <mapping resource="com/zy/five/entity/Book.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
3.开发阶段再创建实体类和实体映射文件(user.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>
<!--
name:类的全限定名
table:类对应的表
id标签:
name:类属性 type:类属性对应的java类型 column:数据库类段
property标签:
name:类属性 type:类属性对应的java类型 column:数据库类段
-->
<class name="com.zy.one.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>
三.编码7步骤:
1.对框架核心配置文件进行建模
2.获取sessionFactory工厂
3.获取session会话
4.开启事务(查询不需要)
5.session操作对象
6.提交事务(查询不需要事务)
7.关闭session
例1:查询
package com.zy.one.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class QueryDemo {
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();
}
}
例2:增加
package com.zy.one.test;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zy.one.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.setCreateDatetime(new Timestamp(System.currentTimeMillis()));
user.setId(56);
user.setRealName("哈哈");
user.setRemark("武侠剧");
user.setSex("4");
user.setUserName("大口大口");
user.setUserPwd("520");
session.save(user);
transaction.commit();
session.close();
}
}
②.数据库数据
我增加的id是56而数据库的id是17的原因是:
2.hibernate的主键生成器:
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识 。
①程序员自己控制:assigned(数据类型不限、保存前必须赋值)
② 数据库控制: identity(标识列/自动增长 数字,无需赋值)
sequence( 数字,无需赋值, 默认使hibernate_sequence这个序列,
也可以通过sequence/sequence_name参数赋值 )
③hibernate控制:increment uuid/uuid.hex(是由容器自动生成的一个32位的字符串,.hex代表的 是十六进制) 32位的字符串,无需赋值,
④ 其它:native(等于identity+sequence)
⑤increment( 数字,无需赋值)
3.可以去user.hbm.xml中<generator class="increment" />改为<generator class="assigned" />
例3:修改
package com.zy.one.test;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zy.one.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, 17);
user.setUserName("影子");
transaction.commit();
session.close();
}
}
②.数据库数据
例4:删除
package com.zy.one.test;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zy.one.entity.User;
public class DelDemo {
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, 16);
session.delete(user);
transaction.commit();
session.close();
}
}
②方法二
User user=new User();
user.setId(15);
session.delete(user);