首页 > 代码库 > Spring datasource

Spring datasource

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
 3     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
 4     xmlns:cache="http://www.springframework.org/schema/cache"  
 5     xsi:schemaLocation="  
 6     http://www.springframework.org/schema/context  
 7     http://www.springframework.org/schema/context/spring-context.xsd  
 8     http://www.springframework.org/schema/beans  
 9     http://www.springframework.org/schema/beans/spring-beans.xsd  
10     http://www.springframework.org/schema/tx  
11     http://www.springframework.org/schema/tx/spring-tx.xsd  
12     http://www.springframework.org/schema/jdbc  
13     http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
14     http://www.springframework.org/schema/cache  
15     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
16     http://www.springframework.org/schema/aop  
17     http://www.springframework.org/schema/aop/spring-aop.xsd  
18     http://www.springframework.org/schema/util  
19     http://www.springframework.org/schema/util/spring-util.xsd">
20     <!-- 初始化bean -->  
21     <context:component-scan base-package="com.bxw.datasourse.*"></context:component-scan>
23     <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
24         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
25         <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
26         <property name="username" value="root"></property>
27         <property name="password" value="root"></property>
28     </bean>
29 </beans>

driverClassName:驱动;

url:路径;

username:数据库登录账号;

password:数据库登录密码。

 

dao:

1 package com.bxw.datasourse.dao;
2 
3 import javax.sql.DataSource;
4 
5 public interface UserDao {
6     public void saveUser();
7 }

impl:

 1 package com.bxw.datasourse.daoimpl;
 2 
 3 
 4 import java.sql.SQLException;
 5 
 6 import javax.annotation.Resource;
 7 import javax.sql.DataSource;
 8 
 9 import org.springframework.stereotype.Repository;
10 
11 import com.bxw.datasourse.dao.UserDao;
12 import com.mysql.jdbc.Connection;
13 @Repository("userDaoImpl")
14 public class UserDaoImpl implements UserDao {
15     private    DataSource datasource;
16     @Resource
17     public void setDatasource(DataSource datasource) {
18         this.datasource = datasource;
19     }
20     
21     public void saveUser() {
22         try {
23             Connection conn = (Connection) datasource.getConnection();
24             conn.createStatement().executeUpdate("insert into user value(null,zhangsan)");
25         } catch (SQLException e) {
26             e.printStackTrace();
27         }
28         System.out.println("user saved");
29     }
30 
31 }

ioc注入datasource

 

Test

 1 package com.bxw.datasourse.daoimpl;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import junit.framework.TestCase;
 7 
 8 public class UserDaoImplTest extends TestCase {
 9 
10     public void testSaveUser() {
11         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
12         UserDaoImpl udi = (UserDaoImpl) ac.getBean("userDaoImpl");
13         udi.saveUser();
14     }
15 
16 }

 

Spring datasource