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

来源:互联网 发布:编程从入门到精通 编辑:程序博客网 时间:2024/06/05 08:14

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

<?xml version='1.0' encoding='UTF-8'?>  <!DOCTYPE hibernate-mapping PUBLIC   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   <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

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>        <session-factory>         <!-- 数据库驱动名称 -->         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>         <!-- 数据库链接地址 -->         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>         <!-- 数据库用户名称 -->         <property name="hibernate.connection.username">root</property>         <!-- 数据库密码 -->         <property name="connection.password">root</property>         <!-- 设置数据库连接池默认个数 -->         <!-- <property name="connection.pool_size">1</property> -->         <!-- 设置数据库SQL语言类型 -->         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>         <property name="hbm2ddl.auto">update</property>         <!-- 设置是否显示SQL语句-->         <property name="show_sql">true</property>         <!-- 设置是否格式化SQL语句 -->         <!-- <property name="format_sql">true</property> -->         <!-- 设置使用线程-->         <property name="current_session_context_class">thread</property>         <!-- 设置hibernate的映射文件-->         <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();    }}

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