Spring笔记

本文探讨了在Spring框架中使用JDBC与Hibernate的性能比较,重点介绍了如何配置数据库连接池和使用jdbcTemplate。同时,详细讲解了Spring与JDBC的事务管理,包括设置MySQL的autocommit属性以及如何进行声明式事务管理。文章还列举了不同类型的事务管理器,如DataSourceTransactionManager和HibernateTransactionManager。
摘要由CSDN通过智能技术生成

msyql 完全清除表

truncate t_user;

性能比较: JDBC > hibernate(反射太多了,影响性能)

Spring 和 JDBC的使用 (相当于SqlHelper ,在没有使用hibernate的情况下)

  1. 配置数据库连接池 (单例 ),由我们的Spring管理一个全局唯一的数据库连接池
  2. 使用jdbcTemplate (和HibernateTemplate类似 )
  3. 使用c3p0
  4. applicationContext.xml
<bean id="dataSource" class=".....ComboPooledDataSource">
        <property name=""></property>
    </bean>

让Spring帮我们setter setter setter ,也可以自己通过编程new 这个DataSource

完整配置:

<

?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">


    <!-- 自动扫描与装配bean -->
    <context:component-scan base-package="cn.itcast.spring.p_jdbc"></context:component-scan>


    <!-- 加载外部的配置文件 -->
    <context:property-placeholder location="classpath:cn/itcast/spring/p_jdbc/jdbc.properties"/>


    <!-- 一、配置数据库连接池 ComboPooledDataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 基本的连接信息 -->
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="user" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <!-- 一些管理的配置 -->
        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="3"></property>
        <!--连接池中保
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值