1. 程式人生 > >Intellij Idea Hibernate 環境搭建

Intellij Idea Hibernate 環境搭建

  1. 新建hibernate專案
  2. 新增mysql-connector
    • ctrl+alt+shift+s lib add mysql-connector

  3. 建立實體類
    • src/entity/User.java
        package entity;
    
        public class User {
            //hibernate 要求實體類有一個屬性唯一
            private int uid;
            private String username;
            private String password;
            private String address;
    
            public int getUid() {
                return uid;
            }
    
            public void setUid(int uid) {
                this.uid = uid;
            }
    
            public String getUsername() {
                return username;
            }
    
            public void setUsername(String username) {
                this.username = username;
            }
    
            public String getPassword() {
                return password;
            }
    
            public void setPassword(String password) {
                this.password = password;
            }
    
            public String getAddress() {
                return address;
            }
    
            public void setAddress(String address) {
                this.address = address;
            }
        }
  4. 建立src/rntity/User.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: 實體類的全路徑
                - table: 資料庫表名稱
            - id
                - name: 實體類的id屬性名稱
                - column: 生成表的欄位名稱
            - generator
                - class
                    - native: 生成表主鍵id自增長
            - property
                - name: 屬性名
                - column: 欄位名
        -->
        <class name="entity.User" table="t_user">
            <id name="uid">
                <generator class="native"/>
            </id>
            <property name="username"/>
            <property name="password"/>
            <property name="address"/>
        </class>
    </hibernate-mapping>
  1. 配置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>
            <!--第一部分: 配置資料庫資訊-->
            <!--ctrl+alt+shift+s lib add mysql-connector-->
            <property name="connection.url">
                jdbc:mysql://127.0.0.1:3306/hibernate_test?useUnicode=true&amp;characterEncoding=utf-    8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC
            </property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.username">root</property>
            <property name="connection.password">passwd</property>
            <!--第二部分: 可選-->
            <!-- DB schema will be updated if needed -->
            <property name="hbm2ddl.auto">update</property>
            <!--輸出sql語句, 並格式化-->
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
            <!--各個資料庫的方言-->
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <!--第三部分: 對映核心檔案-->
            <mapping resource="entity/User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
  1. 修改Main.java
    import entity.User;
    import org.hibernate.*;
    import org.hibernate.cfg.Configuration;
    
    
    public class Main {
        private static final SessionFactory ourSessionFactory;
    
        static {
            try {
                //載入核心配置檔案
                Configuration configuration = new Configuration();
                configuration.configure();
    
                //建立SessionFactory物件, 並在資料庫中把表創建出來
    
                ourSessionFactory = configuration.buildSessionFactory();
            } catch (Throwable ex) {
                throw new ExceptionInInitializerError(ex);
            }
        }
    
        public static Session getSession() throws HibernateException {
            //建立Session
            return ourSessionFactory.openSession();
        }
    
        public static void main(final String[] args) throws Exception {
            final Session session = getSession();
    
            try {
                User user = new User();
                user.setUsername("小李");
                user.setPassword("123");
                user.setAddress("日本");
                session.save(user);
                session.beginTransaction().commit();
            } finally {
                session.close();
            }
        }
    }
  1. 執行Main.java
  2. 注意事項
    <property name="connection.url">
                jdbc:mysql://127.0.0.1:3306/hibernate_test?useUnicode=true&amp;characterEncoding=utf-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC
    </property>
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <mapping resource="entity/User.hbm.xml"/> 要放到最後