SpringBoot整合JDBC模板
目录

Grade实体类
public class Grade {
private Integer gradeId;
private String gradeName;
public Grade(){
}
public Grade(Integer gradeId, String gradeName) {
this.gradeId = gradeId;
this.gradeName = gradeName;
}
public Grade(String gradeName) {
this.gradeName = gradeName;
}
public Integer getGradeId() {
return gradeId;
}
public void setGradeId(Integer gradeId) {
this.gradeId = gradeId;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
}
IGradeDao
import com.boot.entity.Grade;
import java.util.List;
public interface IGradeDao {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer id);
public List<Grade> findAll();
}
IGradeDaoImpl
import com.boot.dao.IGradeDao;
import com.boot.entity.Grade;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class IGradeDaoImpl implements IGradeDao {
//导入JDBCTemplate模板
@Resource
private JdbcTemplate jdbcTemplate; @Override
public int insertGrade(Grade grade) {
return jdbcTemplate.update("insert into Grade(GradeName) values(?)",grade.getGradeName());
} @Override
public int updateGrade(Grade grade) {
return jdbcTemplate.update("update Grade set GradeName=? where GradeId=?",grade.getGradeName(),grade.getGradeId());
} @Override
public int deleteGrade(Integer id) {
return jdbcTemplate.update("delete from Grade where GradeId=?",id);
} @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("GradeId"),rs.getString("GradeName"));
return grade;
}
};
return jdbcTemplate.query("select * from Grade", rowMapper);
}
}
IGradeService
public interface IGradeService {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer id);
public List<Grade> findAll();
}
IGradeServiceImpl
@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 id) {
return iGradeDao.deleteGrade(id);
}
@Override
public List<Grade> findAll() {
return iGradeDao.findAll();
}
}
JDBCTemplateController
@RestController
public class JDBCTemplateController {
@Resource
private IGradeService iGradeService; @RequestMapping("/insertGrade")
public int insertGrade(){
return iGradeService.insertGrade(new Grade("S1"));
}
@RequestMapping("/updateGrade")
public int updateGrade(){
return iGradeService.updateGrade(new Grade(,"S2"));
}
@RequestMapping("/deleteGrade")
public int deleteGrade(){
return iGradeService.deleteGrade();
}
@RequestMapping("/findAll")
public List<Grade> findAll(){
return iGradeService.findAll();
}
}
application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springbootjpa
username: root
password: ##更改Tomcat端口
server:
port:
##指定当前工程项目访问地址
context-path: /jdbc
SpringBoot整合JDBC模板的更多相关文章
- springboot整合jsp模板
springboot整合jsp模板 在使用springboot框架里使用jsp的时候,页面模板使用jsp在pom.xnl中需要引入相关的依赖,否则在controller中无法返回到指定页面 〇.搭建s ...
- Springboot整合thymeleaf模板
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- springboot整合JDBC出现Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
今天使用springboot整合JDBC的使用,开始使用的是 com.mysql.jdbc.Driver驱动 结果运行出现此异常 那我们根据提示要求来修改即可 把驱动改成最新的com.mysql.cj ...
- 3、SpringBoot整合之SpringBoot整合JDBC
SpringBoot整合JDBC 一.创建SpringBoot项目 选择Spring Web.JDBC API.MySQL Driver 二.在pom配置文件中修改JDBC版本,导入lombok &l ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot 整合jdbc和mybatis
摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...
- springboot整合thymleaf模板引擎
thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更 ...
- 「快学springboot」SpringBoot整合freeMark模板引擎
前言 虽然现在流行前后端分离开发和部署,但是有时候还是需要用到服务端渲染页面的.比如:需要考虑到SEO优化等问题的时候,FreeMark其实还是很有作用的.本人的博客本来是用React开发的,但是后来 ...
- SpringBoot整合jdbc及整合Druid数据源
一.整合jdbc 1.创建一个springInitializr项目 勾选 web----springweb.SQL----JDBC API,MYSQL Diver 2.连接数据库 3.创建yml 4. ...
随机推荐
- collections模块之defaultdict()与namedtuple()方法简单介绍
一.defaultdict() 作用:根据数据创建字典时,需要为一些数据生成字典,而且对值得类型进行限定的时候,考虑defaultdict from collections import defaul ...
- python 开机 定时启动
Windows开机自动运行.py文件1.找到写好的.py文件,例如我的.py文件路径:D:\编程测试文件\untitled\03131105.py 2.选中文件03131105.py,右键——属性—— ...
- C语言 hello
#include <stdio.h> int main() { /* 我的第一个 C 程序 */ printf("Hello, World! \n"); ; } 实例解 ...
- C# 历遍对象属性
今天有个网友问如何历遍对象的所有公共属性,并且生成XML.采用序列化方式的话比较简单,我写个手工解析的例子,这样能让初学者更加理解也比较灵活,记录一下吧或许会有人用到. 对象模型: public cl ...
- Docker Swarm部署集群
一.Swarm简介 Swarm是Docker的一个编排工具,参考官网:https://docs.docker.com/engine/swarm/ Swarm 模式简介 要在Swarm模式下运行dock ...
- ssm动态sql语句
1.将上面的元素分为四组来演示,分别为:[if,where,trim],[if,set,trim],[choose,when,otherwise],[foreach] ________________ ...
- C#泛型集合之——哈希集合
1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型& ...
- 一文读懂内网、公网和NAT
我们做弱电监控系统的时候,都避免不了要跟IP地址打交道,比如摄像头.NVR.服务器等这些设备安装好之后,就需要给它们配上IP,那这个IP地址你了解嘛?今天我们就一起来聊聊什么是内网.公网和NAT地址转 ...
- bootstrap-combined.min.css 与 bootstrap.css冲突
使用bootstrap-paginator.js分页组件时,根据github上的demo,需要引入下列css: <link href="//netdna.bootstrapcdn.co ...
- 数据库与数据仓库的区别实际讲的是OLTP与OLAP的区别
什么是数据仓库 数据仓库,英文名称为Data Warehouse,可简写为DW或DWH.数据仓库,是为企业所有级别的决策制定过程,提供所有类型数据支持的战略集合.它出于分析性报告和决策支持目的而创建. ...