Hibernate简介
1、ORM框架/持久层框架 jdbc的一个框架
3、通过管理对象来改变数据库中的数据
4、通过管理对象来操作数据库
5、跨数据库的无缝移植(就是可以直接换数据库比如原先项目用的是mySql后面项目转Oracle不需要改任何代码)
6、不需要写sql语句,这是一个优势同时也成为hibernate被淘汰的原因就是应为sql语句都是hibernate自动生成的所有导致了它丧失了灵活性
为什么使用Hibernate
对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。
Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现。它很大程度的简化DAO层的编码工作
hibernate使用Java反射机制,而不是字节码增强程序来实现透明性。
hibernate的性能非常好,因为它是个轻量级框架。映射的灵活性很出色,它支持各种关系数据库,从一对一到多对多的各种复杂关系。
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.xwt</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>5.1.46</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>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
添加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>
<!-- 1. 数据库相关 -->
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url">jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</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>
<!-- 3. 添加实体映射文件 -->
<mapping resource="com/xwt/one/entity/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
实体类User.java
package com.xwt.one.entity;
import java.sql.Date;
import java.sql.Timestamp;
public class User {
private Integer id;
private String userName;
private String userPwd;
private String realName;
private String sex;
private Date birthday;
private Timestamp createDatetime;
private String remark;
public Integer getId() {
return id;
}
public void setId(Integer 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() {
super();
}
public User(String userName, String userPwd, String realName, String sex, Date birthday, Timestamp createDatetime,
String remark) {
super();
this.userName = userName;
this.userPwd = userPwd;
this.realName = realName;
this.sex = sex;
this.birthday = birthday;
this.createDatetime = createDatetime;
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 + "]";
}
}
实体类对应的配置文件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>
<!--
class标签下的属性
name:指的是数据库中的表所映射的类全路径名
table:要映射的数据库表
id标签:用来表示主键的映射列
name:数据库表的列段映射到实体类中的属性名
type:属性名的类型
column:数据库中的列段
property标签
name:数据库表的列段映射到实体类中的属性名
type:属性名的类型
column:数据库中的列段
-->
<class name="com.xwt.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>
查询QueryDemo.java
package com.xwt.one.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 演示hibernate查询
* @author wt
*
*/
public class QueryDemo {
public static void main(String[] args) {
/**
* 1.对hibernate.cfg.xml这个核心的配置文件进行建模
* 2.建模后的对象去获取sessionfactory对象 sessionfactory对象中包含了数据库相关信息
* 3.通过sessionfactory对象去获取session会话
* 4.session开启事务(查询不需要)
* 5.操作数据库
* 6.提交事务(查询不需要)
* 7.释放资源
*/
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 obj : list) {
System.out.println(obj);
}
session.close();
}
}
增加AddDemo.java
package com.xwt.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.xwt.one.entity.User;
/**
* 演示hibernate新增
* @author wt
*
*/
public class AddDemo {
public static void main(String[] args) {
/**
* 1.对hibernate.cfg.xml这个核心的配置文件进行建模
* 2.建模后的对象去获取sessionfactory对象 sessionfactory对象中包含了数据库相关信息
* 3.通过sessionfactory对象去获取session会话
* 4.session开启事务
* 5.操作数据库
* 6.提交事务
* 7.释放资源
*/
Configuration configure=new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory= configure.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
User user=new User("秀秀", "888", "天仙秀", "女", new Date(System.currentTimeMillis()), new Timestamp(System.currentTimeMillis()), "我想你是爱我的");
session.save(user);
transaction.commit();
session.close();
}
}
修改EditDemo.java
两种方式
package com.xwt.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.xwt.one.entity.User;
/**
* 演示hibernate修改
* @author wt
*
*/
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();
//1
/* User user=new User("山鸡", "123", "李山鸡", "男", new Date(System.currentTimeMillis()), new Timestamp(System.currentTimeMillis()), "臭傻逼");
user.setId(14);
session.update(user); */
//2
User user=session.get(User.class, 9);
user.setRealName("小顽童");
System.out.println(user);
transaction.commit();
session.close();
}
}
1
2
删除DelDemo.java
package com.xwt.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.xwt.one.entity.User;
/**
* 演示hibernate删除
* @author wt
*
*/
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=new User();
user.setId(9);
session.delete(user);
transaction.commit();
session.close();
}
}