2019独角兽企业重金招聘Python工程师标准>>>

概念

Spring中的jdbcTemplate的主要作用是实现数据的交互,下面我们就在dao层中如何使用jdbctemplate写测试案例

项目目录如下

基于xml实现jdbctemplate

这里我们使用的是JdbcDaoSupport这个类,主要是为了减少重复每次在dao层都要用的Jdbctemplate set方法

导入jar包

 <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.1.6.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency>

学生实体类

package com.cc.entity;import java.io.Serializable;/*** 学生实体类*/
public class Student implements Serializable {private int id;private String stuno;private String name;private int classid;public int getId() {return id;}public void setId(int id) { this.id = id; }public String getStuno() {return stuno;}public void setStuno(String stuno) {this.stuno = stuno;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getClassid() {return classid;}public void setClassid(int classid) {this.classid = classid;}@Overridepublic String toString() {return "Student{" +"id=" + id +", stuno='" + stuno + '\'' +", name='" + name + '\'' +", classid=" + classid +'}';}
}

方法接口

package com.cc.dao;import com.cc.entity.Student;/*** 定义接口*/
public interface studentDao {/*** 根据id查找学生* @param* @return*/Student findStudentbyId(Integer id);/*** 根据名称查找用户*/Student findStudentbyName(String name);/*** 更新账户*/void updateStudent(Student student);
}

方法实现类

package com.cc.dao;import com.cc.entity.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import java.util.List;public class StudentDaoImpl extends JdbcDaoSupport implements studentDao {@Overridepublic Student findStudentbyId(Integer id) {List<Student> students =getJdbcTemplate().query("select * from student where id=?",new BeanPropertyRowMapper<Student>(Student.class),id);return students.isEmpty()?null:students.get(0);}@Overridepublic Student findStudentbyName(String name) {List<Student> students =getJdbcTemplate().query("select * from student where name=?",new BeanPropertyRowMapper<Student>(Student.class),name);if(students.isEmpty()){return null;}if(students.size()>1){throw  new RuntimeException("返回结果集不唯一");}return students.get(0);}@Overridepublic void updateStudent(Student student) {getJdbcTemplate().update("update student set stuno=?,name=?,classid=? where id=?",student.getStuno(),student.getName(),student.getClassid(),student.getId());}
}

配置文件

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="studentDao" class="com.cc.dao.StudentDaoImpl"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置数据源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/fresh?characterEncoding=UTF-8"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean>
</beans>

测试类

import com.cc.dao.StudentDaoImpl;
import com.cc.entity.Student;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Testjdbctemplate {@Autowiredprivate StudentDaoImpl student;@Testpublic void testfindbyId(){ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");StudentDaoImpl student=ac.getBean("studentDao", StudentDaoImpl.class);Student stu =student.findStudentbyId(1);System.out.println(stu);}@Testpublic void testfindbyname(){ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");StudentDaoImpl student=ac.getBean("studentDao", StudentDaoImpl.class);Student stu=student.findStudentbyName("陈多多");System.out.println(stu);stu.setName("陈多糖");student.updateStudent(stu);}
}

测试结果如下

转载于:https://my.oschina.net/u/4115727/blog/3053235

