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

这篇具有很好参考价值的文章主要介绍了Hibernate 配置文件(hibernate.cfg.xml、hbm.xml)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 文章来源地址https://www.toymoban.com/news/detail-405231.html

目录

Hibernate.xml 

1、数据库的基本信息。

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

3、Hibernate 基本信息。 

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

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

1、hibernate-mapping标签的属性

2、class标签的属性 

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

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

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


  • 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

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

dynamic-insert=true

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

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

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 

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

dynamic-update=true

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

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

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属性之前Hibernate 配置文件(hibernate.cfg.xml、hbm.xml)

  设置where属性之后

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

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

 

到了这里,关于Hibernate 配置文件(hibernate.cfg.xml、hbm.xml)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 一文快速入门体验 Hibernate

    Hibernate 是一个优秀的持久层的框架,当然,虽然现在说用得比较多的是 MyBaits,但是我工作中也不得不接触 Hibernate,特别是一些老项目需要你维护的时候。所以,在此写下这篇文章,方便自己回顾,也方便新手入门体验 Hibernate。 注:使用的版本是 Hibernate 5.x 的 ORM(Object R

    2024年02月03日
    浏览(35)
  • hibernate入门

    Hibernate 是一个开源的 ORM(对象关系映射)框架,它可以将 Java 对象与数据库表进行映射,从而实现面向对象的数据持久化。使用 Hibernate,可以避免手动编写 SQL 语句,从而提高开发效率,并且可以轻松地切换不同的数据库。 entity 实体类是映射到数据库表中的 Java 类,它包含

    2024年02月13日
    浏览(33)
  • Hibernate(一)——入门

    在之前经常用到操作数据库的框架是Mybatis或者Mybatis-plus。 Hibernate在項目中用过,但没有深入的了解过,所以这次趁着假期把这个框架了解一下。 Hibernate就是一个 持久层 的 ORM 框架 什么是ORM框架? 利用描述对象和数据库表之间映射的元数据,自动把Java应用程序中的对象,持

    2024年02月02日
    浏览(33)
  • Hibernate Validator 组件

    Hibernate Validator 组件介绍 Hibernate Validator 是一个基于 Java 的验证框架,它提供了强大且灵活的验证功能,用于验证 JavaBean 对象的状态。它是基于 JSR 380 规范(Bean Validation 2.0)的实现,并且可以与任何 Java 应用程序集成。 Hibernate Validator 的目标是提供一种方便、易于使用的验证

    2024年02月11日
    浏览(27)
  • 9.3. Hibernate框架

    Hibernate是一个开源的持久层框架,它可以帮助我们将Java对象映射到数据库表中,并实现对象的持久化操作。Hibernate提供了丰富的API,可以方便地进行CRUD(增删改查)操作,而无需手动编写复杂的JDBC代码。 9.3.1. Hibernate核心组件 Hibernate主要包含以下几个核心组件: SessionFact

    2024年02月08日
    浏览(40)
  • 框架分析(9)-Hibernate

    link 主要对目前市面上常见的框架进行分析和总结,希望有兴趣的小伙伴们可以看一下,会持续更新的。希望各位可以监督我,我们一起学习进步。 Hibernate是一个开源的Java持久化框架,它提供了一种将Java对象映射到数据库表的方法,使得开发人员可以使用面向对象的方式来

    2024年02月10日
    浏览(73)
  • hibernate 懒加载

    @Entity @Table(name = \\\"Student\\\") public class StudentInformation { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int rollno; private String name; // Mapping to the other table @OneToMany(mappedBy =\\\"stud\\\" ,cascade = CascadeType.ALL,fetch= FetchType.LAZY ) private SetAddress addressSet=new HashSet();; public StudentInformation() { } public St

    2024年02月09日
    浏览(77)
  • MyBatis和Hibernate的区别

    Hibernate 和 MyBatis 都是目前业界中主流的对象关系映射(ORM)框架,它们的主要区别如下。 1)sql 优化方面 Hibernate 使用 HQL(Hibernate Query Language)语句,独立于数据库。不需要编写大量的 SQL,就可以完全映射,但会多消耗性能,且开发人员不能自主的进行 SQL 性能优化。提供了

    2024年02月09日
    浏览(27)
  • MyBatis与Hibernate的区别

    目录 1、MyBatis与Hibernate 1.1 Hibernate简介 1.2 MyBatis简介 2、开发对比 2.1 开发速度 2.2 开发社区 2.3 开发工作量 3、优化对比 3.1 Hibernate的调优方案 3.2 Mybatis调优方案 3.3 SQL优化方面 3.4 扩展性方面 4、对象管理与抓取策略 4.1 对象管理 4.2 抓取策略 5、缓存机制 5.1 Hibernate缓存 5.2 MyB

    2024年04月28日
    浏览(25)
  • 【hibernate validator】(五)分组约束

    https://blog.zysicyj.top/ 定义一个超级跑车 使用组继承 定义序列组 使用序列组 定义一个具有重定义的默认组的类 使用重新定义的默认组 实现和使用默认组序列 必须集合@Valid哦,否则报错 本文由 mdnice 多平台发布

    2024年02月11日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包