Springboot 使用JdbcTemplate
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的更多相关文章
- springboot之JdbcTemplate
springboot可以使用JdbcTemplate进行数据库访问,代码如下 添加pom文件 <parent> <groupId>org.springframework.boo ...
- SpringBoot使用JdbcTemplate
前言 本文是对SpringBoot使用JdbcTemplate操作数据库的一个介绍,,提供一个小的Demo供大家参考. 操作数据库的方式有很多,本文介绍使用SpringBoot结合JdbcTempla ...
- springboot 整合jdbcTemplate
springboot 整合jdbcTemplate 〇.搭建springboot环境(包括数据库的依赖) 一.添加依赖 如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖: ...
- springboot使用jdbcTemplate连接数据库
springboot使用jdbcTemplate连接数据库 1.pom.xml: <?xml version="1.0" encoding="UTF-8" ...
- 【使用篇二】SpringBoot使用JdbcTemplate操作数据库(12)
Spring对数据库的操作在jdbc上面做了深层次的封装,提供了JdbcTemplate模板. 在SpringBoot使用JdbcTemplate很简单: 引入数据库驱动包(mysql或oracle) ...
- 关于SpringBoot集成JDBCTemplate的RowMapper问题
JdbcTemplate 是Spring提供的一套JDBC模板框架,利用AOP 技术来解决直接使用JDBC时大量重复代码的问题.JdbcTemplate虽然没有MyBatis 那么灵活,但是直接使用J ...
- SpringBoot整合JdbcTemplate连接Mysql
import java.io.IOException; import javax.sql.DataSource; import org.apache.ignite.IgniteSystemProper ...
- springboot集成jdbcTemplate
这里使用springboot自带的jdbcTemplate连接mysql数据库 1. 添加依赖包 <!-- jdbc --> <dependency> <groupId& ...
- SpringBoot(四) SpringBoot整合JdbcTemplate
一.数据准备CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `username` varchar ...
随机推荐
- Shell初学(五)bash shell的基本功能
记住,所谓的bash shell 并不单纯指的是shell脚本,其实是Linux系统的所有指令集. shell脚本 只是汇总了指令集到文件,然后按流程和顺序执行. [1]如何查看我们的预设shell ...
- Eclipse编写代码时代码自动补全 + 防止按空格自动补全
都知道Eclipse中的自动补全代码是一个非常好用的工具 如下: 1.Windows——>Preferences——>Java–>Editor–>点击Content Asist ...
- CSP 俄罗斯方块(201604-2)
问题描述 俄罗斯方块是俄罗斯人阿列克谢·帕基特诺夫发明的一款休闲游戏. 游戏在一个15行10列的方格图上进行,方格图上的每一个格子可能已经放置了方块,或者没有放置方块.每一轮,都会有一个新的由4个小方 ...
- python-day42(正式学习)
目录 数据库 卸载 安装 连接数据库 用户信息查看 数据库的基本操作 表的基本操作 记录的基本操作 复习 今日内容 数据库配置 数据库修改信息 用户操作:重点 表的修改 创建表的完整语法 数据库表的引 ...
- USBIP源码分析
简介 在普通的电脑上,想使用USB设备,必须将插入到主机.USBIP却可以通过网络,让主机访问其他主机上的外部设备,而用户程序完全感知不到区别. usbip的文章在这里:https://pdfs.se ...
- JMX jconsole 的使用
JMX 1. JMX简单介绍 JMX的全称为Java Management Extensions. 顾名思义,是管理Java的一种扩展.这种机制可以方便的管理正在运行中的Java程序.常用于管理线程, ...
- springboot2.X版本得@Transactional注解事务不回滚不起作用
参考文章 https://my.oschina.net/happyBKs/blog/1624482 https://blog.csdn.net/u011410529/article/detail ...
- 图片哈希概论及python中如何实现对比两张相似的图片
Google 以图搜图的原理,其中的获取图片 hash 值的方法就是 AHash. 每张图片都可以通过某种算法得到一个 hash 值,称为图片指纹,两张指纹相近的图片可以认为是相似图片. 以图搜图的原 ...
- Spring 注入所得
Spring在注入的时候 @Autowired @Qualifier(value = "inpatientInfoInInterService") private Inpatien ...
- jumpserver开源堡垒机部署安装
0x01.前言 Jumpserver 是全球首款完全开源的堡垒机,使用 GNU GPL v2.0 开源协议,是符合 4A 的专业运维审计系统. Jumpserver 使用 Python / Djang ...