0


Hibernate 配置文件详解

Hibernate 配置文件:

  • hibernate.xml
  • xxx.hbm.xml

一、hibernate.xml:

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

1.配置连接数据库的基本信息:

<!--数据源配置--><propertyname="connection.url">jdbc:mysql://localhost:3306/ssm</property><propertyname="connection.driver_class">com.mysql.cj.jdbc.Driver</property><propertyname="connection.username">Cailinhao</property><propertyname="connection.password">CAIlinhao11014359</property>

2.集成C3P0,配置数据库连接池信息:

<!--配置数据库连接池,这里使用C3P0--><!--连接数量不够时每次自增的个数--><propertyname="hibernate.c3p0.acquire_increment">10</property><!--设置连接失效时间--><propertyname="c3p0.idle_test_period">10000</property><!--设置连接超时时间--><propertyname="c3p0.timeout">5000</property><!--设置最大连接数--><propertyname="c3p0.max_size">30</property><!--设置最小连接数--><propertyname="c3p0.min_size">5</property><!--设置statement最大线程数--><propertyname="hibernate.c3p0.max_statements">10</property>

3.配置Hibernate基本信息:

<!--打印SQL语句--><propertyname="show_sql">true</property><!--格式化SQL语句--><propertyname="format_sql">true</property><!--数据库方言,方便根据不同方言自动生成SQL语句--><propertyname="dialect">org.hibernate.dialect.MySQL8Dialect</property><!--是否自动生成数据表,如果表存在则直接使用,否则县创建--><propertyname="hibernate.hbm2ddl.auto">update</property>

4.注册实体关系映射文件:

<!--注册实体关系映射文件--><mappingresource="org/example/Entity/CustomerEntity.hbm.xml"/><mappingresource="org/example/Entity/OrdersEntity.hbm.xml"/><mappingresource="org/example/Entity/AccountCourseEntity.hbm.xml"/><mappingresource="org/example/Entity/AccountsEntity.hbm.xml"/><mappingresource="org/example/Entity/CoursesEntity.hbm.xml"/>

二、xxx.hbm.xml:

实体关系映射文件,用来将实体类和数据库表建立映射关系。

<?xml version='1.0' encoding='utf-8'?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!--hibernate-mapping属性:
    package:为class标签对应的实体类设置包名,设置包名后class标签的name属性可以省略包名
    schema:数据库 schema 名
    catalog:数据库 catalog 名
    default-access:Hibernate用来访问属性的策略
    default-lazy:全局设置懒加载,默认为true
    auto-import:指定是否可以在查询语句中使用非全限定类名,默认为true(如果项目中有两个同名的持久化类,应该设置为false)
--><!--class属性:通过class将实体类和数据库表联系起来
    name:实体类名
    table:数据库表名
    schema:数据库名
    proxy:指定一个接口,在延迟加载时作为代理使用
    dynamic-update:动态更新,使用了动态更新后如果更新的数据的某个字段为null,则hibernate在执行的时候该字段不会传null给数据库,而是由数据库自己设置该字段为空
    dynamic-insert:动态添加,作用同动态更新
    where:与HQL配合使用,用来规定查询条件
--><!--id属性:指定主键和实体类属性对应
    name:实体类属性名
    type:实体类该属性的数据类型
    column:数据表字段名
    generator:主键生成策略:{uuid:使用UUID算法; identity:数据库自增;}
--><!--property属性:指定非主键和实体类属性对应
    name:实体类属性名
    type:实体类该属性的数据类型
    column:数据表字段名
    update:该字段是否可以修改,默认为true
    insert:该字段是否可以添加,默认为true
    lazy:延迟加载策略
--><hibernate-mappingpackage="org.example.Entity"><classname="org.example.Entity.CustomerEntity"table="customer"schema="ssm"><!--主键--><idname="id"column="id"type="java.lang.Integer"><generatorclass="uuid"/></id><!--非主键--><propertyname="name"column="name"type="java.lang.String"/><propertyname="age"column="age"type="java.lang.Integer"/><!--配置一对多关系--><setname="orders"table="orders"lazy="true"inverse="true"cascade="delete"><!--inverse:关联关系交给对方维护;cascade:级联删除--><!--该属性对应的外键--><keycolumn="customer_id"/><!--该外键所属的实体类--><one-to-manyclass="org.example.Entity.OrdersEntity"/></set></class></hibernate-mapping>
标签: hibernate java 后端

本文转载自: https://blog.csdn.net/m0_53881899/article/details/127978285
版权归原作者 姓蔡小朋友 所有, 如有侵权,请联系我们删除。

“Hibernate 配置文件详解”的评论:

还没有评论