初识hibernate

使用环境

hibernate 5.2 mysql8.0 jdk1.8 mysql-connector 8

导入依赖

编辑hibernate.cfg.xml文件

hibernate.cfg.xml

//数据库方言
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
//mysql8.0的连接写法
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
//?前为数据库名
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/homework?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=UTC&amp;useSSL=false&amp;allowPublicKeyRetrieval=true</property>
//用户名和密码
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">765489</property>
<property name="show_sql">true</property>
//映射
<mapping resource="pojo/User.hbm.xml"/>

创建实体类

在pojo包中创建User.java

package pojo;

import java.util.Date;

public class User {
    private int id;
    private String name;
    private String gender;
    private int age;
    private Date birthday;

    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;
    }

    public String getGender() {
        return gender;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

创建数据库的表

设置主键为自增

编辑User.hbm.xml

在pojo包中导入User.hbm.xml文件,编写文件内容

<?xml version="1.0"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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="pojo.User" table="T_USER">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="gender"/>
        <property name="age"/>
        <property name="birthday"/>

    </class>
    

</hibernate-mapping>

创建usermanager进行测试

在dao包下创建usermanager.java

package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import pojo.User;

import java.sql.Date;

public class managerUser {
    public static void main(String[] args) {
        SessionFactory sessionFactory = null;
        //创建Con对象:对应hibernate的基本配置信息和对象关系映射
        Configuration config = new Configuration().configure();
        //创建一个SessionFactoryRegistry对象hibernate的任何配置和服务都需要在该对象中注册后才有用
        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();
        sessionFactory  = config.buildSessionFactory(standardRegistry);
        //创建并打开会话
        Session session =sessionFactory.openSession();
        //开启事务
        org.hibernate.Transaction transaction = session.beginTransaction();
        User u = new User();
        u.setName("skadi");
        u.setGender("女");
        u.setAge(18);
        u.setBirthday(Date.valueOf("2022-2-2"));
        session.save(u);
        //提交事务
        transaction.commit();
        //关闭session
        session.close();
        //关闭sessionfactory
        sessionFactory.close();

    }
}

插入数据如下 

配置途中出现java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException,检查发现是jdk版本过高,使用jdk9以下版本即可。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值