马士兵Spring-dataSource

一、简单使用例子:

这里使用commons.dbcp:

beanx.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
           
    <context:annotation-config />
    <context:component-scan base-package="com.cy"/>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
      <property name="username" value="root"/>
      <property name="password" value="root"/>
    </bean>
    
    <bean id="logInteceptor" class="com.cy.aop.LogInterceptor"></bean>
    
    <aop:config>
        <aop:aspect id="logAspect" ref="logInteceptor">
            <aop:before method="before" pointcut="execution(public * com.cy.service..*.add(..))"/>
        </aop:aspect>
    </aop:config>
    
 
</beans>

userdao实现类UserDAOImpl.java:

将dataSource注入到dao的实现:

package com.cy.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Component;

import com.cy.dao.UserDAO;
import com.cy.model.User;

@Component
public class UserDAOImpl implements UserDAO {
    
    //resource默认注入方式by name
    @Resource
    private DataSource dataSource;
    
    public void save(User user) {
        try {
            Connection conn = dataSource.getConnection();
            conn.createStatement().executeUpdate("insert into user values (null, 'zhangsan', '123456')");
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        System.out.println("user saved!");
    }

}

其他类,和上一章中使用的一摸一样;

引入dbcp相关jar包、mysql驱动包:

测试代码:

package com.cy.service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cy.model.User;

public class UserServiceTest {
    
    @Test
    public void testAdd() throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        UserService service = (UserService)ctx.getBean("userService");
        System.out.println(service.getClass());
        service.add(new User());
        ctx.destroy();
    }
}
View Code

console:

查看数据库插入成功:

二、把上面的改成配置文件的方式:

beans.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
           
    <context:annotation-config />
    <context:component-scan base-package="com.cy"/>
    
    <!-- 如果下面使用到了占位符的方式,请在locations下面去找 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean> 
</beans>

在classpath下面新建jdbc.properties:

jdbc.properties:

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

                            

原文地址:https://www.cnblogs.com/tenWood/p/6865085.html