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
- package com.hibernate.first.model;
- public class Student2 {
- private String id;
- private String carId;
- private String stuName;
- private Integer age;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getCarId() {
- return carId;
- }
- public void setCarId(String carId) {
- this.carId = carId;
- }
- public String getStuName() {
- return stuName;
- }
- public void setStuName(String stuName) {
- this.stuName = stuName;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- }
2. 编写实本映射文件Student.hbm.xml
现在有了这个实体类文件,还要让Hibernate知道这个资源,所以将这个类映射成.hbm.xml文件,让Hibernate可以使用它。Student.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.hibernate.first.model.Student2" table="student2"
- catalog="t_message"> <!-- 把类和数据表关联起来 -->
- <id name="id" unsaved-value="null">
- <generator class="uuid.hex" /> <!-- id产生方式是uuid.hex -->
- </id>
- <property name="carId" type="java.lang.String" length="45"/> <!-- 映射学号 -->
- <property name="stuName" type="java.lang.String" length="45"/> <!-- 映射姓名 -->
- <property name="age" type="java.lang.Integer" /> <!-- 映射年龄 -->
- </class>
- </hibernate-mapping>
3. 编写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">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="connection.username">root</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/t_message</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="connection.password">123456</property>
- <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
- <property name="show_sql">true</property>
- <property name="c3p0.min_size">1</property>
- <property name="c3p0.max_size">2</property>
- <property name="c3p0.timeout">600</property>
- <property name="myeclipse.connection.profile">conn_mysql</property>
- <mapping resource="com/hibernate/first/model/Student2.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
4.写一个测试文件BM2.java
源码如下:
- package com.hibernate.first;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hibernate.first.model.Student2;
- public class BM2 {
- public static void main(String[] args) {
- try{
- Configuration cfg = new Configuration().configure();
- SessionFactory sessions = cfg.buildSessionFactory();
- Session session = sessions.openSession();
- Transaction tx = session.beginTransaction();
- for(int i=0; i<10;i++){
- Student2 student = new Student2();
- student.setAge(20);
- student.setCarId("dfdfdf");
- student.setStuName("student");
- session.save(student);
- }
- tx.commit();
- session.close();
- }
- catch(HibernateException e){
- e.printStackTrace();
- }
- }
- }