spring29: JdbcTemplate详解
spring提供用于操作jdbc工具类,类似DBUtils, 依赖连接池DataSource(数据源)。
通过api
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;public class TestApi {public static void main(String[] args){// 1. 创建数据源(连接池) dbcpBasicDataSource basicDataSource = new BasicDataSource();// 基本四项basicDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); //驱动basicDataSource.setUrl("jdbc:oracle:thin:@//127.0.0.1:1521/orcl");basicDataSource.setUsername("scott");basicDataSource.setPassword("123456");// 2. 创建模板JdbcTemplate jdbcTemplate = new JdbcTemplate(basicDataSource);// 3. 通过api操作jdbcTemplate.update("insert into course(cid, cname) values(?,?)", "306","ios编程");}
}
配置dbcp
配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property><property name="url" value="jdbc:oracle:thin:@//127.0.0.1:1521/orcl"></property><property name="username" value="scott"></property><property name="password" value="123456"></property></bean><!-- 创建模板,需要注入数据源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.dbcp.CourseDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean></beans>
Dao类
package com.atchina.d_spring_jdbctemplate.dbcp;import org.springframework.jdbc.core.JdbcTemplate;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class CourseDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public void update(Course c){String sql = "update course a set cname=? where cid = ?";jdbcTemplate.update(sql, c.getCname(), c.getId());}
}
测试类
package com.atchina.d_spring_jdbctemplate.dbcp;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class TestDBCP {@Testpublic void demo01(){Course c = new Course();c.setCname("数据库原理");c.setId(306);String xmlPath = "com/atchina/d_spring_jdbctemplate/dbcp/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CourseDao dao = (CourseDao)applicationContext.getBean("courseDao");dao.update(c);}
}
配置c3p0
配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置数据源c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property><property name="jdbcUrl" value="jdbc:oracle:thin:@//127.0.0.1:1521/orcl"></property><property name="user" value="scott"></property><property name="password" value="123456"></property></bean><!-- 创建模板,需要注入数据源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.c3p0.CourseDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean></beans>
测试类
package com.atchina.d_spring_jdbctemplate.c3p0;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class TestC3P0 {@Testpublic void demo01(){Course c = new Course();c.setCname("操作系统");c.setId(306);String xmlPath = "com/atchina/d_spring_jdbctemplate/c3p0/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CourseDao dao = (CourseDao)applicationContext.getBean("courseDao");dao.update(c);}
}
使用JdbcDaoSupport
import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class CourseDao extends JdbcDaoSupport{public void update(Course c){String sql = "update course a set cname=? where cid = ?";this.getJdbcTemplate().update(sql, c.getCname(), c.getCid());}public List<Course> findAll(){String sql = "select * from course";return this.getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(Course.class));}
}
配置文件
JdbcDaoSupport中有JdbcTemplate的属性,所以在配置文件中可以不用配置jdbcTemplate,直接配置一个数据源即可。jdbcDaoSupport会根据数据源创建一个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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置数据源c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property><property name="jdbcUrl" value="jdbc:oracle:thin:@//127.0.0.1:1521/orcl"></property><property name="user" value="scott"></property><property name="password" value="123456"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.support.CourseDao"><property name="dataSource" ref="dataSource"></property></bean></beans>
使用properties文件
配置properties文件
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@//127.0.0.1:1521/orcl
jdbc.user=scott
jdbc.password=123456
配置spring文件
<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 加载配置文件"classpaht:"前缀表示src下在配置文件之后通过${key}获得内容 --><context:property-placeholder location="classpath:com/atchina/d_spring_jdbctemplate/properties/jdbcinfo.properties"/><!-- 配置数据源c3p0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 配置dao --><bean id="courseDao" class="com.atchina.d_spring_jdbctemplate.properties.CourseDao"><property name="dataSource" ref="dataSource"></property></bean></beans>
dao类
package com.atchina.d_spring_jdbctemplate.properties;import java.util.List;import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class CourseDao extends JdbcDaoSupport{public void update(Course c){String sql = "update course a set cname=? where cid = ?";this.getJdbcTemplate().update(sql, c.getCname(), c.getCid());}public List<Course> findAll(){String sql = "select * from course";return this.getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(Course.class));}public Course getById(int cid){String sql = "select * from course where cid = ? ";return this.getJdbcTemplate().queryForObject(sql, BeanPropertyRowMapper.newInstance(Course.class), cid);}
}
测试类
package com.atchina.d_spring_jdbctemplate.properties;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.atchina.d_spring_jdbctemplate.pojo.Course;public class TestProperties {@Testpublic void demo01(){String xmlPath = "com/atchina/d_spring_jdbctemplate/properties/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CourseDao dao = (CourseDao)applicationContext.getBean("courseDao");Course cr = dao.getById(306);System.out.println(cr);}
}
spring29: JdbcTemplate详解相关推荐
- JdbcTemplate详解 - 2
JdbcTemplate详解 - 2 http://tianya23.blog.51cto.com/1081650/275292 1.由于之前JdbcTemplate的程序需要编写一堆的RowMapp ...
- spring教程--JdbcTemplate详解
Spring的JdbcTemplate JdbcTemplate模板与DbUtils工具类比较类似. 1 Spring对持久层技术支持: JDBC:org.springframework.jdbc.c ...
- 详解jdbcTemplate和namedParameterJdbcTemplate
我们开发DAO层时用的最多的就是ORM框架(Mybatis,hibernate)了.在有些特殊的情况下,ORM框架的搭建略显笨重,这时最好的选择就是Spring中的jdbcTemplate了.本文对j ...
- Spring JdbcTemplate方法详解
2019独角兽企业重金招聘Python工程师标准>>> Spring JdbcTemplate方法详解 标签: springhsqldbjava存储数据库相关sql 2012-07- ...
- getinstance方法详解_二、设计模式总览及工厂模式详解
二.架构师内功心法之设计模式 2.架构师内功心法之设计模式 2.1.课程目标 1.通过对本章内容的学习,了解设计模式的由来. 2.介绍设计模式能帮我们解决哪些问题. 3.剖析工厂模式的历史由来及应用场 ...
- Spring JDBC详解
<Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 一.概述 在Spring JDBC模块中,所有的 ...
- spring框架使用Quartz执行定时任务实例详解
版权声明:本文为博主原创文章,如需转载,请标明出处. https://blog.csdn.net/alan_liuyue/article/details/80382324 Quartz简介 1.Qua ...
- Spring Boot 2.x基础教程:默认数据源Hikari的配置详解
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | http://blog.di ...
- Java编程配置思路详解
Java编程配置思路详解 SpringBoot虽然提供了很多优秀的starter帮助我们快速开发,可实际生产环境的特殊性,我们依然需要对默认整合配置做自定义操作,提高程序的可控性,虽然你配的不一定比官 ...
最新文章
- UML-2-迭代、进化和敏捷
- ArrayList的实现原理--转
- java静态类和非静态类_关于java:静态和非静态内部类的区别?
- C语言程序练习-L1-017 到底有多二 (15分)
- 【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)
- 二、uniapp项目(分段器的使用、scroll-view、视频下载、转发)
- mysql心得体会一百字_MYSQL CPU 100%实例详解
- 南邮计算机专硕考研专业课,南京邮电大学(专业学位)计算机技术研究生考试科目和考研参考书目...
- 例子 类的定义与对象的创建 狗的例子
- Asp.net直接保存文件到客户端
- Python SPSS教程
- docker安装gamit_gamit的安装步骤
- element-ui el-upload框去除‘按 delete 键可删除‘提示
- 二进制方式部署k8s集群1.21版本-域名形式
- WinRAR v5.71 简体中文正式版
- Vue 组件之间传值
- C++调用C# dll 未能加载文件或程序集
- Qt多功能计算器(一)——基本功能
- 勃林格殷格翰与Lifebit合作识别全球传染病暴发;百济神州和Shoreline Biosciences达成合作 | 医药健闻...
- 中国大学慕课MOOC ,Shell测试答案