读书人

spring连接数据库的两种形式

发布时间: 2013-08-01 15:23:18 作者: rapoo

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

<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="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">??
  3. ????<property?name="location"?value="jdbc.properties"/>??
  4. ?</bean>??
  5. ?<bean?id="dataSource"?class="org.springframework.jdbc.datasource.DriverManagerDataSource"?destroy-method="close">??
  6. ????<property?name="driverClassName"?value="${jdbc.driverClassName}"/>??
  7. ????<property?name="url"?value="${jdbc.url}"/>??
  8. ????<property?name="username"?value="${jdbc.username}"/>??
  9. ????<property?name="password"?value="${jdbc.password}"/>??
  10. ?</bean>??

?

//连接mysql数据库

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://192.168.1.3:3306/mlxcdb

jdbc.username=root

jdbc.password=mysql

?

读书人网 >其他数据库

热点推荐