码迷,mamicode.com
首页 > 编程语言 > 详细

Spring整合hibernate

时间:2015-07-30 19:21:01      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1. Spring 整合 Hibernate 整合什么 ?

1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
2). 让 Hibernate 使用上 Spring 的声明式事务

2. 整合步骤:

1). 加入 hibernate
①. jar 包
②. 添加 hibernate 的配置文件: hibernate.cfg.xml
③. 编写了持久化类对应的 .hbm.xml 文件。

2). 加入 Spring
①. jar 包
②. 加入 Spring 的配置文件

3). 整合.

3. 编写代码

 

一:配置hibernate

  1、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 <hibernate-configuration>
 6     <session-factory>
 7         <!-- 配置 hibernate 的基本属性 -->
 8         <!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 -->
 9         <!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 -->
10         <!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. -->
11         
12         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
13         <property name="hibernate.show_sql">true</property>
14         <property name="hibernate.format_sql">true</property>
15         
16         <property name="hibernate.hbm2ddl.auto">true</property>
17         <!-- 配置 hibernate 二级缓存相关的属性. -->
18     </session-factory>
19 </hibernate-configuration>

  2.持久化对象   使用hibernate TOOL生成了

 1 package com.jxsm.entities;
 2 
 3 public class UName {
 4     private int id;
 5     private String name;
 6     private String isbn;
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public String getIsbn() {
20         return isbn;
21     }
22     public void setIsbn(String isbn) {
23         this.isbn = isbn;
24     }
25      
26 }
 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2015-7-30 17:29:41 by Hibernate Tools 3.4.0.CR1 -->
 5 <hibernate-mapping>
 6     <class name="com.jxsm.entities.UName" table="UNAME">
 7         <id name="id" type="int">
 8             <column name="ID" />
 9             <generator class="identity" />
10         </id>
11         <property name="name" type="java.lang.String">
12             <column name="NAME" />
13         </property>
14         <property name="isbn" type="java.lang.String">
15             <column name="ISBN" />
16         </property>
17     </class>
18 </hibernate-mapping>

 未完!!

Spring整合hibernate

标签:

原文地址:http://www.cnblogs.com/img-zoom/p/4623146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!