一.Hibernate概念:
定义:是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装。可以通过操作实体类对象来进行操作数据库。
优势:跨越数据库的无缝衔接
二.Hibernate框架的使用:
第一步:添加hibernate jar包依赖
<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.pjl</groupId>
<artifactId>hib</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hib Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<junit.version>4.12</junit.version>
<servlet.version>4.0.0</servlet.version>
<hibernate.version>5.3.0.Final</hibernate.version>
<mysql.version>8.0.19</mysql.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.version}</version>
</dependency>
</dependencies>
<build>
<finalName>hib</finalName>
</build>
</project>
第二步:添加hibernate.cfg.xml配置文件
引入hibernate-configuration-3.0.dtd约束
Hibernate.cfg.xml:
<?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>
<!--数据库相关 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> -->
<!--数据库相关 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/pjl/one/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>
第三步:User.hbm.xml:(实体类映射文件)
首先建一个User实体类
package com.pjl.one.entity;
import java.sql.Date;
import java.sql.Timestamp;
public class User {
private int id;
private String userName;
private String userPwd;
private String realName;
private String sex;
private Date birthday;
private Timestamp createDatetime;
private String remark;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Timestamp getCreateDatetime() {
return createDatetime;
}
public void setCreateDatetime(Timestamp createDatetime) {
this.createDatetime = createDatetime;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public User(int id, String userName, String userPwd, String realName, String sex, Date birthday,
Timestamp createDatetime, String remark) {
super();
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.realName = realName;
this.sex = sex;
this.birthday = birthday;
this.createDatetime = createDatetime;
this.remark = remark;
}
public User() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName + ", sex="
+ sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark=" + remark + "]";
}
}
-
name 对象属性
-
type 属性类别
-
column 表列段
-
generator 主键生成格式
<generator class="assigned" />//程序员自己控制
<?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.pjl.one.entity.User" table="t_hibernate_user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="assigned" />
</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的crud:
使用hibernate框架进行操作的七大步骤:
1.对框架核心配置文件进行建模
2.获取sessionFctoty工厂
3、获取session会话
4、开启事务(查询不用事务)
5、session操作对象
6、提交事务(查询不用事务)
7、关闭session
第一步:增加
package com.pjl.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.pjl.one.entity.User;
public class AddDemo {
public static void main(String[] args) {
/**
* 1.对框架核心配置文件进行建模
* 2.获取sessionFactory工厂
* 3.获取session会话
* 4.开启事务(查询不需要事务)
* 5.session操作对象
* 6.提交事务(查询不需要事务)
* 7.关闭session
*/
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(58);
user.setRealName("kk");
user.setRemark("jj");
user.setSex("nv");
user.setUserName("oooo");
user.setUserPwd("123456");
System.out.println(user);
transaction.commit();
session.close();
}
}
第二步:删除
package com.pjl.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.pjl.one.entity.User;
public class DelDemo {
public static void main(String[] args) {
/**
* 1.对框架核心配置文件进行建模
* 2.获取sessionFactory工厂
* 3.获取session会话
* 4.开启事务(查询不需要事务)
* 5.session操作对象
* 6.提交事务(查询不需要事务)
* 7.关闭session
*/
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,58);
session.delete(user);*/
//第二种:new一个对象,删掉
User user=new User();
user.setId(34);
session.delete(user);
transaction.commit();
session.close();
}
}
第三步:修改
package com.pjl.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.pjl.one.entity.User;
public class EditDemo {
public static void main(String[] args) {
/**
* 1.对框架核心配置文件进行建模
* 2.获取sessionFactory工厂
* 3.获取session会话
* 4.开启事务(查询不需要事务)
* 5.session操作对象
* 6.提交事务(查询不需要事务)
* 7.关闭session
*/
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,14);
user.setRealName("hhhh");
transaction.commit();
session.close();
}
}
第四步:查询
package com.pjl.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) {
/**
* 1.对框架核心配置文件进行建模
* 2.获取sessionFactory工厂
* 3.获取session会话
* 4.开启事务(查询不需要事务)
* 5.session操作对象
* 6.提交事务(查询不需要事务)
* 7.关闭session
*/
Configuration configure=new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory= configure.buildSessionFactory();
Session session =sessionFactory.openSession();
List list = session.createQuery("form User").list();
for (Object object : list) {
System.out.println(object);
}
session.close();
}
}
展示效果: