选WEB项目和sql驱动
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
server:
port: 80
spring:
druid:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cs?serverTimezone=UTC
username: root
password: root
# 添加表前缀
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
# id自增
id-type: auto
/*
Navicat Premium Data Transfer
Source Server : 2020_1_16win
Source Server Type : MySQL
Source Server Version : 50729
Source Host : localhost:3306
Source Schema : ssm_db
Target Server Type : MySQL
Target Server Version : 50729
File Encoding : 65001
Date: 09/01/2022 22:32:11
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tbl_book
-- ----------------------------
DROP TABLE IF EXISTS `tbl_book`;
CREATE TABLE `tbl_book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tbl_book
-- ----------------------------
INSERT INTO `tbl_book` VALUES (1, '计算机理论', 'Spring实战 第5版', 'Spring入门经典教程,深入理解Spring原理技术内幕');
INSERT INTO `tbl_book` VALUES (2, '计算机理论', 'Spring 5核心原理与30个类手写实战', '十年沉淀之作,手写Spring精华思想');
INSERT INTO `tbl_book` VALUES (3, '计算机理论', 'Spring 5设计模式', '全方位解析面向Web应用的轻量级框架,带你成为Spring MVC开发高手');
INSERT INTO `tbl_book` VALUES (4, '计算机理论', 'Spring MVC+MyBatis开发从入门到项目实战', '源码级剖析Spring框架,适合已掌握Java基础的读者');
INSERT INTO `tbl_book` VALUES (5, '计算机理论', '轻量级Java Web企业应用实战', '源码级剖析Spring框架,适合已掌握Java基础的读者');
INSERT INTO `tbl_book` VALUES (6, '计算机理论', 'Java核心技术卷I基础知识(原书第11版)', 'Core Java第11版,Jolt大奖获奖作品,针对Java SE9、10、11全面更新');
INSERT INTO `tbl_book` VALUES (7, '计算机理论', '深入理解Java虚拟机', '5个维度全面剖析JVM,大厂面试知识点全覆盖');
INSERT INTO `tbl_book` VALUES (8, '计算机理论', 'Java编程思想(第4版)', 'Java学习必读经典,殿堂级著作!赢得了全球程序员的广泛赞誉');
INSERT INTO `tbl_book` VALUES (9, '计算机理论', '零基础学Java(全彩版)', '零基础自学编程的入门图书,由浅入深,详解Java语言的编程思想和核心技术');
INSERT INTO `tbl_book` VALUES (10, '市场营销', '直播就该这么做:主播高效沟通实战指南', '成长为网红的秘密都在书中');
INSERT INTO `tbl_book` VALUES (11, '市场营销', '直播销讲实战—本通', '和秋叶—起学系列网络营销书籍');
INSERT INTO `tbl_book` VALUES (12, '市场营销', '直播带货:淘宝、天猫直播从新手到高手', '—本教你如何玩转直播的书,10堂课轻松实现带货月入3W+');
SET FOREIGN_KEY_CHECKS = 1;
package com.springboot.domain;
import com.sun.tracing.dtrace.ArgsAttributes;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@ToString
@AllArgsConstructor //有参构造
@NoArgsConstructor //无参构造
@Data
public class Book {
private int id;
private String type;
private String name;
private String description;
}
package com.springboot.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.domain.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
package com.springboot;
import com.springboot.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SsmpDemoApplicationTests {
@Autowired
BookDao bookDao;
@Test
void contextLoads() {
System.out.println(bookDao.selectById(1));
}
}
package com.springboot.service;
import com.springboot.domain.Book;
import java.util.List;
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delete(Integer id);
Book getById(Integer id);
List<Book>getAll();
}
业务层实现
package com.springboot.service.impl;
import com.springboot.dao.BookDao;
import com.springboot.domain.Book;
import com.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service //定义成业务层对应的bean
public class BookServicelmpl implements BookService {
// 数据层接口
@Autowired
private BookDao bookDao;
@Override
public Boolean save(Book book) {
return bookDao.insert(book)>0;
}
@Override
public Boolean update(Book book) {
return bookDao.updateById(book)>0;
}
@Override
public Boolean delete(Integer id) {
return bookDao.deleteById(id) >0;
}
@Override
public Book getById(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
}
package com.springboot;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dao.BookDao;
import com.springboot.domain.Book;
import com.springboot.service.impl.BookServicelmpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import sun.dc.pr.PRError;
import javax.management.QueryEval;
@SpringBootTest
class SsmpDemoApplicationTests {
@Autowired
private BookServicelmpl bookServicelmpl;
@Test
void getById() {
System.out.println(bookServicelmpl.getById(1));
}
}
继承IService接口
package com.springboot.service;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.springboot.domain.Book;
public interface IBookService extends IService<Book> {
}
通过ServiceImpl就完成了所有方法的实现
package com.springboot.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.dao.BookDao;
import com.springboot.domain.Book;
import com.springboot.service.IBookService;
import org.springframework.stereotype.Service;
// ServiceImpl 要给实现类和模型类
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao,Book> implements IBookService {
}
package com.springboot;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dao.BookDao;
import com.springboot.domain.Book;
import com.springboot.service.IBookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import sun.dc.pr.PRError;
import javax.management.QueryEval;
@SpringBootTest
class SsmpDemoApplicationTests {
@Autowired
private IBookService iBookService;
@Test
void getById() {
System.out.println(iBookService.getById(1));
}
}
不可和接口现有的方法重复
package com.springboot.service;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.springboot.domain.Book;
public interface IBookService extends IService<Book> {
boolean saveBook(Book book);
}
单独去写他的实现就可以了
package com.springboot.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.dao.BookDao;
import com.springboot.domain.Book;
import com.springboot.service.IBookService;
import org.springframework.stereotype.Service;
// ServiceImpl 要给实现类和模型类
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao,Book> implements IBookService {
private BookDao bookDao;
@Override
public boolean saveBook(Book book) {
return bookDao.insert(book)>0;
}
}
package com.springboot.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.domain.Book;
import com.springboot.service.IBookService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
// 调用业务层
@Autowired
private IBookService iBookService;
@GetMapping
public List<Book>getAll(){
// 查询全部
return iBookService.list();
}
@PutMapping
public boolean update(@RequestBody Book book){
// 修改
return iBookService.updateById(book);
}
@PostMapping
public boolean save(@RequestBody Book book){
// 新增
return iBookService.save(book);
}
@DeleteMapping("{id}")
public boolean delete(@PathVariable Integer id){
// 删除单个
return iBookService.removeById(id);
}
// http:localhost/book/2
@GetMapping("{id}")
public Book getByID( Integer id){
// 查询单个
return iBookService.getById(id);
}
// 分页
@GetMapping("{start}/{end}")
public IPage<Book> getPage(@PathVariable Integer start,@PathVariable Integer end){
IPage page = new Page(start,end);
return iBookService.page(page);
}
}