重温hibernate(一)hibernate环境搭建

本文是hibernate环境搭建的入门教程,通过idea创建maven项目,详细介绍了所需的依赖,包括core、log4j和mysql-connector。接着展示了Customer.java实体类、Customer.hbm.xml映射文件、hibernate.cfg.xml配置文件的创建过程。最后,通过CustomerTest.java进行测试,演示如何保存一个客户到数据库,并提到了session与线程绑定的配置选项和c3p0与jdbc的验证。

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

目录

项目结构

pom.xml

Customer.java

Customer.hbm.xml

hibernate.cfg.xml

CustomerTest.java


实现:保存一个客户到数据库

idea建maven项目

项目结构

pom.xml

只需这三个依赖core,log4j,mysql-connector

 <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.1.Final</version>
  </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.44</version>
    </dependency>

Customer.java

package com.luobo.entity;

/**
 * . Description:
 *
 * @author: ws
 * @version: 1.0
 */
public class Customer {
    private int id;
    private String name;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!--导入dtd约束 core.jar 里org.hibernate.hibernate-mapping-3.0.dtd找-->
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.luobo.entity">
  <class name="Customer" table="customer">
    <id name="id" column="cus_id">
      <!--generator指定主键生成方式-->
      <generator class="native"/>
    </id>
    <property name="name" column="cus_name"></property>
  </class>
</hibernate-mapping>

hibernate.cfg.xml

hibernate-release-5.4.1.Final\project\etc\hibernate.properties

<?xml version="1.0" encoding="UTF-8" ?>
<!--导入dtd约束 core.jar 里org.hibernate.hibernate-configuration-3.0.dtd找-->
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <!--配置信息:下载hibernate-release-5.4.1.Final.zip,hibernate-release-5.4.1.Final\project\etc\hibernate.properties-->
  <!--配置sessionFactory
  1.连接数据库信息
  2.hibernate可选配置
  3.映射文件的位置-->
  <session-factory>
    <!--1.连接数据库信息-->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql:///test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!--2.hibernate可选配置-->
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!--3.映射文件的位置-->
    <mapping resource="com/luobo/entity/Customer.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

CustomerTest.java

import com.luobo.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * . Description:
 *
 * @author: ws
 * @version: 1.0
 */
public class CustomerTest {
    /*
    *步骤分析:
    * 1.解析主配置文件
    * 2.根据配置文件创建sessionFactory
    * 3.根据sessionFactory创建session
    * 4.开启事务
    * 5.执行操作(保存)
    * 6.提交事务
    * 7.释放资源
    * */

    @Test
    public void test() {
        Customer c = new Customer();
        c.setName("2");
        //1.解析主配置文件 org.hibernate.cfg.Configuration
        Configuration cfg = new Configuration();
        cfg.configure();
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(c);
        tx.commit();
        session.close();
        sessionFactory.close();
    }
}

附:若需session与线程绑定

配置文件加(亲测貌似不加也行)

<!--session与线程绑定,getCurrentSession-->
<property name="current_session_context_class">thread</property>

pom.xml加

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
  <version>5.4.1.Final</version>
</dependency>

<dependency>
  <groupId>com.mchange</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.5.2</version>
</dependency>

验证:c3p0还是jdbc 

public void test() {
    Configuration cfg = new Configuration();
    cfg.configure();
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            System.out.println(connection.getClass().getName());
        }
    });
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值