【sping揭秘】19、关于spring中jdbctemplate中的DataSource怎么来呢
我們這是可以正好借助之前學(xué)的factorybean類,自己吧jdbctemplate加載到spring容器中,我們可以封裝多個(gè)這種對(duì)象,那么可以實(shí)現(xiàn)針對(duì)不同的數(shù)據(jù)庫(kù)的jdbctemplate
首先我們肯定要引入對(duì)應(yīng)的jar,來(lái)構(gòu)建數(shù)據(jù)源對(duì)象
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
根據(jù)這個(gè)我們簡(jiǎn)單的創(chuàng)建一個(gè)jdbctemplate對(duì)象
package cn.cutter.start.bean;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
/**
* 用來(lái)封裝第三方對(duì)象的類,加入spring容器
* @author xiaof
*
*/
@Component
public class JdbcTemplateFactoryTestBean implements FactoryBean<JdbcTemplate> {
@Override
public JdbcTemplate getObject() throws Exception {
BasicDataSource dataSource = new BasicDataSource();
//設(shè)置相應(yīng)的參數(shù)
//1、數(shù)據(jù)庫(kù)驅(qū)動(dòng)類
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//2、url,用戶名,密碼
dataSource.setUrl("jdbc:mysql://localhost:3306/liferay?characterEncoding=utf-8");
dataSource.setUsername("liferay"); dataSource.setPassword("xiaofeng2017");
//3、初始化連接大小
dataSource.setInitialSize(1);
//4、連接池最大數(shù)據(jù)量
dataSource.setMaxTotal(500);
//5、連接池最大小空閑
dataSource.setMinIdle(1);
dataSource.setMaxIdle(20);
//6、最大等待時(shí)間 單位毫秒
dataSource.setMaxWaitMillis(20 * 1000);
//7、指明連接是否被空閑連接回收器(如果有)進(jìn)行檢驗(yàn)
dataSource.setPoolPreparedStatements(true);
//8、運(yùn)行一次空閑連接回收器的時(shí)間間隔(60秒)
dataSource.setTimeBetweenEvictionRunsMillis(60 * 1000);
//9、驗(yàn)證時(shí)使用的SQL語(yǔ)句
dataSource.setValidationQuery("SELECT 1 FROM DUAL");
//10、借出連接時(shí)不要測(cè)試,否則很影響性能
//11、申請(qǐng)連接的時(shí)候檢測(cè),如果空閑時(shí)間大于 timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測(cè)連接是否有效
dataSource.setTestWhileIdle(false);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return jdbcTemplate;
}
@Override
public Class<?> getObjectType() {
return JdbcTemplate.class;
}
}
好了,測(cè)試一下
@Test
public void testJdbcTemplate() {
ApplicationContext ctx = this.before();
JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplateFactoryTestBean");
// Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean");
//執(zhí)行sql
String sql = "select 1 from dual";
String sql2 = "update xiaof_foo t set t.userName = ?, t.modifiedDate = ? where t.fooid = ? ";
// jdbcTemplate.execute(sql);
jdbcTemplate.update(sql2, "cutter_point", new Date(), "1");
}
Jdbctemplate
創(chuàng)建jdbctemplate只要?jiǎng)?chuàng)建對(duì)應(yīng)的DataSource就可以了,至于其他查詢,多種多樣
NamedParameterJdbcTemplate
我們?cè)谑褂胘dbctemplate的時(shí)候,都是通過(guò)?來(lái)指定對(duì)應(yīng)的參數(shù),那么這里就有一種更加貼近語(yǔ)義的方式
我們創(chuàng)建這個(gè)template對(duì)象
package cn.cutter.start.bean;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
/**
* 加入spring容器,使用 NamedParameterJdbcTemplate
* @author xiaof
*
*/
@Component
public class NamedParameterJdbcTemplateTestFactoryBean implements FactoryBean<NamedParameterJdbcTemplate> {
@Override
public NamedParameterJdbcTemplate getObject() throws Exception {
BasicDataSource dataSource = new BasicDataSource();
//設(shè)置相應(yīng)的參數(shù)
//1、數(shù)據(jù)庫(kù)驅(qū)動(dòng)類
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//2、url,用戶名,密碼
dataSource.setUrl("jdbc:mysql://localhost:3306/liferay?characterEncoding=utf-8");
dataSource.setUsername("liferay"); dataSource.setPassword("xiaofeng2017");
//3、初始化連接大小
dataSource.setInitialSize(1);
//4、連接池最大數(shù)據(jù)量
dataSource.setMaxTotal(500);
//5、連接池最大小空閑
dataSource.setMinIdle(1);
dataSource.setMaxIdle(20);
//6、最大等待時(shí)間 單位毫秒
dataSource.setMaxWaitMillis(20 * 1000);
//7、指明連接是否被空閑連接回收器(如果有)進(jìn)行檢驗(yàn)
dataSource.setPoolPreparedStatements(true);
//8、運(yùn)行一次空閑連接回收器的時(shí)間間隔(60秒)
dataSource.setTimeBetweenEvictionRunsMillis(60 * 1000);
//9、驗(yàn)證時(shí)使用的SQL語(yǔ)句
dataSource.setValidationQuery("SELECT 1 FROM DUAL");
//10、借出連接時(shí)不要測(cè)試,否則很影響性能
//11、申請(qǐng)連接的時(shí)候檢測(cè),如果空閑時(shí)間大于 timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測(cè)連接是否有效
dataSource.setTestWhileIdle(false);
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
return namedParameterJdbcTemplate;
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return NamedParameterJdbcTemplate.class;
}
}
使用這個(gè),我們來(lái)查詢一下數(shù)據(jù)庫(kù)的數(shù)據(jù)量
數(shù)據(jù)庫(kù)中我們查詢結(jié)果
select count(*) from xiaof_foo t where t.fooId = '1'
代碼中使用NamedParameterJdbcTemplate
@Test
public void testNamedParameterJdbcTemplate() {
ApplicationContext ctx = this.before();
NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplateTestFactoryBean");
// Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean");
//執(zhí)行sql
//設(shè)置參數(shù)對(duì)象
SqlParameterSource sqlParameterSource = new MapSqlParameterSource("fooId", "1");
//統(tǒng)計(jì)個(gè)數(shù)
String sql = "select count(*) from xiaof_foo t where t.fooId = :fooId";
int count = namedParameterJdbcTemplate.queryForObject(sql, sqlParameterSource, Integer.class);
System.out.println("個(gè)數(shù)是:" + count);
}
還有哦,最后注意下,這個(gè) :參數(shù)名 這個(gè)是區(qū)分大小寫(xiě)的
如果有多個(gè)參數(shù),那么直接對(duì)map對(duì)象進(jìn)行put就可以了
@Test
public void testNamedParameterJdbcTemplate() {
ApplicationContext ctx = this.before();
NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplateTestFactoryBean");
// Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean");
//執(zhí)行sql
//設(shè)置參數(shù)對(duì)象
MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource("fooid", "1");
sqlParameterSource.addValue("userName", "cutter_point");
//統(tǒng)計(jì)個(gè)數(shù)
String sql = "select count(*) from xiaof_foo t where t.fooId = :fooid and userName = :userName";
int count = namedParameterJdbcTemplate.queryForObject(sql, sqlParameterSource, Integer.class);
System.out.println("個(gè)數(shù)是:" + count);
}
結(jié)果:
借助bean對(duì)象進(jìn)行傳參
@Test
public void testNamedParameterJdbcTemplateModel() {
ApplicationContext ctx = this.before();
NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplateTestFactoryBean");
// Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean");
String sql = "select * from xiaof_foo t where t.fooId = :fooId";
XiaoFFoo xiaoFFoo = new XiaoFFoo();
xiaoFFoo.setFooId(1l);
SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(xiaoFFoo);
List<Map<String, Object>> xiaoFFoo2s = namedParameterJdbcTemplate.queryForList(sql, sqlParameterSource);
System.out.println("名字是:" + xiaoFFoo2s.get(0).get("userName"));
}
SimpleJdbcTemplate
集jdbctemplate和namedparameterJdbctemplate 與一身,并在兩者基礎(chǔ)上新增java 5的特性:
動(dòng)態(tài)參數(shù)
自動(dòng)拆箱解箱
范型
不過(guò)這個(gè)在后面的spring中會(huì)被去除,既然這樣,我們就不浪費(fèi)時(shí)間再這個(gè)上面了,拜拜呢你嘞。。。
總結(jié)
以上是生活随笔為你收集整理的【sping揭秘】19、关于spring中jdbctemplate中的DataSource怎么来呢的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一个微服务架构的消费端
- 下一篇: 步骤2 - websocket服务器转发