Intellij Idea Hibernate 环境搭建

Posted twfb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"/> 要放到最后

以上是关于Intellij Idea Hibernate 环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

Intellij Idea Hibernate 环境搭建

Intellij IDEA下的第一个Hibernate项目

IntelliJ IDEA使用hibernate

Intellij Idea 15 下新建 Hibernate 项目以及如何添加配置

Intellij Idea 用Maven 创建Hibernate 项目

无法在 IntelliJ IDEA 中生成 JPA Hibernate 元模型类