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="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
  <property name="connection.username">root</property>
  <property name="connection.password">root</property>
  <property name="dialect">
   org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>

  <mapping resource="Person.hbm.xml" />
 </session-factory>

</hibernate-configuration>

 

Person.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>
 <class name="com.messi.model.Person" table="_person">
  <id name="id" column="id" type="int">
   <generator class="increment"></generator>
  </id>
  <property name="username" type="string"></property>
  <property name="password" type="string"></property>
  <property name="gender" type="character"></property>
  <property name="birthday" type="date"></property>
  <property name="graduation" type="boolean"></property>
  <property name="telephone" type="long"></property>
  <property name="marryTime" type="timestamp"></property>
  <property name="file" type="binary"></property>
 </class>
</hibernate-mapping>

 

Person.java

package com.messi.model;

import java.sql.Date;
import java.sql.Timestamp;

public class Person {
 private int id;
 private String username;
 private String password;
 private char gender;
 private Date birthday;
 private boolean graduation;
 private long telephone;
 private Timestamp marryTime;
 private byte[] file;

 public int getId() {
  return id;
 }

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

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public char getGender() {
  return gender;
 }

 public void setGender(char gender) {
  this.gender = gender;
 }

 public Date getBirthday() {
  return birthday;
 }

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

 public boolean isGraduation() {
  return graduation;
 }

 public void setGraduation(boolean graduation) {
  this.graduation = graduation;
 }

 public long getTelephone() {
  return telephone;
 }

 public void setTelephone(long telephone) {
  this.telephone = telephone;
 }

 public Timestamp getMarryTime() {
  return marryTime;
 }

 public void setMarryTime(Timestamp marryTime) {
  this.marryTime = marryTime;
 }

 public byte[] getFile() {
  return file;
 }

 public void setFile(byte[] file) {
  this.file = file;
 }

}

 

Test.java

package com.messi.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Timestamp;
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 com.messi.model.Person;

@SuppressWarnings("deprecation")
public class Test {

 public static void main(String[] args) {
  SessionFactory sf;
  Transaction tx = null;
  try {
   sf = new Configuration().configure().buildSessionFactory();
   Session session = sf.openSession();
   tx = session.beginTransaction();
   Person person = new Person();
   person.setUsername("zhangsan");
   person.setPassword("123");
   person.setGender('男');
   person
     .setBirthday(new java.sql.Date(new java.util.Date()
       .getTime()));
   person.setGraduation(false);
   person.setTelephone(1234567890);
   person.setMarryTime(new Timestamp(new java.util.Date().getTime()));
   InputStream is = new FileInputStream("d:/html_zh_CN.zip");
   byte[] buffer = new byte[is.available()];
   is.read(buffer);
   person.setFile(buffer);

   session.save(person);
   tx.commit();

   tx = session.beginTransaction();
   Query query = session.createQuery("from Person");
   List<Person> list = (List<Person>) query.list();
   for (Person p : list) {
    System.out.println(p.getId());
    FileOutputStream fos = new FileOutputStream("c:/html_zh_CN"
      + p.getId() + ".zip");
    fos.write(p.getFile());
   }
   tx.commit();

  } catch (Exception e) {
   if (null != tx) {
    tx.rollback();
   }
   e.printStackTrace();
  }

 }
}


 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值