仅实现了保存用户(几个字段)的小实例,初步感受hibernate!!!
先定义一个用户类User
public class User { private int id; private String name; private Date birthday; 。。。省略get和set。。。
定义User类的映射文件User.hbm.xml
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Hibernate映射文件的根元素 --> <hibernate-mapping package="com.xx.hibernate.domain"> <class name="User" table="sysuser"> <!-- 映射标识属性 --> <id name="id" column="id"> <!-- 指定主键生成策略 --> <generator class="native"/> </id> <!-- 映射普通属性 --> <property name="name" column="name" /> <property name="birthday" column="birthday" /> </class> </hibernate-mapping>
定义hibernate配置文件hibernate.cfg.xml
<!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">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">dzpmxt</property> <property name="connection.password">dzpmxt</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/xx/hibernate/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
以上,就能通过hibernate将User对象保存到数据库的sysuser表中了
定义一个测试类Base
public class Base { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure(); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); User user = new User(); user.setBirthday(new Date()); user.setName("用户名"); s.save(user); tx.commit(); s.close(); System.out.println("end"); } }
---完---
相关推荐
hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例
这是hibernate框架的一个简单的 容易理解的实例,算是hibernate学习入门的hello world实例吧,适合初学者
hibernate 入门详细操作步骤 包括hibernate包,实例实现文档····
hibernate入门实例,内容是增加人员信息实例,手动配置相关配置文件、pojo类,测试数据。后台数据采用mysql数据库,有实体表创建语句,相关hibernate的jar包与数据库驱动包也上传了,可以直接使用,很方便!
hibernate入门实例 hibernate入门实例 hibernate入门实例
Hibernate入门案例,提供MyEclipse和Eclipse的项目源文件以及war文件
hibernate入门实例DOC,经典入门势力
Struts & Hibernate入门实例
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
Eclipse快速上手Hibernate之入门实例
java web Hibernate 快速入门实例
hibernate入门实例的具体操作,包括配置文件的手工和由eclipse生成两种方法
myeclipse下hibernate入门实例介绍
struts+hibernate入门实例,数据库用oracle 10g, 开发工具:Eclipse3.2.1
Hibernate入门实例源代码,eclipse工程,可直接运行。Hibernate入门实例源代码,eclipse工程,可直接运行。
webwork+spring+hibernate入门实例,使用mysql数据库,建一个名为imti的数据库,把数据库文件导入数据库 把imti文件直接导入myeclipse
ajax+hibernate入门实例源码。测试方法: 一、先在mysql数据库中创建表并插入数据(脚本在压缩包中) 二、将WebRoot目录复制到tomcat的webapps中。 运行图: <br>
Struts+Hibernate入门实例(eclipse版)
基于SpringMVC以及Spring,Hibernate的非注解入门小demo
struts2+hibernate 入门 学习 包含对学生信息的增删查改,包含数据库test.sql,放到项目下能够 运行的实例