14

ドキュメントを調べて、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>
4

3 に答える 3

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

于 2012-09-29T23:47:38.363 に答える
7

このクラスを使用しますorg.springframework.jdbc.datasource.DriverManagerDataSource- DriverManagerDataSource。ベスト プラクティスとして、データベースの値を.propertiesファイルに分離し、Spring Servlet xml 構成に構成することをお勧めします。以下の例では、プロパティはキーと値のペアとして保存されvalue、対応する を使用してにアクセスしますkey

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
于 2015-06-30T10:26:42.750 に答える