如何在Spring中通过XML定义MySql数据源bean [英] How to Define a MySql datasource bean via XML in Spring

查看:124
本文介绍了如何在Spring中通过XML定义MySql数据源bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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>

推荐答案

两个答案都适合该问题.但是,仅在FYI中,如果您要使用 DriverManagerDataSource 作为数据源,对数据源bean的每次调用都会创建与数据库的新连接,不建议将其用于生产,甚至不合并连接.

Both the answers are appropriate for the question. But just for an FYI if you're going to use DriverManagerDataSource as 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.

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

If you need a connection pool, consider 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>

其中 initialSize maxActive 是与池相关的属性.

Where initialSize and maxActive are pooling related properties.

要使用此功能,请确保您的必需的罐子小路.

To use this make sure you have the required jar in your path.

这篇关于如何在Spring中通过XML定义MySql数据源bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