Spring配置JDBCTemplate
案例:单测查询全部学生
项目结构:

1.导入部署jar包:spring-jdbc
<!--spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
2.创建实体类:Student
public class Student {
private Integer id; //编号
private String name; //姓名
private Integer age; //年龄
private Date birthday; //出生日期
}
3.创建dao层的接口和实现类:
IStudentDAO:
public interface IStudentDAO {
//查询全部
public List<Student> findAll();
}
StudentDAOImpl:继承JdbcDaoSupport 实现IStudentDAO
public class StudentDAOImpl extends JdbcDaoSupport implements IStudentDAO{
public List<Student> findAll() {
String sql="select * from student";
List<Student> list=this.getJdbcTemplate().query(sql, new RowMapper<Student>() {
/**
*
* @param rs
* @param i
* @return
* @throws SQLException
*/
public Student mapRow(ResultSet rs, int i) throws SQLException {
Student student=new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setBirthday(rs.getDate("birthday"));
return student;
}
});
return list;
}
}
4.创建service层的接口和实现类:
IStudentService:
public interface IStudentService {
//查询全部
public List<Student> findAll();
}
StudentServiceImpl:
public class StudentServiceImpl implements IStudentService {
private IStudentDAO dao;
public List<Student> findAll() {
return dao.findAll();
}
public IStudentDAO getDao() {
return dao;
}
public void setDao(IStudentDAO dao) {
this.dao = dao;
}
}
在applicationContextSpring15jdbctemplate.xml中配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--00.识别jdbc.properties-->
<!--方式一-->
<!-- <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
<!--方式二-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties"></property>
</bean>
<!--01.建立数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03.dao的配置-->
<bean id="studentDao" class="cn.happy.spring22jdbctemplate.dao.impl.StudentDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--04.service的配置-->
<bean id="studentService" class="cn.happy.spring22jdbctemplate.service.impl.StudentServiceImpl">
<property name="dao" ref="studentDao"></property>
</bean>
</beans>
其他三种数据源的方案:
<!--02建立数据源 dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03建立数据源 c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--03建立数据源 alibaba-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.uname}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
单元测试:
//jdbcTemplate
@Test
public void test01(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextSpring15jdbctemplate.xml");
IStudentService dao=(IStudentService) context.getBean("studentService");
List<Student> list = dao.findAll();
for (Student item:list){
System.out.println(item.getName());
}
}
执行结果:

Spring配置JDBCTemplate的更多相关文章
- Spring:JdbcTemplate使用指南
Spring:JdbcTemplate使用指南 Spring:JdbcTemplate使用指南 前言: 本文指在介绍Spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转 ...
- spring配置连接池和dao使用jdbcTemplate
1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 (1)把代码中的实现在配置文件中实现 2 dao使用jdbcTemplate (1) 创建ser ...
- Spring配置连接池和 Dao 层使用 jdbcTemplate
1.Spring 配置 c3p0 连接池 (1)导入jar包(Maven项目) <dependency> <groupId>com.mchange</groupId> ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- spring配置详解
1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...
- spring配置事务
一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...
- spring使用jdbcTemplate和jdbcdaosupport和namedparameter
jdbcTemplate: 首先连接数据库 <!-- 导入外部文件 --> <context:property-placeholder location="classpat ...
- Spring之JDBCTemplate学习
一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.core. ...
- spring使用JdbcTemplate和jdbcDaosupport及具名参数使用
关于jdbctemplate: 个人感觉比Java链接mysql那一套方便好维护多了,只需在配置文件维护即可 需要的包: com.springsource.net.sf.cglib-2.2.0.jar ...
随机推荐
- axure交互样式(下拉列表和矩形)
*****矩形交互样式之单选按钮*****1.账号输入框.密码输入框等文本框实现效果:输入框获取焦点时边框是蓝色,失 去焦点时边框为红色: 2.实现思路:边框用矩形来设置选中和未选中.禁用和启用即可 ...
- 设置IDEA中的web
- Fisher–Yates shuffle 算法
费希尔 - 耶茨洗牌 维基百科,自由的百科全书 所述费-耶茨洗牌是一种算法,用于产生随机排列的有限的序列 -in平原而言,算法打乱的序列.该算法有效地将所有元素放在帽子里; 它通过随机从帽子中 ...
- OpenCV-Python入门教程4-颜色空间转换
一.颜色空间转换 import cv2 import numpy as np img = cv2.imread('lena.jpg') # 转换成灰度图 img_gray = cv2.cvtColor ...
- 删除Apache服务的命令
转到\Apache24\bin目录下,使用cmd命令sc delete apache2.2
- 一脸懵逼学习Storm的搭建--(一个开源的分布式实时计算系统)
Storm的官方网址:http://storm.apache.org/index.html :集群部署的基本流程(基本套路): 集群部署的流程:下载安装包.解压安装包.修改配置文件.分发安装包.启动集 ...
- UE4 日志
第一种 输出在控制台中,需要在启动之后的游戏窗口中点击~以打开控制台,然后输入showlog,这时候会弹出一个cmd日志窗口.在程序中使用代码 UE_LOG(LogTemp, Warning, TEX ...
- ajax一次获取整个表单的数据
$.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid').serialize(),// ...
- sed 简单修改配置文件ip地址
sed -i 's/old ip/new ip/g' file.txt
- 修改注册表.exe的文件目录
文件打开方式不能选择默认打开文件 cmd >regedit 以sublime_text为例 HKEY_CLASSES_ROOT/Applications/sublime_text.exe/she ...