IDEA 自動生成Hibernate實體類和Mapping文件


一、新建工程Demo(如果選的時候勾選了hibernate,IDEA會自動下載Hibernate包,不需要手動導入)
新建工程
二、導入相關包 Mysql && Hibernate

Hibernate包含的包

導入Mysql && Hibernate包

Hibernate包含的包
三、添加Hibernate

圖片.png

圖片.png
四、自動生成配置文件
1.連接數據庫(View-Tool Windows-datebase)

如果test connection是灰色的,仔細看Driver這個位置,需要導入mysql的包,IDEA可以直接下載。或者手動導入

圖片.png
2.選擇Persistence

Generate Persistence Mapping

設置

核心配置文件設置mysql的用戶名和密碼

五、主函數

主函數
六、運行結果

插入成功

相關代碼:

UserEntity.hbm.xml(自動生成)

<?xml version='1.0' encoding='utf-8'?>
<!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="com.war.hibernate.UserEntity" table="user" schema="demo">
        <id name="id" column="id"/>
        <property name="username" column="username"/>
        <property name="password" column="password"/>
        <property name="address" column="address"/>
    </class>
</hibernate-mapping>

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.url">jdbc:mysql://localhost:3306/demo</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>
    <mapping class="com.war.hibernate.UserEntity"/>
    <!--配置文件連接到核心文件-->
    <mapping resource="com/war/hibernate/UserEntity.hbm.xml"/>
    <!-- DB schema will be updated if needed -->
    <!-- 設置數據庫創建表的策略 -->
     <!--<property name="hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

TestMain.java

package com.war.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * Created by Administrator on 2017/6/22.
 */
public class TestMian {
    public static void main(String[] args) {
        //1.加載HIbernate核心配置文件
        Configuration cfg = new Configuration();
        cfg.configure();
        //2.使用sessionFactory
        SessionFactory sessionFactory = cfg.buildSessionFactory();
        //3使用sessionfactory 實例化session對象
        Session session = sessionFactory.openSession();
        //4.開始事務
        Transaction transaction = session.beginTransaction();
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername("Susan1");
        userEntity.setPassword("12345");
        userEntity.setAddress("TaiBei");
        //5.調用 session 保存 數據。
        session.save(userEntity);
        //6.commit transaction
        transaction.commit();
        //7.close  Database resources
        session.close();
        sessionFactory.close();

    }

}

UserEntity.java(自動生成)

package com.war.hibernate;

import javax.persistence.*;

/**
 * Created by Administrator on 2017/6/22.
 */
@Entity
@Table(name = "user", schema = "demo", catalog = "")
public class UserEntity {
    private int id;
    private String username;
    private String password;
    private String address;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

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

    @Basic
    @Column(name = "username")
    public String getUsername() {
        return username;
    }

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

    @Basic
    @Column(name = "password")
    public String getPassword() {
        return password;
    }

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

    @Basic
    @Column(name = "address")
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

        UserEntity that = (UserEntity) o;

        if (id != that.id) 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;
        if (address != null ? !address.equals(that.address) : that.address != null) return false;

        return true;
    }

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

原文:http://www.jianshu.com/p/c920962872d3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM