什么是hibernate
ORM框架/持久层框架 jdbc的一个框架
object reference mapping
通过管理对象来改变数据库中的数据
通过管理对象来操作数据库
hibernate的优势以及他的缺点
hibernate的优势是什么呢?
它在人开发编程的时候不需要开发人员写sql语句,提高了开发的效率
它的优势也是它的缺点,因为sql语句不可被更改,导致很多东西完成 不了,解决bug的效率降低
hibernate的入门知识
首先我们需要添加个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>com.yz</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>
配置完pom.xml之后
我们需要将web.xml的版本更改一下,我们需要把2.5改成3.1
<!-- 第二步修改web.xml由2.3至3.0 -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
</web-app>
这些是基本配置
接下来我们需要一个最重要的配置文件
主配置文件
这个是连接数据库的桥梁
<?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/yz?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/liyi/one/entity/User.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:对应的是需要映射的实体类的全路径名
table:实体类对应的数据库中的表
id标签:配置的是表中的主键
name:对应的是实体类的属性名
type:指的是实体类数据类型
column:数据库表对应的列段
property:配置除去主键以外列段对应的类属性映射关系
name:对应的是实体类的属性名
type:指的是实体类数据类型
column:数据库表对应的列段
insert="false" update="false"
上面的表示的含义是:该列段或者该属性制作查询用,不做更新
-->
<class name="com.yz.one.entity.User" table="tb_user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment" />
</id>
<property name="name" type="java.lang.String" column="name">
</property>
<property name="upwd" type="java.lang.String" column="upwd">
</property>
</class>
</hibernate-mapping>
我们还需要一个实体类
package com.yz.one.entity;
public class User {
private Integer id;
private String name;
private String upwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public User(Integer id, String name, String upwd) {
super();
this.id = id;
this.name = name;
this.upwd = upwd;
}
public User() {
super();
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", upwd=" + upwd + "]";
}
}
最后我们就需要进行测试了
首先我们进行查询的代码
package com.yz.one.demo;
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();
}
}
增加
package com.yz.one.demo;
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;
import com.liwangwang.entity.User;
public class InsertDemo {
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();
session.save(new User("王主席", "123"));
transaction.commit();
session.close();
}
}
删除
package com.yz..one.demo;
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;
import com.liwangwang.entity.User;
public class DeleteDemo {
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(3);
session.delete(user);
transaction.commit();
session.close();
}
}
修改
package com.yz.one.demo;
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;
import com.liwangwang.entity.User;
public class UpdateDemo {
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, 2);
user.setUserName("yy");
transaction.commit();
session.close();
}
}
说说hibernate管理的三种状态
从查询到增加和修改删除各自状态都不相同