Hibernate 初始配置

创建数据库

DROP DATABASE if exists hibernate;
create database hibernate charset=utf8mb4;
use hibernate;
create table user(
    id int auto_increment primary key,
    name varchar(10),
    gender varchar(10),
    age int,
    birthday date
)

Maven 导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>hibernate</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.31.Final</version>
        </dependency>
    </dependencies>
</project>

创建实体类

Hibernate 初始配置_第1张图片

package cn.edu.hrbust.entity;

import java.sql.Date;
import java.util.Objects;

/**
 * @ClassName UserEntity
 * @Description
 * @Author Shen
 * @Date 2021/5/10 22:28
 * @Version 1.0
 **/
public class User {
     
    private int id;
    private String name;
    private String gender;
    private Integer 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 Integer getAge() {
     
        return age;
    }

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

    public Date getBirthday() {
     
        return birthday;
    }

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

    @Override
    public boolean equals(Object o) {
     
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User that = (User) o;
        return id == that.id && Objects.equals(name, that.name) && Objects.equals(gender, that.gender) && Objects.equals(age, that.age) && Objects.equals(birthday, that.birthday);
    }

    @Override
    public int hashCode() {
     
        return Objects.hash(id, name, gender, age, birthday);
    }
}

创建主配置文件

注意数据库配置



<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai&characterEncoding=utf8property>
        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
        <property name="connection.username">rootproperty>
        <property name="connection.password">zs2201property>
        <mapping resource="User.hbm.xml"/>
        
        
    session-factory>
hibernate-configuration>

创建映射文件



<hibernate-mapping>

    <class name="cn.edu.hrbust.entity.User" table="user" schema="hibernate">
        <id name="id">
            <column name="id" sql-type="int(11)"/>
        id>
        <property name="name">
            <column name="name" sql-type="varchar(10)" length="10" not-null="true"/>
        property>
        <property name="gender">
            <column name="gender" sql-type="varchar(10)" length="10" not-null="true"/>
        property>
        <property name="age">
            <column name="age" sql-type="int(11)" not-null="true"/>
        property>
        <property name="birthday">
            <column name="birthday" sql-type="date" not-null="true"/>
        property>
    class>
hibernate-mapping>

测试类

import cn.edu.hrbust.entity.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.sql.Date;

public class Main {
     
    public static void main(final String[] args) {
     
        User u = new User();
        u.setName("张三");
        u.setGender("男");
        u.setAge(21);
        u.setBirthday(Date.valueOf("2001-1-1"));
        Configuration cfg = null;
        SessionFactory sf = null;
        Session session = null;
        Transaction ts = null;
        try {
     
            cfg = new Configuration().configure();
            sf = cfg.buildSessionFactory();
            session = sf.openSession();
            ts = session.beginTransaction();
            session.save(u);
            ts.commit();
        } catch (HibernateException e) {
     
            e.printStackTrace();
            if (ts != null) {
     
                ts.rollback();
            }
        } finally {
     
            session.close();
            sf.close();
        }
    }
}

测试结果

Hibernate 初始配置_第2张图片

期间遇到的问题

保存'张三'时数据库乱码,解决: 数据库URL添加characterEncoding=utf8参数,注意XML中 & 转义为&

你可能感兴趣的:(Java)