整合hibernate

整合什么?

  1. 有ioc容器来管理hibernate的SessionFactory
  2. 让hibernate使用上spring的声明式事务

先加入hibernate 驱动包

新建hibernate.cfg.xml

配置hibernate的基本属性

  1. 数据源需配置到IOC 容器中,所以在此处不再需要配置数据源
  2. 关联的.hbm.xml也在IOC 容器配置SessionFactory实例时进行配置。
  3. 配置hibernate的基本属性:方言,sql的显示及格式化,生成数据表的策略以及二级缓存等。

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="hibernate.show_sql">true</property>

<property name="hibernate.format_sql">true</property>

<property name="hibernate.hbm2ddl.auto">update</propert

Ctrl+shift+T打开源码文件

在加入spring

Db.properties

jdbc.user=root

jdbc.password=root

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring

配置数据源

需导入

xmlns:context="http://www.springframework.org/schema/context"

<!-- 配置数据源 -->

<context:property-placeholder location="classpath:db.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="user" value="${jdbc.user}"></property>

<property name="password" value="${jdbc.password}"></property>

<property name="driverClass" value="${jdbc.driverClass}"></property>

<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>

<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>

</bean>

测试类测试能否拿到datasource

public class Go {

private static ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

public static void main(String[] args) throws SQLException {

DataSource d= ctx.getBean(DataSource.class);

System.out.println(d.getConnection());

}

}

通过spring来操作hibernate且使用spring的事务

Spring的applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

<property name="url"

value="jdbc:mysql://localhost:3306/spring?userUnicode=true&amp;characterEnchoding=utf-8">

</property>

<property name="username" value="root"></property>

<property name="password" value="root"></property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<!-- 指向hibernate的配置文件 -->

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

<property name="mappingLocations" value="classpath:com/ssh/spring_hibernate/entity/*.hbm.xml"></property>

</bean>

<bean id="transactionManager"

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!-- 配置事务属性 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="get*" read-only="true"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice>

<!-- 配置事务的切点 -->

<aop:config>

<aop:pointcut expression="execution(* com.ssh.spring_hibernate.service.*.*(..))"

id="txPointcut"/>

<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

</aop:config>

<tx:annotation-driven transaction-manager="transactionManager" /></beans>

两个Javabean

Account

private Integer id;

private int balance;

private String username;

Book

private Integer id;

private String bookName;

private String isbn;

private int stock;

private int price;

对应的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">

<!-- Generated 2015-8-28 12:55:21 by Hibernate Tools 3.4.0.CR1 -->

<hibernate-mapping>

<class name="com.ssh.spring_hibernate.entity.Account" table="SH_ACCOUNT">

<id name="id" type="java.lang.Integer">

<column name="ID" />

<generator class="native" />

</id>

<property name="username" type="java.lang.String">

<column name="USER_NAME" />

</property>

<property name="balance" type="int">

<column name="BALANCE" />

</property>

</class>

</hibernate-mapping>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2015-8-28 12:55:21 by Hibernate Tools 3.4.0.CR1 -->

<hibernate-mapping>

<class name="com.ssh.spring_hibernate.entity.Book" table="SH_BOOK">

<id name="id" type="java.lang.Integer">

<column name="ID" />

<generator class="native" />

</id>

<property name="bookName" type="java.lang.String">

<column name="BOOK_NAME" />

</property>

<property name="isbn" type="java.lang.String">

<column name="ISBN" />

</property>

<property name="price" type="int">

<column name="PRICE" />

</property>

<property name="stock" type="int">

<column name="STOCK" />

</property>

</class>

</hibernate-mapping>

Spring笔记④--spring整合hibernate链接数据库的更多相关文章

  1. Spring 学习笔记之整合Hibernate

    Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQ ...

  2. Spring学习7-Spring整合Hibernate

    一.Springl为什么要整合Hibernate   二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...

  3. Spring Data初步--整合Hibernate

    Spring Data课程中的技术介绍 Hibernate: Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系 ...

  4. Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  5. Spring笔记——Spring框架简介和初次框架配置

    Spring简介 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Deve ...

  6. Spring学习笔记之整合hibernate

    1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</par ...

  7. Spring再接触 整合Hibernate

    首先更改配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  8. Spring笔记——Spring+JDBC组合开发

      使用Spring+JDBC集成步骤如下:   1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...

  9. Hibernate(链接数据库方便得多)!

    首先让我们看一下配置文件,我这里先是用struts搞得controller,不明白struts的可以去百度一下这里就不讲解了: 之后我们需要做一个hibernate的配置文件内容如下(这里链接的是my ...

随机推荐

  1. 给button增加下划线

    如何给button增加下划线简单版   - (void)setUnderLineForButton:(UIButton *)btn withTitle:(NSString *)title{ //利用富 ...

  2. c++类模板之友元函数

    前言:自从开始学模板了后,小编在练习的过程中.常常一编译之后出现几十个错误,而且还是那种看都看不懂那种(此刻只想一句MMP).于是写了便写了类模板友元函数的用法这篇博客.来记录一下自己的学习. 普通友 ...

  3. inux下进程的最大线程数、进程最大数、进程打开的文件数

    inux下进程的最大线程数.进程最大数.进程打开的文件数 2008-12-07 23:48 =========================    如下转载自这里. linux 系统中单个进程的最大 ...

  4. 第十周课下作业-IPC

    第十周课下作业-IPC 题目:研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 共享内存 共享内存允许两个或多个进程进程共 ...

  5. PostgreSQL处理xml数据初步

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

  6. 1-[Mysql]-数据库介绍及安装

    1.数据库概述 1.数据库服务器: 运行数据库管理软件的计算机 2.数据库管理软件:mysql oracle db2 sqlserver 3.库:文件夹 4.表:文件 5.记录:事物的一系列典型特征: ...

  7. sqlyog 可视化查看数据库关系并创建外键

    + 一个架构设计器,把表拖进来即可查看数据库关系 如果要建立外键,需要在两个要被建立的外键之间进行操作(经过验证不需要都为主键),然后从用鼠标把子外键拖到父外键中,就可以关联上了. 参考: https ...

  8. android prgoressBar setProgressDrawable 在4.0系统式正常,在2.3系统上不能正常使用的问题

    上次在做一个电池电量的进度显示时,需要根据背景主题色来切换电池电量的进度的颜色, 但是在对prgoressBar的setProgressDrawable进行设置之后发现,在4.0系统上能够正常,而在2 ...

  9. AWK高端功能-数组

    第1章 awk命令基础 1.1 awk命令执行过程 1.如果BEGIN 区块存在,awk执行它指定的动作. 2.awk从输入文件中读取一行,称为一条输入记录.如果输入文件省略,将从标准输入读取 3.a ...

  10. MySQL ZIP Archive 5.7.17 安装方法

    1.下载 2.解压缩 3.创建/修改配置文件 在MySQL安装目录下,新建my.ini,内容如下参考 [mysql] # 设置mysql客户端默认字符集 default-character-set=u ...