一,Hibernate介紹:
**
什么是ORM ORM(Object/Relational Mapping):对象/关系映射
是通过使用描述对象和数据库之间映射的数据,将java程序中的对象自动持久化到关系数据库中 ,本质上就是将数据从一种形式转换到另外一种形式**
实现约束
1, 在resource目录下添加hibernate.cfg.xml(核心配置文件),引入hibernate-configuration-3.0.dtd约束(注意mysql版本一致)
2,添加实体类映射文件,引入hibernate-mapping-3.0.dtd约束
1,对框架核心配置文件进行建模
2,获取sessionFactory工厂
3,获取session会话
4,开启事物(查询不需要事物)
5,session操作对象
6,提交事物(查询不需要事物) 7,关闭事物
1.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>mjx_id</groupId>
<artifactId>mjx_hibernate</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mjx_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>mjx_hibernate</finalName>
</build>
</project>
**找到图中两个dtd文件并且外部引用**
添加hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
//这里是引入上图dtd文件约束
"-//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/?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/mjx/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>
建实体类
package com.mjx.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 getUsreName() {
return userName;
}
public void setUsreName(String usreName) {
this.userName = usreName;
}
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 + ", usreName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
+ remark + "]";
}
}
建实体类对应的映射文件
<?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.mjx.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>
CRUD
//查询
package com.mjx.entity;
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;
public class Text {
public static void main(String[] args) {
// 1、对框架核心配置文件进行建模
Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
// 2、获取sessionFactory工厂
SessionFactory sessionFactory = configure.buildSessionFactory();
// 3、获取session会话
Session session = sessionFactory.openSession();
// 4、开启事务(查询不需要事务)
Transaction transaction = session.beginTransaction();
// 5、session操作对象
//增加
//新建对象
User user = new User();
user.setBirthday(new Date(System.currentTimeMillis()));// 生日
user.setCreateDatetime(new Timestamp(System.currentTimeMillis()));// 日期时间
user.setId(12);
user.setRealName("小明");
user.setRemark("城市");
user.setSex("男");
user.setUsreName("小小");
user.setUserPwd("123456");
//增加方法
session.save(user);
//修改
//User user = session.get(User.class, 12);
//user.setRealName("隔壁老陈");
//删除
//方法一:
//User user = session.get(User.class, 12);
//session.delete(user);
//方法二:
//User user=new User();
//user.setId(12);
//session.delete(user);
// 6、提交事务(查询不需要事务)
transaction.commit();
// 7、关闭session
session.close();
}
}
修改删除增加
package com.mjx.entity;
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;
public class Text {
public static void main(String[] args) {
// 1、对框架核心配置文件进行建模
Configuration configure = new Configuration().configure("/hibernate.cfg.xml");
// 2、获取sessionFactory工厂
SessionFactory sessionFactory = configure.buildSessionFactory();
// 3、获取session会话
Session session = sessionFactory.openSession();
// 4、开启事务(查询不需要事务)
Transaction transaction = session.beginTransaction();
// 5、session操作对象
//增加
//新建对象
User user = new User();
user.setBirthday(new Date(System.currentTimeMillis()));// 生日
user.setCreateDatetime(new Timestamp(System.currentTimeMillis()));// 日期时间
user.setId(12);
user.setRealName("小明");
user.setRemark("城市");
user.setSex("男");
user.setUserName("小小");
user.setUserPwd("123456");
//增加方法
session.save(user);
//修改
//User user = session.get(User.class, 12);
//user.setRealName("隔壁老陈");
//删除
//方法一:
//User user = session.get(User.class, 12);
//session.delete(user);
//方法二:
//User user=new User();
//user.setId(12);
//session.delete(user);
// 6、提交事务(查询不需要事务)
transaction.commit();
// 7、关闭session
session.close();
}
}