首页 > 代码库 > 创建数据源两种方式

创建数据源两种方式

第一种:直接在applicationContext.xml中配置

    ......
    ......
<!--创建数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    ......
    ......

另外一种:配置properties文件。然后在applicationContext.xml读取,相对第一种方式,另外一种方式更为灵活。

properties文件:DbxoolConnection.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?

useUnicode=true&amp;characterEncoding=UTF-8 db.user=root db.password=root db.alias=QZCDBPool db.houseKeepingTestSql=select 1 db.characterEncoding=UTF-8 db.maximumConnectionCount=100 db.minimumConnectionCount=10

applicationContext.xml文件:

    ......
    ......
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
            <list>
                <value> 
                classpath:DbxoolConnection.properties
                </value>
            </list>
           </property>
        </bean> 


     <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> 
        <property name="driver" value=http://www.mamicode.com/"${db.driver}"/>
        <property name="driverUrl" value=http://www.mamicode.com/"${db.url}"/>
        <property name="user" value=http://www.mamicode.com/"${db.user}"/>
        <property name="password" value=http://www.mamicode.com/"${db.password}"/>
        <property name="alias" value=http://www.mamicode.com/"${db.alias}"/>
        <property name="houseKeepingTestSql" value=http://www.mamicode.com/"${db.houseKeepingTestSql}"/>
        <property name="maximumConnectionCount" value=http://www.mamicode.com/"${db.maximumConnectionCount}"/>
        <property name="minimumConnectionCount" value=http://www.mamicode.com/"${db.minimumConnectionCount}"/>
        <property name="delegateProperties" value=http://www.mamicode.com/"characterEncoding=${db.characterEncoding}"/>
     </bean>    
       ......
       ......

需注意导入org.logicalcobwebs.proxool.ProxoolDataSource引用的包:proxool-0.9.1.jar

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

创建数据源两种方式