0


Hibernate 配置文件(hibernate.cfg.xml、hbm.xml)

  • hibernate.cfg.xml
  • hbm.xml

Hibernate.xml

hibernate.xml 配置 Hibernate 的全局环境。

<?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">root</property>
 <property
name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
 <property name="connection.url">jdbc:mysql://localhost:3306/test?
useUnicode=true&amp;characterEncoding=UTF-8</property>
 <!-- C3P0 -->
 <property name="hibernate.c3p0.acquire_increment">10</property>
 <property name="hibernate.c3p0.idle_test_period">10000</property>
 <property name="hibernate.c3p0.timeout">5000</property>
 <property name="hibernate.c3p0.max_size">30</property>
 <property name="hibernate.c3p0.min_size">5</property>
 <property name="hibernate.c3p0.max_statements">10</property>
 <!-- 数据库⽅⾔ -->
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 <!-- 打印SQL -->
 <property name="show_sql">true</property>
 <!-- 格式化SQL -->
 <property name="format_sql">true</property>
 <!-- 是否⾃动⽣成数据库 -->
 <property name="hibernate.hbm2ddl.auto"></property>
 <!-- 注册实体关系映射⽂件 -->
 <mapping resource="com/southwind/entity/People.hbm.xml"></mapping>
 <mapping resource="com/southwind/entity/Customer.hbm.xml"></mapping>
 <mapping resource="com/southwind/entity/Orders.hbm.xml"></mapping>
 <mapping resource="com/southwind/entity/Account.hbm.xml"></mapping>
 <mapping resource="com/southwind/entity/Course.hbm.xml"></mapping>
 </session-factory>
</hibernate-configuration>

1、数据库的基本信息。

<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?
useUnicode=true&amp;characterEncoding=UTF-8</property>

2、集成 C3P0,设置数据库连接池信息。

<property name="hibernate.c3p0.acquire_increment">10</property>
<property name="hibernate.c3p0.idle_test_period">10000</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_statements">10</property>

3、Hibernate 基本信息。

<!-- 数据库⽅⾔ -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印SQL -->
<property name="show_sql">true</property>
<!-- 格式化SQL -->
<property name="format_sql">true</property>
<!-- 是否⾃动⽣成数据库 -->
<property name="hibernate.hbm2ddl.auto">update</property>

hibernate.hbm2ddl.auto 属性的可选值:

  • update:动态创建表,如果表存在,则直接使⽤,如果表不存在,则创建。
  • create:⽆论表是否存在,都会重新创建。
  • create-drop:初始化创建表,程序结束时删除表。
  • validate:校验实体关系映射⽂件和数据表是否对应,不能对应直接报错。

4、注册实体关系映射文件。

<!-- 注册实体关系映射⽂件 -->
<mapping resource="com/southwind/entity/People.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Customer.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Orders.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Account.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Course.hbm.xml"></mapping>

实体关系映射文件 (实体类文件名.hbm.xml)

<?xml version="1.0"?>
<!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.southwind.entity.Course" table="t_course">
         <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>
         <set name="accounts" table="account_course" lazy="false">
             <key column="cid"></key>
             <many-to-many class="com.southwind.entity.Account" column="aid">
             </many-to-many>
         </set>
     </class>
</hibernate-mapping>

1、hibernate-mapping标签的属性

  • package:给 class 节点对应的实体类统⼀设置包名,此处设置包名,class 的 name 属性就可以 省略包名。
  • schema:数据库 schema 的名称
  • catalog:数据库 catalog 的名称
  • default-cascade:默认的级联关系,默认为 none
  • default-access:Hibernate ⽤来访问属性的策略
  • default-lazy:指定了未明确注明 lazy 属性的 Java 属性和集合类,Hibernate 会采⽤什么样的加载 ⻛格,默认为 true
  • auto-import:指定我们是否可以在查询语句中使⽤⾮全限定类名,默认为 true,如果项⽬中有两 个同名的持久化类,最好在这两个类的对应映射⽂件中国配置为 false

2、class标签的属性

  • name:实体类名
  • table:数据表名
  • schema:数据库 schema 的名称,会覆盖 hibernate-mapping 的 schema
  • catalog:数据库 catalog 的名称,会覆盖 hibernate-mapping 的 catalog
  • proxy:指定⼀个接⼝,在延迟加载时作为代理使⽤
  • dynamic-update:动态更新(默认为false)
  • dynamic-insert:动态添加 (默认为false)

2.1、dynamic-insert:动态添加 (默认为false)

package com.southwind.test;
import com.southwind.entity.Account;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test8 {
 public static void main(String[] args) {
 //创建 Configuration
 Configuration configuration = new
Configuration().configure("hibernate.xml");
 //获取 SessionFactory
 SessionFactory sessionFactory = configuration.buildSessionFactory();
 //获取 Session
 Session session = sessionFactory.openSession();
 People people = new People();
 people.setName("张三");
 session.save(people);
 session.close();
 }
}

dynamic-insert=false

dynamic-insert=true

<class name="com.southwind.entity.People" table="people" dynamicinsert="true">

2.2、dynamic-update:动态更新(默认为false)

public class Test8 {
 public static void main(String[] args) {
 //创建 Configuration
 Configuration configuration = new
Configuration().configure("hibernate.xml");
 //获取 SessionFactory
 SessionFactory sessionFactory = configuration.buildSessionFactory();
 //获取 Session
 Session session = sessionFactory.openSession();
 People people = session.get(People.class,6);
 people.setMoney(2000.0);
 session.update(people);
 session.beginTransaction().commit();
 session.close();
 }
}

**dynamic-update=false **

dynamic-update=true

<class name="com.southwind.entity.People" table="people" dynamic-insert="true"
dynamic-update="true">

2.3、where:查询时给 SQL 添加 where 条件

import com.southwind.entity.Account;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test8 {
 public static void main(String[] args) {
 //创建 Configuration
 Configuration configuration = new
Configuration().configure("hibernate.xml");
 //获取 SessionFactory
 SessionFactory sessionFactory = configuration.buildSessionFactory();
 //获取 Session
 Session session = sessionFactory.openSession();
 String hql = "from People";
 Query query = session.createQuery(hql);
 List<People> list = query.list();
 for (People people:list) {
 System.out.println(people);
 }
 session.beginTransaction().commit();
 session.close();
 }
}

** 设置where属性之前**

** 设置where属性之后**

<class name="com.southwind.entity.People" table="people" dynamic-insert="true"
dynamic-update="true" where="id = 6">

标签: hibernate xml java

本文转载自: https://blog.csdn.net/weixin_57558097/article/details/128481780
版权归原作者 兴趣使然h 所有, 如有侵权,请联系我们删除。

“Hibernate 配置文件(hibernate.cfg.xml、hbm.xml)”的评论:

还没有评论