【问题标题】:How to Define a MySql datasource bean via XML in Spring如何在 Spring 中通过 XML 定义 MySql 数据源 bean
【发布时间】:2012-09-21 08:54:33
【问题描述】:

我查看了定义 bean 的文档。我只是不清楚用于 Mysql 数据库的类文件。任何人都可以填写下面的bean定义吗?

<bean name="dataSource" class="">
    <property name="driverClassName" value="" />
    <property name="url" value="mysql://localhost/GameManager" />
    <property name="username" value="gamemanagertest" />
    <property name="password" value="1" />
</bean>

【问题讨论】:

    标签: spring javabeans datasource definition


    【解决方案1】:
    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/GameManager" />
        <property name="username" value="gamemanagertest" />
        <property name="password" value="1" />
    </bean>
    

    http://docs.spring.io/spring-data/jdbc/docs/1.1.0.M1/reference/html/orcl.datasource.html

    【讨论】:

    • 您的网址错误。我修好了它。那真的是用户设置的用户名和密码吗?不知何故,我对此表示怀疑。
    • 如果有人想使用连接池怎么办?
    • 销毁方法呢?
    【解决方案2】:

    使用此类 org.springframework.jdbc.datasource.DriverManagerDataSource - DriverManagerDataSource。作为最佳实践,如果我们将数据库值隔离到 .properties 文件中并将其配置到我们的 spring servlet xml 配置中,则效果会更好。在下面的示例中,属性存储为键值对,我们使用相应的key 访问value

    applicationContext-dataSource.xml

    <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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="connectionCachingEnabled" value="true"/>
    </bean>
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    

    jdbc.propeties 文件:

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/sample_db
    jdbc.username=root
    jdbc.password=sec3ret
    

    【讨论】:

    【解决方案3】:

    两个答案都适合该问题。但仅供参考,如果您打算使用 DriverManagerDataSource 作为数据源,每次调用您的数据源 bean 都会创建一个到数据库的新连接,不建议用于生产,甚至它也不会池化连接。

    如果您需要连接池,请考虑Apache Commons DBCP

    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/GameManager" />
        <property name="username" value="gamemanagertest" />
        <property name="password" value="1" />
        <property name="initialSize" value="2" />
        <property name="maxActive" value="5" />
    </bean>
    

    其中initialSizemaxActive 是池相关属性。

    要使用它,请确保您的路径中有required jar

    【讨论】:

    • 如果需要连接池,可以考虑HikariCP
    • 是的,一个不错的候选人:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多