Hibernate基础配置

本文详细介绍了Hibernate框架的基础配置步骤,包括导入jar包、配置Hibernate.cfg.xml文件、设置类名.hbm.xml映射文件,以及如何通过测试类进行数据保存操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Hibernate框架进行基础的配置

1.导入jar包

2.引入配置文件Hibernate.cfg.xml   该配置文件配置了连接数据库的基本配置

可选配置:  hibernate.hbm2ddl.auto

none:不使用hibernate自动建表

create:如果数据库中已有表,删除原表,重新创建,如果没有表,创建新表

create-drop  如果已经有表,删除原表,执行操作,再删除这个表      如果没有表,新建表,使用后删除

                     当把sessionFactory  close()后生效      一般做测试用

update  如果有表,使用原表     如果没有,重新建表

validate  检验映射结构和表结构是否一致,不一致报错

<!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.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=utf8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 打印SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!--映射文件地址-->
		<mapping resource="Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

3.配置   类名.hbm.xml     即与数据库的映射文件

class标签:作用建立类与表的映射关系

属性:

            name:类的全路径

            table:表名

ID标签:作用建立类中的属性与表中的主键对应关系

属性:

        name:类中的属性

        column:表中主键字段

property标签:建立类中普通属性与表的对应关系

      name:类中属性

      column:表中字段

<?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>
    <class name="com.bullet.domain.Customer" table="customer">
        <!--建立类中的属性与表中的主键对应-->
        <id name="cust_id" column="cust_id" >
            <generator class="native"/>
        </id>
        <!--建立类中的普通的属性和表的字段的对应-->
        <property name="cust_name" column="cust_name" />
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
    </class>
</hibernate-mapping>

 <generator  主键生成策略>

increment: 自动增长策略  long  int  short  在单线程中使用

identity: 自动增长,使用数据库底层的增长策略,适用于有自动增长机制的数据库(MySQL支持   Oracle不支持)

sequence:自动增长,采用序列增长方式,必须得要支持序列的数据库(Oracle支持   MySQL不支持)

UUID

native:主键自动增长,根据数据库的不同自动切换策略

assigned:手动生成来管理主键

 POJO类:

package com.bullet.domain;

import lombok.Getter;
import lombok.Setter;

@Setter@Getter
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;
}

 测试类:

package com.bullet.test;

import com.bullet.domain.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class HibernateTest {
    @Test
    public void test1(){
        //1.加载配置文件
        Configuration configure = new Configuration().configure();
        //2.创建sessionFactory
        SessionFactory sessionFactory = configure.buildSessionFactory();
        //获取session
        Session session = sessionFactory.openSession();

        Customer customer = new Customer();
        customer.setCust_name("LeBron");
        customer.setCust_level("1");
        //保存
        session.save(customer);
        //释放资源
        session.close();
        sessionFactory.close();

    }
}

可能出现的问题,在测试时,控制台打印Unknown initial character set index '255' received from server. Initial client character ,原因是MySQL驱动和数据库字符集设置不搭配

因此要在url后面配置,加上   ?useUnicode=true&characterEncoding=utf8

如果是在xml中配置    则&要改用转义字符 &amp;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值