美文网首页Java学习笔记
IDEA自动生成Hibernate配置文件

IDEA自动生成Hibernate配置文件

作者: 梧桐树下挨过打 | 来源:发表于2017-06-22 12:51 被阅读3538次

    一、新建工程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;
        }
    }
    

    相关文章

      网友评论

        本文标题:IDEA自动生成Hibernate配置文件

        本文链接:https://www.haomeiwen.com/subject/zvxfcxtx.html