hibernate环境搭建之HelloWorld

1.包的介绍

2.HelloWorld

  • 建立项目

 

 

  • 创建hibernate.cfg.xml文件核心配置文件模板
<?xml version='1.0' encoding='utf-8'?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hiber01</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- 设置数据库连接池的初始化连接数 -->
        <property name="connection.pool_size">1</property>

        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="show_sql">true</property>
        <!--跟表的映射配置文件-->
        <mapping resource="model/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>
  • 创建实体类
package model;

import java.util.Date;

public class User {
    private int userId;
    private String uname;
    private String gender;
    private Date birthday;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
  • Object 与 relation的映射 文件名xx.hbm.xml

<

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--tutorial.domain-->
<hibernate-mapping>
    <class name="model.User" table="t_user">
        <id name="userId" column="user_id">
            <generator class="assigned"></generator>
        </id>
        <property name="uname" column="uname"/>
        <property name="gender" column="gender"/>
        <property name="birthday" column="birthday"/>
    </class>
</hibernate-mapping>
  • 生成表
package utils;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class DBExport {

    public static void main(String[] args) {
        // 创建hebernate配置对象
        Configuration cfg = new Configuration();
        // 指定hibernate.cfg.xml位置
        cfg.configure("hibernate.cfg.xml");
        // 创建表的对象
        SchemaExport se = new SchemaExport(cfg);
        se.create(true, true);
    }
}

log:

  • insert测试
package test;

import model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import org.hibernate.service.ServiceRegistry;
import java.util.Date;

public class Testhebernate {

    @Test
    public void test(){
        // 创建hibernate配置对象
        Configuration cfg = new Configuration();
        // 配置核心文件的位置
        cfg.configure("hibernate.cfg.xml");
        // 注册配置属性信息
        ServiceRegistry sr = (ServiceRegistry) new StandardServiceRegistryBuilder().
                applySettings(cfg.getProperties()).build();
        // 创建SessionFactory
        SessionFactory factory = cfg.buildSessionFactory(sr);
        // 创建Session会话
        Session session = factory.openSession();
        // 开启事务
        Transaction tx = session.beginTransaction();
        // 创建对象
        User user = new User();
        user.setUserId(1);
        user.setUname("你妹");
        user.setGender("男");
        user.setBirthday(new Date());
        // 通过Session保存对象
        session.save(user);
        // 提交事务
        tx.commit();
        session.close();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值