JdbcTemplate的使用

本文介绍如何通过配置JdbcTemplate实现项目中多个数据源的简化配置,并提供了配置文件示例,包括使用PropertyPlaceholderConfigurer加载配置、设置BoneCP数据源属性及注入JdbcTemplate实例。演示如何在业务类中使用JdbcTemplate进行数据查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候项目中可能要配置多个数据源,可能配置的时候比较麻烦,这个时候可以直接配置JdbcTemplate他来用,这样相对配置简单点,

下面是我的配置文件:

<?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:mybatis="http://mybatis.org/schema/mybatis-spring"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
	http://mybatis.org/schema/mybatis-spring 
	http://mybatis.org/schema/mybatis-spring.xsd"
  default-autowire="byName">

  <bean id="pwmisPropertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:properties/jdbc.properties</value>
        <value>classpath:properties/jdbc.properties</value>
        <value>classpath:properties/bonecp.properties</value>
      </list>
    </property>
  </bean>

  <bean id="pwmisDataSource" class="com.jolbox.bonecp.BoneCPDataSource"
    destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="idleConnectionTestPeriod" value="${bonecp.idleConnectionTestPeriod}" />
    <property name="idleMaxAge" value="${bonecp.idleMaxAge}" />
    <property name="maxConnectionsPerPartition" value="${bonecp.maxConnectionsPerPartition}" />
    <property name="minConnectionsPerPartition" value="${bonecp.minConnectionsPerPartition}" />
    <property name="partitionCount" value="${bonecp.partitionCount}" />
    <property name="acquireIncrement" value="${bonecp.acquireIncrement}" />
    <property name="statementsCacheSize" value="${bonecp.statementsCacheSize}" />
    <property name="releaseHelperThreads" value="${bonecp.releaseHelperThreads}" />
  </bean>
  

  <bean id="pwmisTransactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="pwmisDataSource" />
  </bean>
  
  <bean id="pJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
	<property name="dataSource" ref="pwmisDataSource" /> 
  </bean>
  
   <!-- 配置dao --> 
   <bean id="pPdDao" class="com.wwy.PdDao" />
  
   
   <!-- 配置业务bean -->
   <bean id="pdMigration" class="com.wwy.PdMigration" /> 
   

</beans>
这个就可以在上面注入的dao类中使用了,pPdDao类:

public class PwmisPdDao{
	
	private JdbcTemplate pJdbcTemplate;

	public List<Map<String,Object>> getList() {
		String sql = "select * from pd";
		
		List<Map<String,Object>> rows = pJdbcTemplate.queryForList(sql);
		
		return rows;
	}

	public JdbcTemplate getpJdbcTemplate() {
		return pJdbcTemplate;
	}

	public void setpJdbcTemplate(JdbcTemplate pJdbcTemplate) {
		this.pJdbcTemplate = pJdbcTemplate;
	}
	
}
这样可以通过上面的类中的getList来获取到表pd中的数据了。

当然这只是JdbcTemplate他的查询,他还可以完成insert、update、delete、分页等等。下面就不记录。用的时候继续看API吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值