hibernate demo入门

此demo项目采用hibernate3.2 

new 一个普通的java项目,引入hibernate所需要的jar包:核心jar  hibernate3.jar,其他依赖jar  /lib 下  ,mysql 驱动jar 

 

hibernate 核心配置文件 :默认就放到classpath根目录下(src根目录)即可

hibernate.cfg.xml

注意:session-factory 元素中不要配置 name 属性值,否则会报如下异常

Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  Java.naming.factory.initial

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
	     <!--
	     #hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
	     #hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
	      -->
	    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	    <property name="hibernate.connection.url">jdbc:mysql://localhost:3316/hibernatedemo?useUnicode=true&characterEncoding=UTF-8</property>
	    <property name="hibernate.connection.username">root</property>
	    <property name="hibernate.connection.password">winmshl</property>
	    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	    <property name="hibernate.hbm2ddl.auto">update</property>
	    <property name="show_sql">true</property>
		
		<mapping resource="com/jelly/hibernatedemo/entity/User.hbm.xml"/>
	 
	</session-factory>
</hibernate-configuration>

User.hbm.xml 映射文件:

<?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.jelly.hibernatedemo.entity">

	<class name="User" >
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
	    <property name="birthday"/>
		 
	</class>
	
</hibernate-mapping>

实体bean :

package com.jelly.hibernatedemo.entity;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private Date birthday;

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Date getBirthday() {
		return birthday;
	}

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

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

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
				+ "]";
	}

}



一个简单的demo类(测试类)

package com.jelly.hibernatedemo.main;

import java.util.Date;

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

import com.jelly.hibernatedemo.entity.User;

public class MainTest {
    public static void main(String[] args) {
    	try {
    		Configuration  conf=new Configuration();
    		SessionFactory sessionFactory= conf.configure().buildSessionFactory();
    		Session session=sessionFactory.openSession();
    		Transaction tx=session.beginTransaction();
    		User user=new User() ;
    		user.setName("张三");
    		user.setBirthday(new Date());
    		session.save(user);
    		tx.commit();
    		session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值