Hibernate 基础

首先我们根据数据库中表的字段来设置类的属性
package com.hibrnate;

public class UserTest {

private int id;
private String name;
private String sex;
private int age;

public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

} 

我们再来配置configuration.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/MySQL</property>
        <property name="connection.username">root</property>
        <property name="connection.password">huimie</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">mysql</property>
   
    <property name="connection.pool_size">2</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
   
    <mapping resource="com/hibrnate/table.hbm.xml"/>
   
    </session-factory>

</hibernate-configuration>

我们还要配置一个和java类并列目录中的table.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.hibrnate.UserTest" table="table3">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="name" column="name" type="string" />
<property name="sex" column="sex" type="string" />
<property name="age" column="age" type="integer" />
</class>
</hibernate-mapping>

最后自己写一个Test测试类(这里尽量用JUnit4来测试  简单  不用写main方法这么麻烦)
package com.hibrnate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

public static void main(String[] args) {
UserTest ut = new UserTest();
ut.setName("小萌萌");
ut.setSex("女");
ut.setAge(21);
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
session.save(ut);
transaction.commit();
}

} 

注意jar包的导入

简单hibrnate项目的基本目录


程序运行后hibrnate给你生成的插入语句

程序运行后table2表中生成的数据(最后一条)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值