JdbcTemplate的使用
来源:互联网 发布:解决问题 知乎 编辑:程序博客网 时间:2024/05/17 01:59
有时候项目中可能要配置多个数据源,可能配置的时候比较麻烦,这个时候可以直接配置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.xsdhttp://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吧。
0 0
- jdbctemplate的使用
- Spring的jdbcTemplate使用
- Spring的jdbcTemplate使用
- Spring的jdbcTemplate使用
- Spring的jdbcTemplate使用
- Spring的jdbcTemplate使用
- JdbcTemplate的使用
- Spring的jdbcTemplate使用
- jdbctemplate的使用
- JdbcTemplate的使用
- jdbcTemplate的简单使用
- JDBCTemplate的使用
- 使用spring的jdbcTemplate
- JdbcTemplate的使用
- JDBCTemplate的使用详解
- Spring的JdbcTemplate的使用
- JdbcTemplate代码的部分使用
- 使用jdbcTemplate查询的三种回调
- Linq学习笔记
- ios何时使用self.
- Android DDMS中真机测试时data目录无法显示的处理办法
- bitmap 软引用,弱引用
- Oracle INITRANS和MAXTRANS
- JdbcTemplate的使用
- 并查集
- math.random()函数
- 嵌套矩形
- Java编程思想笔记——第十四章&&第十五章
- 字符串去空格以及反转操作示例
- 利用VIS插件调用后台数据库显示动态流程图
- 剑指offer 面试题5 从尾到头打印链表(递归实现)
- notepad++ 使用心得