Spring Rdbms操作(一):注解方式注入数据源

DruidDataSource配置的连接池

配置如下:

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
         <!-- 数据库基本信息配置 -->
         <property name="url" value="${url}" />  
         <property name="username" value="${username}" />  
         <property name="password" value="${password}" />  
         <property name="driverClassName" value="${driverClassName}" />  
         <property name="filters" value="${filters}" />  
            <!-- 最大并发连接数 -->
         <property name="maxActive" value="${maxActive}" />
         <!-- 初始化连接数量 -->
         <property name="initialSize" value="${initialSize}" />
         <!-- 配置获取连接等待超时的时间 -->
         <property name="maxWait" value="${maxWait}" />
         <!-- 最小空闲连接数 -->
         <property name="minIdle" value="${minIdle}" />  
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
         <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
         <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />  
         <property name="validationQuery" value="${validationQuery}" />  
         <property name="testWhileIdle" value="${testWhileIdle}" />  
         <property name="testOnBorrow" value="${testOnBorrow}" />  
         <property name="testOnReturn" value="${testOnReturn}" />  
         <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
         <!-- 打开removeAbandoned功能 -->
         <property name="removeAbandoned" value="${removeAbandoned}" />
         <!-- 1800秒,也就是30分钟 -->
         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
         <!-- 关闭abanded连接时输出错误日志 -->   
         <property name="logAbandoned" value="${logAbandoned}" />
    </bean>  
DruidDataSource

思路:将ApplicationContext.xml中的<bean id="dataSource" ……/>

dataSource注入到dao层的进行Rdbms操作的代码中。

注解方式:

开启全局扫描:<context:component-scan base-package="com.*" />

注入代码的类代码如下:

import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.object.SqlFunction;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;

@Component(value = "springRdbms")
public class SpringRdbms {
    
    @Resource(name = "dataSource")
    private DruidDataSource dataSource;
    
    public int getSqlNum(String sql){
        @SuppressWarnings("rawtypes")
        SqlFunction sqlFunction = new SqlFunction(dataSource, sql);
        sqlFunction.compile();
        return sqlFunction.run();
    }
}
SpringRdbms

类上,@Component,@Service,@Repository都可。

具体区别:类注解

注入dataSource,@Resource,@Autowired都可。

推荐使用@Resource注解在字段上,这样就不用写setter方法了。并且这个注解是属于J2EE的,减少了与Spring
的耦合,这样代码看起就比较优雅 。

具体区别:bean注解

更多区别,请自行查询资料。

posted @ 2017-05-12 15:17  川雨淅  阅读(193)  评论(0编辑  收藏  举报