- 浏览: 153337 次
- 性别:
- 来自: 杭州
文章分类
最新评论
1.准备条件:
1.1 创建hibernate2工程
1.2 导入hibernate包
1.3 创建hibernate2数据库,以及customers表
2. Customers.java
3. hibernate.cfg.xml
4.customers.hbm.xml
5. HibernateTest.java
1.1 创建hibernate2工程
1.2 导入hibernate包
1.3 创建hibernate2数据库,以及customers表
2. Customers.java
package com.hibernate2.bean; import java.sql.Date; import java.sql.Timestamp; public class Customers { private Long id; private String name; private String email; private String password; private Integer phone; private String address; private char sex; private boolean is_married; private String description; private byte[] image; private Date birthday; private Timestamp registered_time; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getPhone() { return phone; } public void setPhone(Integer phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public boolean isIs_married() { return is_married; } public void setIs_married(boolean isMarried) { is_married = isMarried; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public byte[] getImage() { return image; } public void setImage(byte[] image) { this.image = image; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Timestamp getRegistered_time() { return registered_time; } public void setRegistered_time(Timestamp registeredTime) { registered_time = registeredTime; } }
3. 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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <!-- <property name="format_sql">true</property> --> <property name="show_sql">true</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate2</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="customers.hbm.xml" /> </session-factory> </hibernate-configuration>
4.customers.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.hibernate2.bean.Customers" table="customers"> <id name="id" column="id" type="long"> <generator class="increment"></generator> </id> <property name="name" column="name" type="string"></property> <property name="email" column="email" type="string"></property> <property name="password" column="password" type="string"></property> <property name="phone" column="phone" type="integer"></property> <property name="address" column="address" type="string"></property> <property name="sex" column="sex" type="char"></property> <property name="is_married" column="is_married" type="boolean"></property> <property name="description" column="description" type="text"></property> <property name="image" column="image" type="binary"></property> <property name="birthday" column="birthday" type="date"></property> <property name="registered_time" column="registered_time" type="timestamp"></property> </class> </hibernate-mapping>
5. HibernateTest.java
package com.hibernate2.bean; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.sql.Date; import java.sql.Timestamp; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateTest { private static SessionFactory sessionFactory; static{ try{ sessionFactory=new Configuration().configure().buildSessionFactory(); }catch(Exception ex){ System.out.println("创建sessionFactory时出错"); } } private void saveCustomer(Customers cust){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); session.save(cust); tx.commit(); }catch(Exception ex){ System.out.println("保存数据时出错"); if(tx!=null){ tx.rollback(); } }finally{ session.close(); } } @SuppressWarnings("unchecked") private void findAllCustomer(PrintStream out){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); String hql="from Customers as c order by c.name asc"; Query query=session.createQuery(hql); List custs=query.list(); for(Iterator it=custs.iterator();it.hasNext();){ printCustomer((Customers)it.next(), out); } tx.commit(); }catch(Exception ex){ System.out.println("保存数据时出错"); if(tx!=null){ tx.rollback(); } }finally{ session.close(); } } private void loadAndUpdateCustoemr(Long id,String address){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); Customers cust=(Customers)session.load(Customers.class, id); cust.setAddress(address); tx.commit(); }catch(Exception ex){ System.out.println("保存数据时出错"); if(tx!=null){ tx.rollback(); } }finally{ session.close(); } } @SuppressWarnings("unchecked") private void deleteCutomer(Customers cust){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); List custs=session.createQuery("from Customers").list(); for(Iterator it=custs.iterator();it.hasNext();){ session.delete(it.next()); } tx.commit(); }catch(Exception ex){ System.out.println("保存数据时出错"); if(tx!=null){ tx.rollback(); } }finally{ session.close(); } } private void printCustomer(Customers cust,PrintStream out) throws Exception{ OutputStream output=new FileOutputStream("new_photo.gif"); byte[] buffer=cust.getImage(); output.write(buffer); out.println("--------------以下是"+cust.getName()+"的信息-----------------------"); out.println("ID:"+cust.getId()); out.println("name:"+cust.getName()); out.println("email:"+cust.getEmail()); out.println("password:"+cust.getPassword()); out.println("phone:"+cust.getPhone()); out.println("address:"+cust.getAddress()); out.println("sex:"+cust.getSex()); String married=cust.isIs_married()==true?"已婚":"未婚"; out.println("is_married:"+married); out.println("description:"+cust.getDescription()); out.println("birthday:"+cust.getBirthday()); out.println("registered_time:"+cust.getRegistered_time()); } private void test(PrintStream out) throws Exception{ Customers customer=new Customers(); customer.setName("zs"); customer.setEmail("zs@163.com"); customer.setPassword("123"); customer.setPhone(123456789); customer.setAddress("a new address"); customer.setSex('男'); customer.setIs_married(true); customer.setDescription("a new description"); InputStream in=this.getClass().getResourceAsStream("photo.gif"); byte[] buffer=new byte[in.available()]; in.read(buffer); customer.setImage(buffer); customer.setBirthday(Date.valueOf("1980-1-1")); saveCustomer(customer); // findAllCustomer(out); loadAndUpdateCustoemr(customer.getId(), "update address"); // deleteCutomer(customer); } public static void main(String[] args) throws Exception{ new HibernateTest().test(System.out); sessionFactory.close(); } }
发表评论
-
struts2.1改变配置文件默认位置
2011-03-31 14:01 752<filter> <init-par ... -
整合SSH2时需注意几点
2010-12-10 16:35 8191.需要导入struts2-spring-plugin-2.1 ... -
ssh2
2010-11-11 13:56 1828准备条件: 1. 建WEB工程:ssh2 2. 建数据库ssh ... -
spring整合hibernate
2010-11-08 08:59 802准备条件: 1. 创建数据库hibernatespring,表 ... -
代理模式
2010-11-08 08:58 815一. 静态代理 1. Subject.java packa ... -
单例模式
2010-11-08 08:57 6291. Singleton.java package com ... -
spring整合struts2
2010-11-08 08:56 716前言: 1. 创建工程strutsspring 2. 导入st ... -
反射案例
2010-11-08 08:56 684案例一: 1. Test.java package com ... -
spring配置文件中集合类型的配置
2010-11-08 08:54 926前言: Spring中对于有些bean会有集合类型的属性,以下 ... -
spring用到的设计模式-工厂模式
2010-11-08 08:53 1214Spring用到的设计模式之一 核心:用到工厂设计模式 1. ... -
读取applicationContext.xml的两种方式
2010-11-08 08:52 915第一种: ClassPathResource cpr=ne ... -
用图形查看spring配置文件依赖关系
2010-11-08 08:51 910myeclipse提供了这样的功能: window->s ... -
spring的基本操作IOC
2010-11-08 08:51 706前言: Spring通过IOC/DI来实现获取对象 准备: 1 ... -
spring的基本操作-反射
2010-11-08 08:50 1084前言: Spring通过反射来实现获取对象 准备: 1. 建S ... -
Struts2整合Hibernate
2010-10-29 19:00 19871. 准备条件: 1.1 在MYSQL中创建hibernate ... -
hibernate级联操作
2010-10-29 18:59 869准备条件: 1. 创建hibernate3工程 2. 导入hi ... -
JDBC
2010-10-29 18:56 737一. JDBC连接Access实例 1. 准备条件 1.1 创 ... -
文件上传
2010-10-26 14:49 6611. upload.jsp <%@ page lan ... -
调用指定Action的方法
2010-10-26 14:48 733主要在配置文件中配置下: 注:如应用于增删改查的类 1. st ... -
I18N
2010-10-26 14:47 775一, 查看当前操作系统中可用的语言 package com ...
相关推荐
Hibernate基本数据操作方法 java struts hibernate
关于Hibernate操作持久化类的Criteria方式的书本方法
利用Hibernate编写的基本例程,完成基本的CRUD操作
hibernate环境搭建基本操作封装,使用mysql开源数据库
hibernate hql基本操作
掌握Hibernate的基本功能和基本用法 通过Hibernate将一个Java对象插入到数据库表中 (1) Hibernate是一个面向Java环境的对象/关系数据库映射工具,Hibernate不仅管理Java类型到数据库表的映射,还提供查询数据和...
hibernate3.6.7中文版帮助手册,有了此帮助文档可以很好地让你学习和理解hibernate了。
这个类完全可以实现对数据库的基本操作;此类适用于hibernate4以上版本 ;hibernate4用hibernateSupport很容易引发一系列的问题,如果想避免的话,可以用此类
Hibernate映射的基本操作 one-to-many one-to-one的使用等
毕业设计航空售票系统 struts2+hibernate 基本实现了前台后台操作,DB设计,源代码
hibernate 的基础 常用的主键生成策略 持久化对象的三种状态 一级缓存的常用操作(save/update/saveOrUpdate/delete)一对多关联映射 级联操作
通过使用Strust2+Hibernate实现简单CRUD基本操作,有需要可以下载学习。
hibernate第一天的笔记,包含hibernate的基本操作,基本实现
Hibernate开发需要的8个基础包,其中包括了Hibernate包中没有的一个slf4j实现包,有了这8个基础包可以基本实现Hibernate的大众操作
使用hibernate封装方法,显现使用一个dao,service,impl来关联两个表的操作,实现,增删改查基本操作
这是一个加入了hibernate框架的空白项目,里面有hibernate必须的jar,简单的配置(配置了C3P0连接池),自己写的hibernate工具类,还有一个使用hibernate操作Student类的简单例子,很适合刚学习hibernate的童鞋拿来...
4 hibernate基础配置(重点) 5 ID生成策略(重点 AUTO) 6 Hibernate核心开发接口介绍(重点) ... 风格 1 先脉络,后细节 2 先操作,后原理 3 重Annotation,轻xml配置文件 ... 性能优化 1 注意...
第3章Hibernate中基本的数据库操作实现