Two ways for spring to connect to the database

1. Put the residence of the imitation database connection in 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">

//connect to mysql database

<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. Put the relevant driver information about the connection to the database connection in the jdbc.properties configuration file

  1. <!-- Use properties file to configure data source -->  
  2.  <beanid="propertyPlaceholderConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  3.     <propertyname="location"value="jdbc.properties"/>    
  4.  </bean>  
  5.  <beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"destroy-method="close">     
  6.     <propertyname="driverClassName"value="${jdbc.driverClassName}"/>    
  7.     <propertyname="url"value="${jdbc.url}"/>    
  8.     <propertyname="username"value="${jdbc.username}"/>    
  9.     <propertyname="password"value="${jdbc.password}"/>    
  10.  </bean>  

 

//connect to mysql database

jdbc.driverClassName=com.mysql.jdbc.Driver

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

jdbc.username=root

jdbc.password=mysql

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326982544&siteId=291194637
Recommended