Hibernate4 mysql 采用XML 配置方式入门例子

温故而知新, 既然hibernate4 已经出来了,就把以前hibernate3 的例子重新修改一下,再跑一遍,检验下有什么新的变化, 以前没有记录笔记的习惯,现在记录下来.

1. 创建测试用的表
程序代码 程序代码

-- ----------------------------
Drop TABLE IF EXISTS `stock`;
Create TABLE `stock` (
  `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `STOCK_CODE` varchar(10) NOT NULL,
  `STOCK_NAME` varchar(20) NOT NULL,
  PRIMARY KEY (`STOCK_ID`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of stock
-- ----------------------------


Hibernate4 所需要的jar包

程序代码 程序代码

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.4.Final.jar
hibernate-core-4.3.0.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
mysql-connector-java-5.1.18-bin.jar


3. 创建工程,java工程或者动态web工程都可以
所需要的实体类: Stock.java
程序代码 程序代码

package com.yihaomen.hibernate.domain;

/**
* Model class for Stock
*/
public class Stock implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private Integer stockId;
    private String stockCode;
    private String stockName;

    public Stock() {
    }

    public Stock(String stockCode, String stockName) {
        this.stockCode = stockCode;
        this.stockName = stockName;
    }

    public Integer getStockId() {
        return this.stockId;
    }

    public void setStockId(Integer stockId) {
        this.stockId = stockId;
    }

    public String getStockCode() {
        return this.stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    public String getStockName() {
        return this.stockName;
    }

    public void setStockName(String stockName) {
        this.stockName = stockName;
    }

}





4. Hiberante 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.yihaomen.hibernate.domain.Stock" table="stock">
        <id name="stockId" type="java.lang.Integer">
            <column name="STOCK_ID" />
            <generator class="identity" />
        </id>
        <property name="stockCode" type="string">
            <column name="STOCK_CODE" length="10" not-null="true" unique="true" />
        </property>
        <property name="stockName" type="string">
            <column name="STOCK_NAME" length="20" not-null="true" unique="true" />
        </property>
    </class>
</hibernate-mapping>



5. Hibernate4 配置文件
程序代码 程序代码

<?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="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yihaomen-hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="com/yihaomen/hibernate/domain/Stock.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>


注意数据库的配置,修改成满足自己的。

6. 配置 Hibernate4 工具类
参考前面一篇文章:http://www.yihaomen.com/article/java/453.htm

7. 写测试代码,保存数据到数据库
程序代码 程序代码

package com.yihaomen.hibernate4.test;

import org.hibernate.Session;

import com.yihaomen.hibernate.domain.Stock;
import com.yihaomen.hibernate.util.HibernateUtil;

public class AppTest
{  
     public static void main( String[] args )
        {
            System.out.println("Hibernate + MySQL");
            Session session = HibernateUtil.getSessionFactory().openSession();
            
            session.beginTransaction();
            Stock stock = new Stock();
            
            stock.setStockCode("4715");
            stock.setStockName("GENM");
            
            session.save(stock);
            session.getTransaction().commit();
        }

}


8. 查看运行结果
程序代码 程序代码

...
一月 05, 2014 3:04:24 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: insert into stock (STOCK_CODE, STOCK_NAME) values (?, ?)


查看数据库,运行成功.
源代码下载, 省去了jar 包: 下载文件 hibernate4 mysql xml source code download

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
[本日志由 轻舞肥羊 于 2014-01-05 03:08 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: hibernate
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.