一、hibernate简介
(可随意切换数据库)
ORM框架/持久层框架? ? ? ? jdbc的一个框架
object reference mapping? ? ? ? 对象映射关系
通过管理对象来改变数据库中的数据
通过管理对象来操作数据库
优势:跨数据库的无缝移植(无需写数据库,底层生成)
二、hibernate使用
1.新建一个maven项目
2.导入hibernate依赖
<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>zw.com</groupId>
<artifactId>zw_hibernate</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>zw_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>zw_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>
在pom.xml中导jar包依赖时,可能没下载完全,就可以按以下操作检查(可以解决90%jar包没下载完全的问题):
先清除jar包:
?然年进行测试:
?表示pom文件没有写错?
3.找到框架的配置文件
在resource文件中新建xml文件,取名为hibernate.cfg.xml
?
?使用外部引用的方法在hibernate.cfg.xml文件中引入dtd约束
<?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/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>
<?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.zking.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>
user类:
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;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
+ remark + "]";
}
}
还需要写实体类的映射文件及测试代码
三、hibernate的使用
????????1、对框架核心配置文件(hibernate.cfg.xml)进行建模
? ? ? ? 2、获取sessionFactory工厂
? ? ? ? 3、获取session会话
? ? ? ? 4、开启事务(查询不需要事务)
? ? ? ? 5、session操作对象
? ? ? ? 6、提交事务(查询不需要事务)
? ? ? ? 7、关闭session
查询:
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("from User").list();
for(Object object:list) {
System.out.println(object);
}
session.close();
}
}
得到数据:
AddDemo:
?
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(55);
user.setRealName("小熊");
user.setRemark("武侠");
user.setSex("男");
user.setUserName("yiyi");
user.setUserPwd("123456");
session.save(user);
transaction.commit();
session.close();
}
}
修改EditDemo:
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, 55);
// 修改数据
user.setRealName("toto");
System.out.println(user);
transaction.commit();
session.close();
}
}
删除DelDemo:
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, 55);
// 修改数据
session.delete(user);
transaction.commit();
session.close();
}
?
|