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

<?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();
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值