栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 写出一个简单的接口 实现前端对数据库的数据访问

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 写出一个简单的接口 实现前端对数据库的数据访问

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)具体方法名接口 实现

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号