MySQL与Hibernate的整合使用

Hibernate作为一个ORM工具,与数据库是分不开的。下面是一个MySQL与Hibernate的整合使用的一个例子。

 

1.编写实体对象文件

    1.1  建立数据库t_message,里面有一张表Student2表。

           建表语句如下:

                    CREATE TABLE `student2` (
                           `id` varchar(100) NOT NULL,
                           `stuName` varchar(20) default '',
                            `carId` varchar(20) NOT NULL default '',
                              `age` varchar(4) default ''
                             ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    1.2  编写一个简单的实体对象文件,通过Hibernate来操作这个数据表,文件名字叫Student2.java

          文件放在:/HibernateProject/src/com/hibernate/first/model

        

  1. package com.hibernate.first.model;
  2. public class Student2 {
  3.       private String id;
  4.       private String carId;
  5.       private String stuName;
  6.       private Integer age;
  7.     public String getId() {
  8.         return id;
  9.     }
  10.     public void setId(String id) {
  11.         this.id = id;
  12.     }
  13.     public String getCarId() {
  14.         return carId;
  15.     }
  16.     public void setCarId(String carId) {
  17.         this.carId = carId;
  18.     }
  19.     public String getStuName() {
  20.         return stuName;
  21.     }
  22.     public void setStuName(String stuName) {
  23.         this.stuName = stuName;
  24.     }
  25.     public Integer getAge() {
  26.         return age;
  27.     }
  28.     public void setAge(Integer age) {
  29.         this.age = age;
  30.     }
  31.     
  32. }

2.  编写实本映射文件Student.hbm.xml

    现在有了这个实体类文件,还要让Hibernate知道这个资源,所以将这个类映射成.hbm.xml文件,让Hibernate可以使用它。Student.hbm.xml的源码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <!-- 
  5.     Mapping file autogenerated by MyEclipse Persistence Tools
  6. -->
  7. <hibernate-mapping>
  8.     <class name="com.hibernate.first.model.Student2" table="student2"
  9.         catalog="t_message">                                <!-- 把类和数据表关联起来 -->
  10.         <id name="id" unsaved-value="null">
  11.             <generator class="uuid.hex" />                  <!-- id产生方式是uuid.hex -->
  12.         </id>
  13.         <property name="carId" type="java.lang.String" length="45"/>    <!-- 映射学号 -->
  14.         <property name="stuName" type="java.lang.String" length="45"/>  <!-- 映射姓名 -->
  15.         <property name="age" type="java.lang.Integer" />                <!-- 映射年龄 -->
  16.     
  17.     </class>
  18. </hibernate-mapping>

 

3.  编写Hibernate配置文件hibernate.cfg.xml

     源码如下:

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <!-- Generated by MyEclipse Hibernate Tools.                   -->
  6. <hibernate-configuration>
  7. <session-factory>
  8.     <property name="connection.username">root</property>      
  9.     <property name="connection.url">jdbc:mysql://localhost:3306/t_message</property>
  10.     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  11.     <property name="connection.password">123456</property>
  12.     <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
  13.     <property name="show_sql">true</property>
  14.     <property name="c3p0.min_size">1</property>
  15.     <property name="c3p0.max_size">2</property>
  16.     <property name="c3p0.timeout">600</property>
  17.     <property name="myeclipse.connection.profile">conn_mysql</property>
  18.     <mapping resource="com/hibernate/first/model/Student2.hbm.xml" /> 
  19. </session-factory>
  20. </hibernate-configuration>

 

4.写一个测试文件BM2.java

  源码如下:

  1. package com.hibernate.first;
  2. import org.hibernate.HibernateException;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.cfg.Configuration;
  7. import com.hibernate.first.model.Student2;
  8. public class BM2 {
  9.     public static void main(String[] args) {
  10.        try{
  11.            Configuration  cfg = new Configuration().configure();
  12.            SessionFactory sessions = cfg.buildSessionFactory();
  13.            Session session = sessions.openSession();
  14.            Transaction tx = session.beginTransaction();
  15.            
  16.            for(int i=0; i<10;i++){
  17.                Student2 student = new Student2();
  18.                student.setAge(20);
  19.                student.setCarId("dfdfdf");
  20.                student.setStuName("student");
  21.                session.save(student);
  22.            }
  23.            tx.commit();
  24.            session.close();
  25.        }
  26.        catch(HibernateException e){
  27.            e.printStackTrace();
  28.        }
  29.     }
  30. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值