建表sql
create table customers(
id bigint primary key,
name varchar(20) not null,
pass varchar(20) not null,
email varchar(128) not null,
image mediumblob,
birthday date,
registered_time timestamp
);
Customer
package org.zbq.bean;
import java.sql.Date;
import java.sql.Timestamp;
public class Customer {
private Long id;
private String name;
private String pass;
private String email;
private byte[] image;
private Date birthday;
private Timestamp registeredTime;
public Timestamp getRegisteredTime() {
return registeredTime;
}
public void setRegisteredTime(Timestamp registeredTime) {
this.registeredTime = registeredTime;
}
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 getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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;
}
}
Customer.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="org.zbq.bean.Customer" table="customers">
<id name="id" type="long">
<generator class="increment"></generator>
</id>
<property name="name" type="string" not-null="true"/>
<property name="pass" type="string" not-null="true"/>
<property name="email" type="string" not-null="true"></property>
<property name="image" type="binary"></property>
<property name="birthday" type="date"></property>
<property name="registeredTime" column="registered_time" type="timestamp" />
</class>
</hibernate-mapping>
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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">mysqladmin</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="show_sql">true</property>
<mapping resource="org/zbq/bean/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Test
package org.zbq.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.sql.Date;
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;
import org.zbq.bean.Customer;
public class HibernateTest {
public static SessionFactory sessionFactory;
static {
try{
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void findAll() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Query query = session.createQuery("from Customer as c order by c.name asc");
List<Customer> customers = query.list();
for(Iterator<Customer> it = customers.iterator(); it.hasNext();){
Customer c = it.next();
// System.out.println(c.getName());
printCustomer(System.out, c);
}
tx.commit();
}catch(Exception e){
if(tx != null){
tx.rollback();
}
throw e;
}
}
public static void saveCustomer(Customer customer) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(customer);
tx.commit();
}catch(Exception e){
if(tx != null){
tx.rollback();
}
throw e;
}
}
public static void printCustomer(PrintStream out, Customer customer) throws Exception{
byte[] buff = customer.getImage();
OutputStream fout = new FileOutputStream("p_copy" + customer.getId() + ".jpg");
fout.write(buff);
fout.close();
out.println("ID:" + customer.getId());
out.println("Name:" + customer.getName());
out.println("Email:" + customer.getEmail());
out.println("Birthday:" + customer.getBirthday());
out.println("RegisteredTime:" + customer.getRegisteredTime());
fout.close();
}
public static void updateCustomer(long id, byte[] image) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Customer c = (Customer) session.load(Customer.class, id);
c.setImage(image);
tx.commit();
}catch (Exception e) {
if(tx != null){
tx.rollback();
}
throw e;
}
}
@SuppressWarnings("unchecked")
public static void deleteCustomer(long id) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Query query = session.createQuery("from Customer");
List<Customer> lists = query.list();
for(Customer c : lists){
if(c.getId() == id){
session.delete(c);
}
}
tx.commit();
}catch (Exception e) {
if(tx != null){
tx.rollback();
}
throw e;
}
}
public void test(PrintStream out) throws Exception{
Customer customer = new Customer();
customer.setBirthday(Date.valueOf("2013-1-12"));
customer.setEmail("Jack@gmail.com");
customer.setName("Jack");
customer.setPass("pass@123");
// customer.setRegisteredTime(registeredTime)
InputStream in = new FileInputStream(new File("WebRoot/images/028.jpg"));
// InputStream in = this.getClass()
// .getResourceAsStream("003.jpg");
// byte[] buff = new byte[in.available()];
// in.read(buff);
byte[] image = new byte[in.available()];
in.read(image);
customer.setImage(image);
saveCustomer(customer);
// updateCustomer(6, image);
// findAll();
// deleteCustomer(1);
in.close();
}
public static void main(String[] args) throws Exception {
new HibernateTest().test(System.out);
File file = new File("WebRoot/images/004.jpg");
System.out.println(file.exists());
// HibernateTest t = new HibernateTest();
// System.out.println(t.getClass().getResourceAsStream(file.getAbsolutePath()));
}
}
分享到:
相关推荐
【Hibernate 基础】是Java开发中一个重要的部分,主要关注如何将对象模型与关系数据库进行映射,以简化数据库操作。本PPT由传智播客制作,旨在帮助学习者掌握Hibernate的基础知识和应用。 首先,我们需要理解基于B/...
hibernate基础jar包。包含junit,antlr,don4j,hibernate-commons-annotations,hibernate-core,hibernate-jpa-api,javassit,jboss-logging,jboss-transaction-api
在本"Hibernate基础包"中,包含了用于SSH开发所需的全部核心库,使得开发者能够快速搭建项目并进行数据库交互。 首先,Hibernate的核心功能在于它的ORM(对象关系映射)机制。ORM允许开发者通过Java对象来操作...
本篇文章将深入探讨`Hibernate基础jar包`的构成,以及它们在Java Hibernate框架中的作用。 首先,Hibernate的核心jar包是实现ORM功能的基础。这些jar包包括但不限于以下: 1. **hibernate-core.jar**:这是...
本资源“Hibernate基础学习源码”提供了五个不同阶段的学习示例,分别命名为Hibernate_01至Hibernate_04以及Hibernate_M2M,涵盖了Hibernate的基本概念、配置、实体映射、CRUD操作以及多对多关系的处理。 1. **...
### Hibernate基础知识点详解 #### 一、什么是对象关系映射(ORM)以及为什么使用ORM? 在企业级应用开发中,持久层(persistence layer)占据了非常重要的地位。它主要负责处理与数据库之间的交互,包括数据的...
在本项目中,"springboot+hibernate基础项目"是一个使用Spring Boot和Hibernate框架构建的典型Java Web应用。Spring Boot简化了Spring的配置过程,而Hibernate则是一个强大的ORM(对象关系映射)工具,使得数据库...
**Hibernate基础知识** 1. **什么是Hibernate**:Hibernate是一个开源的Java ORM框架,它简化了Java应用与关系数据库之间的交互。通过提供一套API,Hibernate可以将Java对象自动持久化到关系数据库中,反之亦然,...
**Hibernate基础全攻略** Hibernate,一个强大的Java对象关系映射(ORM)框架,极大地简化了数据库操作,使得开发者可以专注于业务逻辑而不是繁琐的SQL代码。本篇攻略将深入探讨Hibernate的基础知识,帮助初学者...
**Hibernate 框架概述** Hibernate 是一个开源的对象关系映射(ORM)框架,它允许开发者用面向对象的方式来处理数据库操作。ORM 解决了在 Java 应用程序中使用传统 JDBC 进行数据库操作时遇到的繁琐代码问题,将...
### Hibernate基础知识点详解 #### 一、Hibernate简介与优势 **Hibernate** 是一款开源的对象关系映射(ORM)框架,它允许开发者以面向对象的方式来处理数据库操作,从而简化了Java应用与关系型数据库之间的交互。...
这个“Hibernate基础架包”包含了进行Hibernate开发所需的基本库文件,使得开发者可以快速地将其集成到自己的项目中,无需手动配置大量的依赖。 在Hibernate中,核心概念包括: 1. **实体(Entities)**:实体代表...
**Hibernate基础教程** Hibernate是一个强大的Java对象关系映射(ORM)框架,它简化了数据库与Java应用程序之间的数据交互。这个基础教程将引导我们逐步了解Hibernate的核心概念和使用方法。 **一、Hibernate概述*...
《Hibernate基础教程》是针对Java开发人员的一本经典教材,主要涵盖了如何使用Hibernate这一流行的ORM(对象关系映射)框架来简化数据库操作。配套代码包"Bh3 2nd Ed Source Code"则提供了实例代码,帮助读者更好地...