SpringBoot中JdbcTemplate
步骤如下:
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
1.dao层
public interface IGradeDao {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer tid);
public List<Grade> findAll();
}
2.dao中的impl
@Repository
public class IGradeDaoImpl implements IGradeDao {
//导入jdbctemplate模板
private JdbcTemplate jdbcTemplate;
@Override
public int insertGrade(Grade grade) {
return jdbcTemplate.update("insert into teacher(tname) values(?)",grade.getTname());
}
@Override
public int updateGrade(Grade grade) {
return jdbcTemplate.update("update teacher set tname=? where tid=?",grade.getTname(),grade.getTid());
} @Override
public int deleteGrade(Integer tid) {
return jdbcTemplate.update("delete from teacher where tid=?",tid);
} @Override
public List<Grade> findAll() {
//封装行数据映射
RowMapper<Grade> rowMapper=new RowMapper<Grade>() {
@Override
public Grade mapRow(ResultSet rs, int rowNum) throws SQLException {
Grade grade=new Grade(rs.getInt("tid"),rs.getString("tname"));
return grade;
}
};
return jdbcTemplate.query("select * from teacher", rowMapper);
}
}
3.entity层
package com.wdksft.entity;
public class Grade {
public Grade(Integer tid, String tname) {
this.tid = tid;
this.tname = tname;
}
public Grade(){
}
public Grade(String tname) {
this.tname = tname;
}
private Integer tid;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
private String tname;
}
4.Service层
package com.wdksft.service;
import com.wdksft.entity.Grade;
import java.util.List;
public interface IGradeService {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer tid);
public List<Grade> findAll();
}
5.Service层中的impl
package com.wdksft.service; import com.wdksft.dao.IGradeDao;
import com.wdksft.entity.Grade;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List;
@Service("iGradeService")
public class IGradeServiceImpl implements IGradeService {
@Resource
private IGradeDao iGradeDao; @Override public int insertGrade(Grade grade) {
return iGradeDao.insertGrade(grade);
} @Override
public int updateGrade(Grade grade) {
return iGradeDao.updateGrade(grade);
} @Override
public int deleteGrade(Integer tid) {
return iGradeDao.deleteGrade(tid);
} @Override
public List<Grade> findAll() {
return iGradeDao.findAll();
}
}
6.controller层
package com.wdksft.controller; import com.wdksft.entity.Grade;
import com.wdksft.service.IGradeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource;
import java.util.List; @RestController
public class JDBCTemplateController {
@Resource
private IGradeService iGradeService;
@RequestMapping("/insertGrade")
public int insertGrade(){
return iGradeService.insertGrade(new Grade("风"));
}
@RequestMapping("/updateGrade")
public int updateGrade(){
return iGradeService.updateGrade(new Grade(,"风"));
}
@RequestMapping("/deleteGrade")
public int deleteGrade(){
return iGradeService.deleteGrade();
}
@RequestMapping("/findAll")
public List<Grade> findAll(){
return iGradeService.findAll();
}
}
7.application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///test
username: root
password: root
SpringBoot中JdbcTemplate的更多相关文章
- SpringBoot使用JdbcTemplate
前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...
- redis基本操作和在springboot中的使用
本文介绍redis的使用 redis启动步骤 说明 redis自增自减相关操作 redis string set操作 get操作 其他操作 redis hash set操作 get操作 其他操作 re ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)
Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...
- 关于SpringBoot集成JDBCTemplate的RowMapper问题
JdbcTemplate 是Spring提供的一套JDBC模板框架,利用AOP 技术来解决直接使用JDBC时大量重复代码的问题.JdbcTemplate虽然没有MyBatis 那么灵活,但是直接使用J ...
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- Spring 中jdbcTemplate 实现执行多条sql语句
说一下Spring框架中使用jdbcTemplate实现多条sql语句的执行: 很多情况下我们需要处理一件事情的时候需要对多个表执行多个sql语句,比如淘宝下单时,我们确认付款时要对自己银行账户的表里 ...
随机推荐
- maven聚合(依赖聚合)
maven聚合工程 原文地址:http://juvenshun.iteye.com/blog/305865 http://blog.csdn.NET/woxueliuyun/article/detai ...
- 第一个Hadoop程序-单词计数
上一篇配置了Hadoop,本文将测试一个Hadoop的小案例 hadoop的Wordcount程序是hadoop自带的一个小的案例,是一个简单的单词统计程序,可以在hadoop的解压包里找到,如下: ...
- thinkphp6.0 多应用模块下提示控制器不存在
thinkphp6.0 多应用模块下提示控制器不存在 在项目根目录下使用Composer composer require topthink/think-multi-app 参考链接
- poj 3281 Dining (Dinic)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22572 Accepted: 10015 Descript ...
- python 抓取youtube教程
前言: 相信大家很多人都看过youtube网站上的视频,网站上有很多的优质视频,清晰度也非常的高,看到喜欢的想要下载到本地,虽然也有很多方法,但是肯定没有python 来的快, 废话不多说,上代码: ...
- 0MQ底层队列设计
ypipe_t has a yqueue_t. pipe_t relates two ypipe(s).pipe_t就是0MQ框架内使用的底层队列. yqueue_t的设计目的. yqueue_t 的 ...
- 结合源码,重温 Android View 的事件处理知多少 ?
前言 Android View 的 事件处理在我们的编程中,可谓是无处不在了.但对于大多数人而言,一直都是简单的使用,对其原理缺乏深入地认识. 学 Android 有一段时间了,最近发现,很多基础知识 ...
- Java w3c离线手册
提供给大家使用,懒得找: 前端后端一般都用的到 查看文档 1. JDK_API_1_6_zh_CN.CHM 2. W3School离线手册(2018.04.01).chm 3. jqu ...
- pdf 在线预览之 vue-pdf插件
vue-pdf 支持到ie11npm安装:npm install --save vue-pdf 组件template: <div class="show-pdf"> ...
- Scala函数式编程(四)函数式的数据结构 上
这次来说说函数式的数据结构是什么样子的,本章会先用一个list来举例子说明,最后给出一个Tree数据结构的练习,放在公众号里面,练习里面给出了基本的结构,但代码是空缺的需要补上,此外还有预留的test ...