hibernate相关配置----配置文件形式配置实体

1.下载相关jar包

下载路径:http://hibernate.org/orm/
jar包路径:\hibernate-release-5.2.10.Final\lib\required
加数据库连接jar:mysql-jdbc.jar

2.完事具备

写实体User为例
/hibernate_test/src/com/test/domin/User.java

package com.test.domin;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

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

    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;
    }

}

写配置文件
/hibernate_test/src/com/test/domin/User.hbm.xml

  
  

 <hibernate-mapping>  
    <class name="com.test.domin.User" table="user">  
        <id name="id" column="id" type="java.lang.Integer">  
            <generator class="native">generator>  
        id>  
        <property name="username" column="username" type="string">property>  
        <property name="password" column="password" type="string">property>  
    class>  
hibernate-mapping>  

写配置文件
/hibernate_test/config/hibernate.cfg.xml


 

 <hibernate-configuration>
        <session-factory>

         
         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
         
         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testproperty>
         
         <property name="hibernate.connection.username">rootproperty>
         
         <property name="connection.password">rootproperty>
         
         
         
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>

         <property name="hbm2ddl.auto">updateproperty>

         
         <property name="show_sql">trueproperty>
         
         
         
         <property name="current_session_context_class">threadproperty>

         
         <mapping  resource="com/test/domin/User.hbm.xml"/>

     session-factory>
 hibernate-configuration>

写测试类
/hibernate_test/test/test/HibernateTest.java

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.test.domin.User;

public class HibernateTest {
    @Test
    public void testHibernateEnv() {
        //加载配置文档
        Configuration conf = new Configuration();
        conf.configure("hibernate.cfg.xml");
        //创建工厂  
        SessionFactory sf = conf.buildSessionFactory();
        //取得session  
        Session session = sf.openSession();
        //开始事务  
        session.beginTransaction();
        User user = new User();
        user.setUsername("test");
        user.setPassword("123");
        session.save(user);
        System.out.println("保存成功");
        session.getTransaction().commit();
        session.close();
        sf.close();
    }
}

大功告成,快去试试吧。
备注:新手一个,不喜勿喷,欢迎交流。

你可能感兴趣的:(hibernate学习笔记)