*IntelliJ IDEA配置Hibernate

为IntelliJ IDEA安装Hibernate插件

*IntelliJ IDEA配置Hibernate

新建项目,勾选Web Application和hibernate,貌似不用选Web Application呀.

*IntelliJ IDEA配置Hibernate

*IntelliJ IDEA配置Hibernate

*IntelliJ IDEA配置Hibernate

项目建好以后配置数据源(首先要建立一个数据库,数据库必须已经存在).

可以新建个package用来放IDEA帮你生成的类(实体类),一般是xx.xx.model.

配置Persistence.(数据库中的表已经存在了,由表来反推出实体以及映射信息)

点击【Persistence】,在图示位置右键,选择让IDEA帮你根据数据库结构生成hibernate map配置。

*IntelliJ IDEA配置Hibernate

导入数据表结构

1、选择刚才建立的数据源.

2、可以把刚才建的包填到Package的位置,用来放自动生成的实体类.

3、刷新一下,下面框框里会出现数据库里所有的表和视图.

4、勾选要导入的表结构

这样选中的表对应的实体就生成好了。

*IntelliJ IDEA配置Hibernate

这样就不需要映射文件了,当然也可以按照以前的方式来.

映射信息都已经以注解的形式包含在实体类信息中了.

@Entity
@Table(name = "friend", schema = "hibernate", catalog = "")
public class FriendEntity {
    private long id;
    private String name;
    private Byte age;

    @Id
    @Column(name = "id", nullable = false)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name", nullable = true, length = 20)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "age", nullable = true)
    public Byte getAge() {
        return age;
    }

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

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

        FriendEntity that = (FriendEntity) o;

        if (id != that.id) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;
        if (age != null ? !age.equals(that.age) : that.age != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }
}

*IntelliJ IDEA配置Hibernate

配置hibernate.cfg.xml.

参考以前学习hibernate的配置,自动生成的少了很多.

<hibernate-configuration>
    <session-factory>      <!--配置数据源的时候填了相关信息,这里会自动帮我们生成-->
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <mapping class="com.test.FriendEntity"/>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>
上一篇:Nim积解法小结


下一篇:线性回归与岭回归参数向量公式推导