码迷,mamicode.com
首页 > Web开发 > 详细

Intellij Idea Hibernate 环境搭建

时间:2018-12-09 16:48:47      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:字段名   prope   add   uil   char   turn   show   commit   ble   

  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 环境搭建

标签:字段名   prope   add   uil   char   turn   show   commit   ble   

原文地址:https://www.cnblogs.com/edhg/p/10090826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!