hibernate---“helloword”程序

刚学习了hibernate,写了第一个程序,很简单,和大家交流下,也提供给药学习的hibernate的朋友参考下。
我的开发环境:Eclipse3.4+Mysql
1.首先Eclipse中新建一个Java project,新建一个Java类,取名user,其实就是一个valuebean,一共有三个属性,id,name,password,加上set,get方法。这个就算是OK了
内容如下:

package com.test.hibernate;

public class User {
private int id;
private String name;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}


2.新建映射文件,*.hbm.xml.要和user放在一个包下。
内容如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.hibernate">
<class name="User">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="password"></property>
</class>
</hibernate-mapping>


3.在src目录下新建hibernate配置文件hibernate.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">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<!-- 标明使用的是哪种数据库 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!--数据表是否存在,不存在则建立-
create-drop 测试使用,不存建立,关闭则删除表
create 新建,关闭不删除
update 更新数据表。不会删除
validate 校验,对象和数据关系是否对应,不对应报异常
-->
<property name="hbm2ddl.auto">create</property>
<!-- 映射文件路径 -->
<mapping resource="com/test/hibernate/userMapping.hbm.xml"/>

</session-factory>
</hibernate-configuration>

4.导入hibernate的jar包。我的前一篇文章讲过了hibernate包的配置,哎这里我就不多说了。
5.在新建一个java类。主要是测试用。
Test.java;
内容如下:
package com.test.main;

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

import com.test.hibernate.User;
public class TestHibernate {

/**
* @param args
*/
public static void main(String[] args) {

Configuration cfg = new Configuration();
SessionFactory sf =null;
Session session=null;
User user = new User();
Transaction ta=null;
try{
cfg.configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();
ta= session.beginTransaction();

user.setName("wqy1");
user.setPassword("1234");

session.save(user);
ta.commit();
}catch(HibernateException he){
if(ta!=null)
ta.rollback();//事件回滚
throw he;//抛出异常
}finally{
session.close();
}

System.out.println("success--"+user.getId()+"=="+user.getName()+"=="+user.getPassword());
}

}

6.好了,一切OK,现在运行程序,试试效果吧 :D
-----------------------------------------------------------------[quote]本人也是刚学习,有些地方可能不是很合适,希望大家一起交流,多多指正。 :idea: [/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值