Springboot 使用JdbcTemplate

book

package com.draymonder.book.jdbc;

public class Book {
private Integer id;
private String name;
private String author; @Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
}
}

bookDao

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; @Repository
public class BookDao { @Autowired
JdbcTemplate jdbcTemplate; public int addBook(Book book) {
return jdbcTemplate
.update("insert into book(name, author) values (?, ?)", book.getName(), book.getAuthor());
} public int updateBook(Book book) {
return jdbcTemplate
.update("update book set name=?, author=? where id=?", book.getName(), book.getAuthor(),
book.getId());
} public int deleteBookById(Integer id) {
return jdbcTemplate.update("delete from book where id=?", id);
} public Book getBookById(Integer id) {
return jdbcTemplate.queryForObject("select * from book where id=?", new BeanPropertyRowMapper<>(Book.class), id);
} public List<Book> getAllBooks() {
return jdbcTemplate.query("select * from book", new BeanPropertyRowMapper<>(Book.class));
} }

bookService

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class BookService { @Autowired
BookDao bookDao; public int addBook(Book book) {
return bookDao.addBook(book);
} public int updateBook(Book book) {
return bookDao.updateBook(book);
} public int deleteBookById(Integer id) {
return bookDao.deleteBookById(id);
} public Book getBookById(Integer id) {
return bookDao.getBookById(id);
} public List<Book> getAllBooks() {
return bookDao.getAllBooks();
} }

bookController

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class BookController {
@Autowired
BookService bookService; @GetMapping("/bookOps")
public void bookOps() {
Book b1 = new Book();
b1.setName("西厢记");
b1.setAuthor("王实甫");
int i = bookService.addBook(b1);
System.out.println("add book >>> " + i); Book b2 = new Book();
b2.setId(1);
b2.setName("朝花夕拾");
b2.setAuthor("鲁迅");
int updateBook = bookService.updateBook(b2);
System.out.println("update book >>> " + updateBook); Book b3 = bookService.getBookById(1);
System.out.println("get book >>> " + b3); int delete = bookService.deleteBookById(1);
System.out.println("delete book >>> " + delete); List<Book> allBooks = bookService.getAllBooks();
allBooks.forEach(System.out::println);
}
}

Springboot 使用JdbcTemplate的更多相关文章

  1. springboot之JdbcTemplate

    springboot可以使用JdbcTemplate进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boo ...

  2. SpringBoot使用JdbcTemplate

    前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...

  3. springboot 整合jdbcTemplate

    springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: ...

  4. springboot使用jdbcTemplate连接数据库

    springboot使用jdbcTemplate连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8" ...

  5. 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)

    Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...

  6. 关于SpringBoot集成JDBCTemplate的RowMapper问题

    JdbcTemplate 是Spring提供的一套JDBC模板框架,利用AOP 技术来解决直接使用JDBC时大量重复代码的问题.JdbcTemplate虽然没有MyBatis 那么灵活,但是直接使用J ...

  7. SpringBoot整合JdbcTemplate连接Mysql

    import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProper ...

  8. springboot集成jdbcTemplate

    这里使用springboot自带的jdbcTemplate连接mysql数据库 1. 添加依赖包 <!-- jdbc --> <dependency> <groupId& ...

  9. SpringBoot(四) SpringBoot整合JdbcTemplate

    一.数据准备CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `username` varchar ...

随机推荐

  1. 四则运算计算器的微信小程序_1 界面

    主界面wxml文件: page{ height:100%; } .content{ min-height:100%; display:flex; flex-direction:column; alig ...

  2. Clion 常用功能

    1.创建新文件并加入项目 打开CMakeList.txt,加入这样的一段话,随后点击图中的Reload changes add_executable(项目名 文件名) 2.文件重命名,移动,复制,移除 ...

  3. 微信小程序全局属性

    通用全局属性:(前端也有) Infinity 代表正的无穷大的数值. NaN 指示某个值是不是数字值. undefined 指示未定义的值. 通用全局函数:(前端也有) decodeURI() 解码某 ...

  4. 使用Python基于TensorFlow的CIFAR-10分类训练

    TensorFlow Models GitHub:https://github.com/tensorflow/models Document:https://github.com/jikexueyua ...

  5. JAVA文件类工具

    FileUtil package cn.jiangzeyin.util.file; import org.springframework.util.Assert; import java.io.*; ...

  6. QVector与QMap查找效率实战(QMap快N倍,因为QVector是数组,QMap是有序二叉树,查找的时候是N和LogN的速度对比)

    因为项目使用QVector,太慢了,听说QMap比QVector查找时快,所以写一个小程序试试: 从30000个数据中找5000个 程序运行截图如下: QVector QMap 一样的数据,找一样的数 ...

  7. redis 学习(16)-- redis 持久化

    redis 持久化 什么是持久化 redis 将所有数据保持在内存中,对数据的更新将异步地保存在磁盘中 持久化的方式 1. 快照 快照是某时某刻对数据的完整备份. 在: MySQL Dump Redi ...

  8. mac系统下Eclipse + pydev配置python Interpreter

    mac系统下Eclipse + pydev配置python Interpreter   之前都在windows下使用Eclipse + pydev 进行开发,未发现什么异常,最近对wxpy.itcha ...

  9. 依赖注入 php

    依赖注入:将当前类依赖的对象,以参数的方式注入到当前类中,简称依赖注入 <?php class Mi { public function size() { return '5.99寸全面屏'; ...

  10. WooyunWifi路由器

    WooyunWifi 初始化配置 为了开始使用您的WooyunWifi路由器,您需要对WooyunWifi进行初始化配置,这些配置主要位于Openwrt Luci管理界面中,如果您对Openwrt路由 ...