Spring系列教程六: Spring jdbcTemplate在Dao中的使用相关推荐

  1. 爆破专栏丨Spring系列教程解决Spring Security环境中的跨域问题

    上一章节中,一一哥 给各位讲解了同源策略和跨域问题,以及跨域问题的解决方案,在本篇文章中,我会带大家进行代码实现,看看在Spring Security环境中如何解决跨域问题. 一. 启用Spring ...

  2. freemarker ftl模板_Spring Boot2 系列教程(十)Spring Boot 整合 Freemarker

    今天来聊聊 Spring Boot 整合 Freemarker. Freemarker 简介 这是一个相当老牌的开源的免费的模版引擎.通过 Freemarker 模版,我们可以将数据渲染成 HTML ...

  3. 最新 Spring 系列教程,都在这了

    转载自  最新 Spring 系列教程,都在这了 Spring Boot 系列 什么是 Spring Boot? 公司不用 Spring Boot,果断离职了! 告诉你,Spring Boot 真是个 ...

  4. 以太坊构建DApps系列教程(六):使用定制代币进行投票

    在本系列关于使用以太坊构建DApps教程的第5部分中,我们讨论了如何为Story添加内容,查看如何添加参与者从DAO购买代币的功能以及在Story中添加提交内容.现在是编写DAO最终形式的时候了:投票 ...

  5. [转]Android Studio系列教程六--Gradle多渠道打包

    转自:http://www.stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ Android Studio系列教程六--Grad ...

  6. 米思齐(Mixly)图形化系列教程(六)-for循环

    目录 For执行过程 省略 省略'循环变量赋值' 省略'循环条件' 省略"循环变量增量" FOR循环使用举例 遍历数组 顺序输出数据 指定程序重复执行次数 死循环 求和 教程导航 ...

  7. PVE系列教程(六)、安装Windows11系统(专业版、企业版、家庭版通用)

    为了更好的浏览体验,欢迎光顾勤奋的凯尔森同学个人博客 PVE系列教程(六).安装Windows11系统(专业版.企业版.家庭版通用) 一.创建win11的虚拟机,并设置参数 在PVE右上角点击创建虚拟 ...

  8. Spring Security系列教程解决Spring Security环境中的跨域问题

    原创:千锋一一哥 前言 上一章节中,一一哥 给各位讲解了同源策略和跨域问题,以及跨域问题的解决方案,在本篇文章中,我会带大家进行代码实现,看看在Spring Security环境中如何解决跨域问题. ...

  9. Spring系列(六):@Conditional注解用法介绍

    今天给大家介绍@Conditional懒加载注解用法,希望对大家能有所帮助! 1.@Conditional注解介绍 @Conditional是Spring4版本新提供的一种注解,它的作用是按照设定的条 ...

  10. 手撸Spring系列10:Spring AOP(实战篇)

    说在前头: 笔者本人为大三在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,发布的文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正. ...

最新文章

  1. python字典程序题_python字典练习题
  2. Redis进阶-Stream多播的可持久化的消息队列
  3. Linux之Ubuntu下安装屏幕录像软件(SimpleScreenRecorder)【摘抄】
  4. 转专业学计算机难嘛,大学转专业容易吗 转专业需要什么条件
  5. linux--exec函数族浅析
  6. tensorboard特征图可视化
  7. deville什么意思_欧米茄手表的deville是什么意思?
  8. PostgreSQL与MySQL语法对比总结
  9. 报 刊 集 锦(转载)
  10. dnf全部使用_DNF的命令使用教学
  11. win10自带c语言编程猫,编程猫教程:编程猫如何做游戏?
  12. IEEE论文检测的字体未嵌入问题Times New Roman,Bold, Times New Roman,Italic is not embedded解决方法
  13. 以自动化为“遮羞布”,亚马逊掩盖了惊人的工伤记录
  14. scratch小猫钓鱼 电子学会图形化编程scratch等级考试四级真题和答案解析2021-9
  15. 小工具:批量替换文件夹下所有文件内容中的指定词
  16. 【BZOJ1778】[Usaco2010 Hol]Dotp 驱逐猪猡
  17. PDF文件的旋转和保存
  18. OpenHarmony成长计划学生挑战赛7天打卡活动介绍
  19. 论算法的重要性与乐趣
  20. Redis系列教材 (四)- Jedis 教程

热门文章

  1. java 生成word 分页,jsp转word + 分页
  2. 毕业季--写给未来的自己
  3. java合并果子_合并果子(经典优先队列)
  4. 传奇私服服务端制作详细教程
  5. linux设置mysql开机启动
  6. 统一网关Gateway
  7. java 时区处理_JAVA时区处理(转)
  8. 理查德·费曼:发现的乐趣
  9. java blowfish ecb,来自blowfish / base64的解密消息时出错
  10. APP推广要做哪些?渠道?方案?竞争分析?