- Hibernate 是什么?
1、首先要理解 Hibernate 是什么,一句话总结:
Hibernate 是持久层 ORM 映射框架;
2、什么是ROM 模型;
实体 bean 和 table 之间关系映射;
在很早以前,sun 公司针对 各层设计分为 4层,如下;后面webApplet 被 更轻量的 js、html 取代,省去了在客户端运行需要 jre 环境的操作,大大加大了开发效率,成为现在的经典三层web,service,dao架构模式;针对每一层的解决方案如下:
项目结构如下, xxx.hbm.xml 和 hibernate.cfg.xml命名要规范,底层地洞调用该配置文件:
点开hibernate的configuration底层查看:
Hibernate的配置,分为 mapper 和Hibernate核心文件的配置:
(1)mapper配置:
<?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>
<!-- 创建映射关系,id 和table中主键对应 -->
<class name="com.baidu.bean.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
(2)核心配置文件配置:
<?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>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选的配置 -->
<property name="hibernate.show_sql" >true</property><!--打印sql-->
<property name="hibernate.format_sql">true</property><!--格式化sql -->
<!--加载映射文件 -->
<mapping resource="com/baidu/bean/Customer.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
<!-- Create:表示可以根据实体配置文件来自动生成表(只能生成表);
create-drop:表示加载Hibernate时创建表,退出时,删除表;
update:表示加载时自动更新数据库结构(即如果没有该表,则创建;有则不再创建,只更新);
validate:表示加载Hibernate验证创建数据库表结构;-->
pom.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hibernate1030day01</groupId>
<artifactId>hibernate1030day01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 使用的hibernate为 5.0.7版本 -->
<!-- 配置hibernate 核心包 -->
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.0.Final</version>
</dependency>
<!-- 连接mysql的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<!--log4j 日志包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- -->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
测试类:
/**
* @auther SyntacticSugar
* @data 2018/10/30 0030下午 6:21
*/
public class HibernateTest {
@Test
public void test01(){
/** 配置是hibernate中的 org.hibernate.cfg.Configuration
* 加载配置文件
* 创建sessionFactory
* 创建session 会话对象
*/
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setCust_name("打豆豆");
session.save(customer);//忘记保存了
//提交事务,关闭会话
transaction.commit();
session.clear();
}
}
和数据库 table 对应的实体类,提供setter 、getter、toString:
/**
* @auther SyntacticSugar
* @data 2018/10/30 0030下午 4:58
*/
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
// 提供setter 、getter、toString
}