案例:单测查询全部学生

项目结构:

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的更多相关文章

  1. Spring:JdbcTemplate使用指南

    Spring:JdbcTemplate使用指南 Spring:JdbcTemplate使用指南 前言: 本文指在介绍Spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转 ...

  2. spring配置连接池和dao使用jdbcTemplate

    1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 (1)把代码中的实现在配置文件中实现 2 dao使用jdbcTemplate (1) 创建ser ...

  3. Spring配置连接池和 Dao 层使用 jdbcTemplate

    1.Spring 配置 c3p0 连接池 (1)导入jar包(Maven项目) <dependency> <groupId>com.mchange</groupId> ...

  4. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  5. spring配置详解

    1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...

  6. spring配置事务

    一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...

  7. spring使用jdbcTemplate和jdbcdaosupport和namedparameter

    jdbcTemplate: 首先连接数据库 <!-- 导入外部文件 --> <context:property-placeholder location="classpat ...

  8. Spring之JDBCTemplate学习

    一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.core. ...

  9. spring使用JdbcTemplate和jdbcDaosupport及具名参数使用

    关于jdbctemplate: 个人感觉比Java链接mysql那一套方便好维护多了,只需在配置文件维护即可 需要的包: com.springsource.net.sf.cglib-2.2.0.jar ...

随机推荐

  1. ORACLE EHCC(exadata hybrid columnar compression)

    目录: 1. 简介 2. 压缩方式及压缩比 3. 压缩哪些数据 4. 可能有用的脚本 一.简介 EHCC(Exadata Hybrid Columnar Compression),是Oralce 数据 ...

  2. spring cloud 自定义ribbon客户端

    一.自定义Ribbon客户端-[方式一]配置类 1.1.自定义负载规则 增加RibbonConfiguration.java配置类 public class RibbonConfiguration { ...

  3. springboot+mybatis+springMVC基础框架搭建

    项目结构概览 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  4. 通过ModelForm实现主机添加和编辑

    通过ModelForm实现主机添加和编辑 ModelForm这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来:在使用Model和Form时,都需要对字段进行 ...

  5. 【C#】WebApi 添加过滤器,实现对请求参数和响应内容的日志记录

    filter的介绍 filter在Web API中经常会用到,主要用于记录日志,安全验证,全局错误处理等:Web API提供两种过滤器的基本类型:actionfilterattribute,excep ...

  6. AtCoder Grand Contest 017D (AGC017D) Game on Tree 博弈

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC017D.html 题目传送门 - AGC017D 题意 给定一棵 n 个节点的以节点 1 为根的树. 两个 ...

  7. >maven-compiler-plugin的理解

    在maven中项目中这个插件一直都会看见,但是一直没有认真学习一下为啥使用,现在有空就查询学习一下. 1.使用场景 下载了一些工程需要编译的时候. maven是个项目管理工具,如果我们不告诉它我们的代 ...

  8. Http和RPC区别

    本文转自:https://blog.csdn.net/mou_it/article/details/79873612 RPC(即Remote Procedure Call,远程过程调用)和HTTP(H ...

  9. CentOS7配置FTP服务器增强版~(零基础学会FTP配置)

    ps:原文不知出处,但是原文也不能正常启动,这里做了一些修改!如果能正常配置请在下方留言让更多的人看到,因为之前我本人照着网上的教程安装卸载了十多次也无法正常使用,不希望后面的兄弟继续浪费时间,如果不 ...

  10. 关于XML的简单整理