日期:2014-05-16  浏览次数:20466 次

spring连接数据库的两种方式

1,将仿问数据库连接的住处放到spring.xml中

<?xml version="1.0" encoding="UTF-8"?>

?

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

xmlns:tx="http://www.springframework.org/schema/tx"

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-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

//连接mysql数据库

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close" >

<property name="driverClassName" value="com.mysql.jdbc.Driver">

</property>

<property name="url" value="jdbc:mysql://192.168.1.3:3306/mlxcdb">

</property>

<property name="username" value="root"></property>

<property name="password" value="mysql"></property>

</bean>

?

?

2,将仿问连接数据库连接的相关驱动信息放到jdbc.properties配置文件中

  1. <!--?使用属性文件配置数据源?-->??
  2. ?<bean?id="propertyPlaceholderConfigurer"?class=