我們這是可以正好借助之前學的factorybean類,自己吧jdbctemplate加載到spring容器中,我們可以封裝多個這種對象,那么可以實現針對不同的數據庫的jdbctemplate
首先我們肯定要引入對應的jar,來構建數據源對象
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency>
根據這個我們簡單的創建一個jdbctemplate對象
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; /** * 用來封裝第三方對象的類,加入spring容器 * @author xiaof * */ @Component public class JdbcTemplateFactoryTestBean implements FactoryBean<JdbcTemplate> { @Override public JdbcTemplate getObject() throws Exception { BasicDataSource dataSource = new BasicDataSource(); //設置相應的參數 //1、數據庫驅動類 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、連接池最大數據量 dataSource.setMaxTotal(500); //5、連接池最大小空閑 dataSource.setMinIdle(1); dataSource.setMaxIdle(20); //6、最大等待時間 單位毫秒 dataSource.setMaxWaitMillis(20 * 1000); //7、指明連接是否被空閑連接回收器(如果有)進行檢驗 dataSource.setPoolPreparedStatements(true); //8、運行一次空閑連接回收器的時間間隔(60秒) dataSource.setTimeBetweenEvictionRunsMillis(60 * 1000); //9、驗證時使用的SQL語句 dataSource.setValidationQuery("SELECT 1 FROM DUAL"); //10、借出連接時不要測試,否則很影響性能 //11、申請連接的時候檢測,如果空閑時間大於 timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效 dataSource.setTestWhileIdle(false); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } @Override public Class<?> getObjectType() { return JdbcTemplate.class; } }
好了,測試一下
@Test public void testJdbcTemplate() { ApplicationContext ctx = this.before(); JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplateFactoryTestBean"); // Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean"); //執行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
創建jdbctemplate只要創建對應的DataSource就可以了,至於其他查詢,多種多樣
NamedParameterJdbcTemplate
我們在使用jdbctemplate的時候,都是通過?來指定對應的參數,那么這里就有一種更加貼近語義的方式
我們創建這個template對象
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(); //設置相應的參數 //1、數據庫驅動類 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、連接池最大數據量 dataSource.setMaxTotal(500); //5、連接池最大小空閑 dataSource.setMinIdle(1); dataSource.setMaxIdle(20); //6、最大等待時間 單位毫秒 dataSource.setMaxWaitMillis(20 * 1000); //7、指明連接是否被空閑連接回收器(如果有)進行檢驗 dataSource.setPoolPreparedStatements(true); //8、運行一次空閑連接回收器的時間間隔(60秒) dataSource.setTimeBetweenEvictionRunsMillis(60 * 1000); //9、驗證時使用的SQL語句 dataSource.setValidationQuery("SELECT 1 FROM DUAL"); //10、借出連接時不要測試,否則很影響性能 //11、申請連接的時候檢測,如果空閑時間大於 timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效 dataSource.setTestWhileIdle(false); NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); return namedParameterJdbcTemplate; } @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return NamedParameterJdbcTemplate.class; } }
使用這個,我們來查詢一下數據庫的數據量
數據庫中我們查詢結果
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"); //執行sql //設置參數對象 SqlParameterSource sqlParameterSource = new MapSqlParameterSource("fooId", "1"); //統計個數 String sql = "select count(*) from xiaof_foo t where t.fooId = :fooId"; int count = namedParameterJdbcTemplate.queryForObject(sql, sqlParameterSource, Integer.class); System.out.println("個數是:" + count); }
還有哦,最后注意下,這個 :參數名 這個是區分大小寫的
如果有多個參數,那么直接對map對象進行put就可以了
@Test public void testNamedParameterJdbcTemplate() { ApplicationContext ctx = this.before(); NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplateTestFactoryBean"); // Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean"); //執行sql //設置參數對象 MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource("fooid", "1"); sqlParameterSource.addValue("userName", "cutter_point"); //統計個數 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("個數是:" + count); }
結果:
借助bean對象進行傳參
@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 與一身,並在兩者基礎上新增java 5的特性:
動態參數
自動拆箱解箱
范型
不過這個在后面的spring中會被去除,既然這樣,我們就不浪費時間再這個上面了,拜拜呢你嘞。。。