• spring 整合四种数据源



    <!--pom.xml引入jar包-->
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
    </dependency>


    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties"/>

    <!--配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的-->
    <!--第一种:使用spring自带的DriverManagerDataSource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
    </bean>


    <!--第二种:使用c3p0连接池; -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
    <!-- c3p0连接池的私有属性 -->
         <property name="maxPoolSize" value="30"/>
         <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
         <property name="autoCommitOnClose" value="false"/>
         <!-- 获取连接超时时间 -->
         <property name="checkoutTimeout" value="10000"/>
         <!-- 当获取连接失败重试次数 -->
         <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!--第三种:使用dbcp连接池; -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3309/sampledb" />
      <property name="username" value="root" />
      <property name="password" value="1234" />
    </bean>

    <!--第四种:使用JNDI连接池;-->

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <!-- Tomcatの例 -->
        <value>java:comp/env/jdbc/TerasolunaSampleDataSource</value>
        <!-- Tomcat以外のAPサーバの例 -->
        <!-- <value>jdbc/TerasolunaSampleDataSource</value> -->
      </property>
    </bean>

        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/>
            <property name="typeAliases" value="com.kuang.pojo.User"/>
        </bean>
    
        <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
    
    
    </beans>
  • 相关阅读:
    Ubuntu 12.04 安装scribe 的笔记 | 动漫驿站
    boost install on prinse 12.04 ubuntu
    basic coder » linux下获取当前程序的绝对路径
    spring 使用 groovy 的 utf8 问题
    string转化大小写(C++) | Vimer的程序世界
    linux常用命令一
    STL map与Boost unordered_map 有何不可的日志 网易博客
    本博使用的vim(gvim)相关插件整理
    【转】C++11中值得关注的几大变化 奔向C++ C++博客
    Lua 语言和C/C++集成调研小结
  • 原文地址:https://www.cnblogs.com/gaoyangliu/p/12354981.html
Copyright © 2020-2023  润新知