网站代码素材建设,html5中国网站欣赏,标准的软件开发流程,部队网站建设设计我们这是可以正好借助之前学的factorybean类#xff0c;自己吧jdbctemplate加载到spring容器中#xff0c;我们可以封装多个这种对象#xff0c;那么可以实现针对不同的数据库的jdbctemplate 首先我们肯定要引入对应的jar#xff0c;来构建数据源对象 dependency自己吧jdbctemplate加载到spring容器中我们可以封装多个这种对象那么可以实现针对不同的数据库的jdbctemplate 首先我们肯定要引入对应的jar来构建数据源对象 dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-dbcp2/artifactIdversion2.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 FactoryBeanJdbcTemplate {Overridepublic JdbcTemplate getObject() throws Exception {BasicDataSource dataSource new BasicDataSource();//设置相应的参数//1、数据库驱动类dataSource.setDriverClassName(com.mysql.jdbc.Driver);//2、url用户名密码dataSource.setUrl(jdbc:mysql://localhost:3306/liferay?characterEncodingutf-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;}Overridepublic Class? getObjectType() {return JdbcTemplate.class;}} 好了测试一下 Testpublic void testJdbcTemplate() {ApplicationContext ctx this.before();JdbcTemplate jdbcTemplate (JdbcTemplate) ctx.getBean(jdbcTemplateFactoryTestBean);
// Object obj (IntroductionTestBean) ctx.getBean(introductionTestBean);//执行sqlString 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 FactoryBeanNamedParameterJdbcTemplate {Overridepublic NamedParameterJdbcTemplate getObject() throws Exception {BasicDataSource dataSource new BasicDataSource();//设置相应的参数//1、数据库驱动类dataSource.setDriverClassName(com.mysql.jdbc.Driver);//2、url用户名密码dataSource.setUrl(jdbc:mysql://localhost:3306/liferay?characterEncodingutf-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;}Overridepublic Class? getObjectType() {// TODO Auto-generated method stubreturn NamedParameterJdbcTemplate.class;}} 使用这个我们来查询一下数据库的数据量 数据库中我们查询结果 select count(*) from xiaof_foo t where t.fooId 1 代码中使用NamedParameterJdbcTemplate Testpublic 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就可以了 Testpublic 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对象进行传参 Testpublic 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);ListMapString, Object xiaoFFoo2s namedParameterJdbcTemplate.queryForList(sql, sqlParameterSource);System.out.println(名字是: xiaoFFoo2s.get(0).get(userName));} SimpleJdbcTemplate 集jdbctemplate和namedparameterJdbctemplate 与一身并在两者基础上新增java 5的特性 动态参数 自动拆箱解箱 范型 不过这个在后面的spring中会被去除既然这样我们就不浪费时间再这个上面了拜拜呢你嘞。。。 转载于:https://www.cnblogs.com/cutter-point/p/9147745.html