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.Connection;
 5 import java.sql.SQLException;
 6 
 7 import javax.annotation.Resource;
 8 import javax.sql.DataSource;
 9 
10 import org.springframework.stereotype.Repository;
11 
12 import com.bxw.datasourse.dao.UserDao;
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 values(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 }

 有三种方式配置数据源:dbcp,c3p0,proxool

采用placeholder的方式配置数据源

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

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     <!-- 自动扫描 -->
21     <context:component-scan base-package="com.bxw.datasourse.*"></context:component-scan>
22     <!-- datasource -->
23     <bean id="myJdbcProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
24         <property name="location">
25             <value>classpath:jdbc.properties</value>
26         </property>
27     </bean>
28     <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
29         <property name="driverClassName" value="${jdbc.driverClassName}"></property>
30         <property name="url" value="${jdbc.url}"></property>
31         <property name="username" value="${jdbc.username}"></property>
32         <property name="password" value="${jdbc.password}"></property>
33     </bean>
34 </beans>

先配置jdbc.properties的bean指定它的路径。然后在datasource中用jdbc.xxxx的方式配置。

这样配置的方式便于管理,方便查找。

===========分界线==============

spring整合hibernate4配置文件

 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     <!-- 自动扫描 -->
21     <context:component-scan base-package="com.bxw.datasourse.*"></context:component-scan>
22     <!-- datasource -->
23     <bean id="myJdbcProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
24         <property name="location">
25             <value>classpath:jdbc.properties</value>
26         </property>
27     </bean>
28     <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
29         <property name="driverClassName" value="${jdbc.driverClassName}"></property>
30         <property name="url" value="${jdbc.url}"></property>
31         <property name="username" value="${jdbc.username}"></property>
32         <property name="password" value="${jdbc.password}"></property>
33     </bean>
34     
35     <bean id="sessionFactory" 
36     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
37         <property name="dataSource" ref="datasource"></property>
38         <property name="packagesToScan" value="com.bxw.vo" />
39         <property name="hibernateProperties">
40             <props>
41                 <prop key="hibernate.dialect">
42                     org.hibernate.dialect.MySQLDialect
43                 </prop>
44                 <prop key="hibernate.show_sql">true</prop>
45             </props>
46         </property>
47     </bean>
48 </beans>

将以上datasource注入sessionFactory中,自动扫描实体类,规定hibernate方言,将sql语句输出到控制台。

相同环境使用hibernate3中的AnnotationSessionFactoryBean配置时会报bean无法注入的错误,暂时未发现原因。

原文地址:https://www.cnblogs.com/popcornya/p/6965163.html