Hibernate01_使用原生接口开发

x33g5p2x  于2021-12-25 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(375)

Hibernate01_使用原生接口开发

  • 新建 Maven ⼯程,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.blu</groupId>
	<artifactId>hibernate</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.10</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.19</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.4.12.Final</version>
		</dependency>
	</dependencies>
</project>
  • 创建实体类
package com.blu.entity;
import lombok.Data;

@Data
public class People {
	private Integer id;
	private String name;
	private Double money;
}
  • 对应数据库表

  • hibernate.cfg.xml(全局配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
        
        
<hibernate-configuration>
	 <session-factory>
	 		<!-- 数据源配置 -->
	 		<property name="connection.username">root</property>
	 		<property name="connection.password">123456</property>
	 		<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
	 		<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedemo?useUnicode=true&amp;characterEncoding=UTF-8</property>
	 		
	 		<!-- C3P0连接池 -->
	 		<!-- 连接不够用的时候, 每次增加的连接数 -->
	 		<property name="hibernate.c3p0.acquire_increment">10</property>
	 		<!-- 每隔10000秒释放空闲连接 -->
	 		<property name="hibernate.c3p0.idle_test_period">10000</property>
	 		<!-- 获得连接的超时时间为5000毫秒 -->
	 		<property name="hibernate.c3p0.timeout">5000</property>
	 		<!-- 最大最小连接数 -->
	 		<property name="hibernate.c3p0.max_size">30</property>
	 		<property name="hibernate.c3p0.min_size">5</property>
	 		<!-- 最大的PreparedStatement的数量 -->
			<property name="hibernate.c3p0.max_statements">10</property>
	 		
	 		<!-- 数据库方言 -->
	 		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	 		<!-- 打印和格式化sql -->
	 		<property name="show_sql">true</property>
	 		<property name="format_sql">true</property>
	 		<!-- 是否根据实体类自动生成数据表 -->
	 		<property name="hibernate.hbm2ddl.auto"></property>
	 		
	 		<!-- 注册实体关系映射文件 -->
	 		<mapping resource="com/blu/entity/People.hbm.xml"></mapping>
	 </session-factory>		

</hibernate-configuration>>
  • People.hbm.xml(实体关系映射文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.blu.entity.People" table="people">
		<id name="id" type="java.lang.Integer">
			<column name="id"></column>
			<generator class="identity"></generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name"></column>
		</property>
		<property name="money" type="java.lang.Double">
			<column name="money"></column>
		</property>
	</class>

</hibernate-mapping>
  • 测试
package com.blu.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.blu.entity.People;

public class Test {
	public static void main(String[] args) {
		//创建Configuration
		//如果文件名不为hibernate.cfg.xml,则需要指定:
		//Configuration configuration = new Configuration().configure("hibernate.xml");
		Configuration configuration = new Configuration().configure();
		//获取SessionFactory
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		//获取Session
		Session session = sessionFactory.openSession();
		People people = new People();
		people.setName("张三");
		people.setMoney(1000.0);
		session.save(people);
		session.beginTransaction().commit();
		session.close();
	}
	
}
  • 结果:
Hibernate: 
    insert 
    into
        people
        (name, money) 
    values
        (?, ?)

配置文件详解:

<!-- 如果表存在,则直接使用,表不存在,则创建表-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 无论表是否存在,都会重新创建 -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 程序初始化时创建表,程序结束时删除表 -->
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- 校验实体关系映射文件和数据表是否对应,不能对应则直接报错 -->
<property name="hibernate.hbm2ddl.auto">validate</property>

相关文章