如何在 Spring 中通过 XML 定义 MySql 数据源 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12657503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Define a MySql datasource bean via XML in Spring
提问by Robert Greathouse
I've looked over the documentation to define a bean. I'm just unclear on what class file to use for a Mysql database. Can anyone fill in the bean definition below?
我查看了文档以定义一个 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>
采纳答案by TechnocratSid
Both the answers are appropriate for the question. But just for an FYI if you're going to use DriverManagerDataSourceas your datasource, every call to your datasource bean will create a new connection to your database which is not recommended for production and even it does not pool connections.
这两个答案都适合这个问题。但仅供参考,如果您打算使用DriverManagerDataSource作为您的数据源,每次调用您的数据源 bean 都会创建一个到您的数据库的新连接,这不推荐用于生产,甚至它不池连接。
If you need a connection pool, consider Apache Commons DBCP.
如果您需要连接池,请考虑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>
Where initialSizeand maxActiveare pooling related properties.
其中initialSize和maxActive是池化相关属性。
To use this make sure you have the required jarin your path.
要使用它,请确保您的路径中有所需的 jar。
回答by Robert Greathouse
<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
http://docs.spring.io/spring-data/jdbc/docs/1.1.0.M1/reference/html/orcl.datasource.html
回答by Lucky
Use this class org.springframework.jdbc.datasource.DriverManagerDataSource- DriverManagerDataSource. As a best practice its better if we isolate the database values into a .propertiesfile and configure it into our spring servlet xml configuration. In the below example the properties are stored as key-value pairs and we access the valueusing the corresponding key.
使用此类org.springframework.jdbc.datasource.DriverManagerDataSource- DriverManagerDataSource。作为最佳实践,最好将数据库值隔离到一个.properties文件中并将其配置到我们的 spring servlet xml 配置中。在下面的示例中,属性存储为键值对,我们value使用相应的key.
applicationContext-dataSource.xml:
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.propetiesfile:
jdbc.propeties文件:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sample_db
jdbc.username=root
jdbc.password=sec3ret

