`
dawuafang
  • 浏览: 1107044 次
文章分类
社区版块
存档分类
最新评论

JdbcTemplate的使用

 
阅读更多

有时候项目中可能要配置多个数据源,可能配置的时候比较麻烦,这个时候可以直接配置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吧。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics