IDEA 下 hibernate 反向生成实体和映射文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38589021/article/details/80001743

IDEA 下 hibernate 反向生成实体和映射文件

选中项目,鼠标右键项目,选择Add Frameworks Support 或者 选中项目+F4


或者 选中项目+F4




现在看resources多了hibernate.cfg.xml

接下来配置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>
    <!--数据库连接url配置-->
    <property name="connection.url">jdbc:mysql://localhost:3306/disaster_tolerant</property>
    <!--数据库驱动配置-->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <!--数据库用户名配置-->
    <property name="connection.username">root</property>
    <!--数据库密码配置-->
    <property name="connection.password">123456</property>
  </session-factory>
</hibernate-configuration>

连接你的数据库

在Persistence中右键项目,然后点击Generate Persistence Mapping,选择By Database Schema


勾勾选选的,你懂的

最后生成的pojo文件给  大家 OB一哈

package com.nelsoner.pojo.test;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "tb_user", schema = "disaster_tolerant", catalog = "")
public class TbUser {
    private long userId;
    private String username;
    private String mobile;
    private String password;
    private Timestamp createTime;

    @Id
    @Column(name = "user_id", nullable = false)
    public long getUserId() {
        return userId;
    }

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

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

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

    @Basic
    @Column(name = "mobile", nullable = false, length = 20)
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

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

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

    @Basic
    @Column(name = "create_time", nullable = true)
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

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

        TbUser tbUser = (TbUser) o;

        if (userId != tbUser.userId) return false;
        if (username != null ? !username.equals(tbUser.username) : tbUser.username != null) return false;
        if (mobile != null ? !mobile.equals(tbUser.mobile) : tbUser.mobile != null) return false;
        if (password != null ? !password.equals(tbUser.password) : tbUser.password != null) return false;
        if (createTime != null ? !createTime.equals(tbUser.createTime) : tbUser.createTime != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (userId ^ (userId >>> 32));
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (createTime != null ? createTime.hashCode() : 0);
        return result;
    }
}

展开阅读全文

没有更多推荐了,返回首页