通过数据库来建立实体类

目的:通过数据库来建立实体类

描述:这是我在学习hibernate时,看到视频中的老师通过数据库映射到实体类,觉得很有用,所以写成博客。

(1) 建表

先在数据库里建一张表,然后使用idea在配置文件里连接到数据库,这里我用到的是hibernate.cfg.xml

在这里插入图片描述

(2) 连接数据库

配置文件: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://localhost:3306/springboot?serverTimezone=UTC</property>

        <!-- DB schema will be updated if needed -->
        <!--     <property name="hibernate.hbm2ddl.auto">update</property> -->
        <!--    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>-->

        <!-- Echo all executed SQL to stdout -->


        <property name="show_sql">true</property>

        <!-- validate | update | create | create-drop -->


        <!-- create:如果有表(有mapping配置的映射关系),则先删除表,再新建create 初始化表,清空数据 update:当表结构发生变化时,
        不会删除原有字段,只会新增字段,避免数据误删当没有表的时候,update会自动帮我们创建一张表 (类似create,但不同于create,不会删除原来的表) -->

        <!--     <property name="hbm2ddl.auto">create</property>-->


        <!-- 映射关系 -->


<!--        <mapping class="entity.TUserEntity"/>-->
<!--        <mapping resource="entity/TUserEntity.hbm.xml"/>-->

    </session-factory>

(3) 通过数据库把数据映射到实体类

在这里插入图片描述
使用Datebase连接到据库,怎么连接就不在赘述了
在这里插入图片描述

接下来打开persistance
在这里插入图片描述

在这里插入图片描述

现在实体类中已经映射到数据了
在这里插入图片描述
代码如下:

package entity;

import javax.persistence.*;

@Entity
@Table(name = "t_user", schema = "springboot", catalog = "")
public class TUserEntity {
    
    
    private int uid;
    private String username;
    private String password;

    @Id
    @Column(name = "uid", nullable = false)
    public int getUid() {
    
    
        return uid;
    }

    public void setUid(int uid) {
    
    
        this.uid = uid;
    }

    @Basic
    @Column(name = "username", nullable = false, length = 34)
    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }

    @Basic
    @Column(name = "password", nullable = false, length = 34)
    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TUserEntity that = (TUserEntity) o;

        if (uid != that.uid) return false;
        if (username != null ? !username.equals(that.username) : that.username != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
    
    
        int result = uid;
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_14930709/article/details/105137022
今日推荐