1 使用JdbcTemplate
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF8&characterSetResults=UTF8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
目录结构
数据库表
book表 属性 id name adress time
model book.class
package am.sst.book.model;
public class Book {
private String id; //图书Id
private String name; //图书名称
private String adress; //出版社
private Integer time; //发行时间
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public Integer getTime() {
return time;
}
public void setTime(Integer time) {
this.time = time;
}
}
Dao bookDao.class
package am.sst.book.Dao;
import am.sst.book.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class BookDao {
@Autowired
private JdbcTemplate jdbcTemplate; //加载JdbcTemplate 注入
//添加图书方法
public String add(Book book){
String sql = "insert into book(id,name,adress,time) value (?,?,?,?)";
try{
jdbcTemplate.update(sql,book.getId(),book.getName(),book.getAdress(),book.getTime());
return "1";
}catch (DataAccessException e){
e.printStackTrace();
return "0";
}
}
//查询图书方法
public List findAll(){
String sql = "select * from book ";
List bookList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Book.class));
return bookList;
}
public Book findOne(String id){
String sql = "select * from book where id = "+id;
List bookList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Book.class));
return bookList.get(0); //取其中一个对象出来
}
//修改图书方法
public String upbook(Book book){
String sql = "update book set name = ?,adress = ?,time = ? where id = ?";
try{
jdbcTemplate.update(sql,book.getName(),book.getAdress(),book.getTime(),book.getId());
return "1";
}catch (DataAccessException e){
e.printStackTrace();
return "0";
}
}
//删除图书信息
public String delbook(String id){
String sql = "delete from book where id = ?";
try{
jdbcTemplate.update(sql,id);
return "1";
}catch (DataAccessException e){
e.printStackTrace();
return "0";
}
}
}
Service bookService.class
package am.sst.book.Service;
import am.sst.book.Dao.BookDao;
import am.sst.book.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
private BookDao bookDao;
//添加方法
public String add(Book book){
return bookDao.add(book);
}
//查询方法
public List findAll(){
return bookDao.findAll();
}
public Book findOne(String id){
return bookDao.findOne(id);
}
//修改方法
public String upbook(Book book){
return bookDao.upbook(book);
}
//删除方法
public String delbook(String id){
return bookDao.delbook(id);
}
}
Controller Controller
package am.sst.book.Controller;
import am.sst.book.Service.BookService;
import am.sst.book.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/hi")
public class Controller {
@Autowired
private BookService bookService;
//新增接口
@PostMapping("/add")
public String add(@RequestBody Book book){
return bookService.add(book);
}
//查询接口
@GetMapping("/findAll")
public List findAll(){
return bookService.findAll();
}
@GetMapping("/findOne")
public Book findOne(String id){
return bookService.findOne(id);
}
//修改接口
@PostMapping("/upbook")
public String upbook(Book book){
return bookService.upbook(book);
}
//删除接口
@PostMapping("/delbook")
public String delbook(String id){
return bookService.delbook(id);
}
}
启动程序
package am.sst.book;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootExampleApplication.class, args);
}
}
启动服务运行,可以使用Postman 以POST方式访问 http://locahost:8080/hi/(add)具体方法名接口 实现



