idea hibernate 环境搭建

本文详细介绍了如何在hibernate.cfg.xml中配置数据库连接参数,包括驱动、URL、用户名和密码,以及如何指定映射文件和实体类User。还涵盖了JDBC连接池、SQL方言、会话管理、缓存和SQL日志设置。

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

在这里插入图片描述

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <!-- 表示一个数据库会话工厂 -->
    <session-factory>
        <!-- 指定数据库连接参数 -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/newsdb?serverTimezone=UTC</property>
        <property name="connection.username">hz</property>
        <property name="connection.password">hz</property>
        <!-- 指定JDBC连接池 -->
        <property name="connection.pool_size">1</property>
        <!-- 指定SQL方言,不同的DBMS,有所不同 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- 打开Hibernate自动会话上下文管理  -->
        <property name="current_session_context_class">thread</property>
        <!-- 关闭二级缓存 -->
        <property name="cache.provider_class">org.hibernate.cache.internal.DisabledCaching</property>
        <!-- 指定将所有执行的SQL语句回显到stdout -->
        <property name="show_sql">true</property>
        <!-- 在启动时对表进行检查 -->
        <property name="hibernate.hbm2ddl.auto">validate</property>
        <!-- 指定映射文件,相对路径,若有多个,使用多个mapping元素指定 -->
        <mapping resource="/com/hky/hibernate/User.hbm.xml" />

    </session-factory>
</hibernate-configuration>

实体类

public class User {
    private Integer id;
    private String username;
    private String password;
    private String name;
    private String gender;
    private String birthday;
    private String hometown;
    private String hobby;
    private String introduction;
    private Integer casting;
    private String registerTime;
    private Integer entry;
    //代码省略get set toString 方法 自行添加
}

定义映射文件
User.hbm.xml

<?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 package="com.hky.hibernate">
    <!-- 建立类与数据库表的映射 -->
    <class name="User" table="user">
        <!-- 建立类中的属性与表中的主键对应 -->
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <!--建立类中其他普通属性与表中字段的对应-->
        <property name="username"  column="username"/>
        <property name="password" column="password"/>
        <property name="name" column="name"/>
        <property name="gender" column="gender"/>
        <property name="birthday" column="birthday"/>
        <property name="hometown" column="hometown"/>
        <property name="hobby" column="hobby"/>
        <property name="introduction" column="introduction"/>
        <property name="casting" column="casting"/>
        <property name="registerTime" column="register_time"/>
        <property name="entry" column="entry"/>
    </class>
</hibernate-mapping>

测试:

public class Test {

    @org.junit.Test
    public void demo1() {
        //1.加载核心配置文件
        Configuration configuration = new Configuration().configure();
        //2.创建一个SessionFactory对象:类似于JDBC连接池
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //3.通过SessionFactory获取到Session对象:类似于JDBC中的Connection
        Session session = sessionFactory.openSession();
        //4.手动开启事务
        Transaction transaction = session.beginTransaction();
        //5.代码编写
        User user = session.get(User.class, 3);
        System.out.println(user);
        //6.事务提交
        transaction.commit();
        //7.释放资源
        session.close();
    }
}

ieda 自动生成

public class Main {
    private static final SessionFactory ourSessionFactory;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            User user = session.get(User.class, 3);
            System.out.println(user);
        } finally {
            session.close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值