目录:
pom.xml(把相应的依赖下载好)
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.1.5
com.example
_20231024_ssmp
0.0.1-SNAPSHOT
_20231024_ssmp
_20231024_ssmp
17
org.springframework.boot
spring-boot-starter-web
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
3.5.2
com.alibaba
druid-spring-boot-starter
1.2.9
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
Book.class
package com.example.domain;
import lombok.Data;
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
小结:
技术实现方案
导入MyBatisPlus与Druid对应的starter
继承BaseMapper并指定泛型
制作测试类测试结果
BookDao.interface
package com.example.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookDao extends BaseMapper {
}
Book.class
package com.example.domain;
import lombok.Data;
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
application.yml
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3308/test_db
username: root
password: 666666
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
id-type: auto
BookDaoTest
package com.example.dao;
import com.example.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class BookDaoTest {
@Autowired
public BookDao bookDao;
@Test
void testGetById() {
System.out.println(bookDao.selectById(1));
}
@Test
void testSave() {
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
int ret = bookDao.insert(book);
if (ret == 0) {
System.out.println("新增失败!");
} else {
System.out.println("新增成功!");
}
}
@Test
void testUpdate() {
Book book = new Book();
book.setId(13);
book.setType("aaaa");
book.setName("aaaa");
book.setDescription("aaaa");
int ret = bookDao.updateById(book);
if (ret == 0) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
@Test
void testDelete() {
int ret = bookDao.deleteById(13);
if (ret == 0) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
@Test
void testGetAll() {
System.out.println(bookDao.selectList(null));
}
}
运行结果:
小结:
application.yml
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3308/test_db
username: root
password: 666666
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
运行结果:
小结:
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.18-SNAPSHOT
com.example
_20231024_ssmp1
0.0.1-SNAPSHOT
_20231024_ssmp1
_20231024_ssmp1
1.8
org.springframework.boot
spring-boot-starter-web
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
3.5.2
com.alibaba
druid-spring-boot-starter
1.2.9
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
false
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
false
MPConfig.class
package com.example.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
BookDao.interface
package com.example.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookDao extends BaseMapper {
}
Book.class
package com.example.domain;
import lombok.Data;
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
applicaion.yml
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3308/test_db
username: root
password: 666666
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
BookDaoTest
package com.example.dao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class BookDaoTest {
@Autowired
public BookDao bookDao;
@Test
void testGetById() {
System.out.println(bookDao.selectById(1));
}
@Test
void testSave() {
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
int ret = bookDao.insert(book);
if (ret == 0) {
System.out.println("新增失败!");
} else {
System.out.println("新增成功!");
}
}
@Test
void testUpdate() {
Book book = new Book();
book.setId(13);
book.setType("aaaa");
book.setName("aaaa");
book.setDescription("aaaa");
int ret = bookDao.updateById(book);
if (ret == 0) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
@Test
void testDelete() {
int ret = bookDao.deleteById(13);
if (ret == 0) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
@Test
void testGetAll() {
System.out.println(bookDao.selectList(null));
}
@Test
void testGetPage() {
IPage page = new Page(2, 5);
bookDao.selectPage(page, null);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getPages());
System.out.println(page.getRecords());
}
}
项目结构:
运行结果:
分页操作需要设定分页对象IPage
IPage对象中封装了分页操作中的所有数据
分页操作是在MyBatisPlus的常规操作基础上增强得到,内部是动态的拼写SQL语句,因此需要增强对应的功能,使用MyBatisPlus拦截器实现
小结:
使用QueryWrapper对象封装查询条件,推荐使用LambdaQueryWrapper对象,所有查询操作封装成方法调用
代码示例:
BookDaoTest
package com.example.dao;
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.example.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class BookDaoTest {
@Autowired
public BookDao bookDao;
@Test
void testGetById() {
System.out.println(bookDao.selectById(1));
}
@Test
void testSave() {
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
int ret = bookDao.insert(book);
if (ret == 0) {
System.out.println("新增失败!");
} else {
System.out.println("新增成功!");
}
}
@Test
void testUpdate() {
Book book = new Book();
book.setId(13);
book.setType("aaaa");
book.setName("aaaa");
book.setDescription("aaaa");
int ret = bookDao.updateById(book);
if (ret == 0) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
@Test
void testDelete() {
int ret = bookDao.deleteById(13);
if (ret == 0) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
@Test
void testGetAll() {
System.out.println(bookDao.selectList(null));
}
@Test
void testGetPage() {
IPage page = new Page(2, 5);
bookDao.selectPage(page, null);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getPages());
System.out.println(page.getRecords());
}
@Test
void testGetBy() {
QueryWrapper qw = new QueryWrapper<>();
qw.like("name", "Spring");
bookDao.selectList(qw);
}
@Test
void testGetBy2() {
String name = "Spring";
LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
lqw.like(name != null, Book::getName, name);
bookDao.selectList(lqw);
}
}
运行结果:
小结:
BookService.interface
package com.example.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.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 getAll();
IPage getPage(int currentPage, int pageSize);
}
BookServiceImpl.class
package com.example.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.BookDao;
import com.example.domain.Book;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl 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 getAll() {
return bookDao.selectList(null);
}
@Override
public IPage getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage, pageSize);
return bookDao.selectPage(page, null);
}
}
BookServiceTest.class
package com.example.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class BookServiceTest {
@Autowired
private BookService bookService;
@Test
void save() {
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
Boolean ret = bookService.save(book);
if (ret == false) {
System.out.println("新增失败!");
} else {
System.out.println("新增成功!");
}
}
@Test
void update() {
Book book = new Book();
book.setId(13);
book.setType("aaaa");
book.setName("aaaa");
book.setDescription("aaaa");
Boolean ret = bookService.update(book);
if (ret == false) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
@Test
void delete() {
Boolean ret = bookService.delete(16);
if (ret == false) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
@Test
void getById() {
System.out.println(bookService.getById(4));
}
@Test
void getAll() {
System.out.println(bookService.getAll());
}
@Test
void getPage() {
IPage page = bookService.getPage(2, 5);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getPages());
System.out.println(page.getRecords());
}
}
项目结构:
Service层接口定义与数据层接口定义具有较大区别,不要混用
接口定义
实现类定义
测试类定义
小结:
IBookService.class
package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.domain.Book;
public interface IBookService extends IService {
boolean insert(Book book);
boolean modify(Book book);
boolean delete(Integer id);
Book get(Integer id);
}
BookServiceImpl1.class
package com.example.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.BookDao;
import com.example.domain.Book;
import com.example.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl1 extends ServiceImpl implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public boolean insert(Book book) {
return bookDao.insert(book) > 0;
}
@Override
public boolean modify(Book book) {
return bookDao.updateById(book) > 0;
}
@Override
public boolean delete(Integer id) {
return bookDao.deleteById(id) > 0;
}
@Override
public Book get(Integer id) {
return bookDao.selectById(id);
}
}
IBookServiceTest.class
package com.example.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class IBookServiceTest {
@Autowired
private IBookService bookService;
@Test
void save() {
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
Boolean ret = bookService.save(book);
if (ret == false) {
System.out.println("新增失败!");
} else {
System.out.println("新增成功!");
}
}
@Test
void update() {
Book book = new Book();
book.setId(18);
book.setType("aaaa");
book.setName("aaaa");
book.setDescription("aaaa");
Boolean ret = bookService.updateById(book);
if (ret == false) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
@Test
void delete1() {
Boolean ret = bookService.removeById(17);
if (ret == false) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
@Test
void getById() {
System.out.println(bookService.getById(4));
}
@Test
void getAll() {
System.out.println(bookService.list());
}
@Test
void getPage() {
IPage page = new Page(2, 5);
bookService.page(page);
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getPages());
System.out.println(page.getRecords());
}
// ------------------------追加的功能-----------------------------------
@Test
void insert() {
Book book = new Book();
book.setType("测试数据123");
book.setName("测试数据123");
book.setDescription("测试数据123");
Boolean ret = bookService.insert(book);
if (ret == false) {
System.out.println("新增失败!");
} else {
System.out.println("新增成功!");
}
}
@Test
void modify() {
Book book = new Book();
book.setId(18);
book.setType("aaaa");
book.setName("aaaa");
book.setDescription("aaaa");
Boolean ret = bookService.modify(book);
if (ret == false) {
System.out.println("修改失败!");
} else {
System.out.println("修改成功!");
}
}
@Test
void delete() {
Boolean ret = bookService.delete(17);
if (ret == false) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
@Test
void get() {
System.out.println(bookService.get(4));
}
}
项目结构:
快速开发方案
接口定义
实现类定义
实现类追加功能
小结:
getAll
getById
delete
save
update
getPage
BookController.class
package com.example.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.Book;
import com.example.service.IBookService;
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 bookService;
@GetMapping
public List getAll() {
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book) {
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book) {
return bookService.modify(book);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id) {
return bookService.delete(id);
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
@GetMapping("{currentPage}/{pageSize}")
public IPage getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
return bookService.getPage(currentPage, pageSize);
}
}
IBookService.interface
package com.example.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.domain.Book;
public interface IBookService extends IService {
boolean insert(Book book);
boolean modify(Book book);
boolean delete(Integer id);
Book get(Integer id);
IPage getPage(int currentPage, int pageSize);
}
BookServiceImpl1
package com.example.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.BookDao;
import com.example.domain.Book;
import com.example.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl1 extends ServiceImpl implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public boolean insert(Book book) {
return bookDao.insert(book) > 0;
}
@Override
public boolean modify(Book book) {
return bookDao.updateById(book) > 0;
}
@Override
public boolean delete(Integer id) {
return bookDao.deleteById(id) > 0;
}
@Override
public Book get(Integer id) {
return bookDao.selectById(id);
}
@Override
public IPage getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage, pageSize);
bookDao.selectPage(page, null);
return page;
}
}
项目结构:
小结:
R.class
package com.example.utils;
import lombok.Data;
@Data
public class R {
private Boolean flag;
private Object data;
public R() {
}
public R(Boolean flag) {
this.flag = flag;
}
public R(Boolean flag, Object data) {
this.flag = flag;
this.data = data;
}
}
BookController1.class
package com.example.controller;
import com.example.domain.Book;
import com.example.service.IBookService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController1 {
@Autowired
private IBookService bookService;
@GetMapping
public R getAll() {
return new R(true, bookService.list());
}
@PostMapping
public R save(@RequestBody Book book) {
return new R(bookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book) {
return new R(bookService.modify(book));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id) {
return new R(bookService.delete(id));
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id) {
return new R(true, bookService.getById(id));
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage, @PathVariable int pageSize) {
return new R(true, bookService.getPage(currentPage, pageSize));
}
}
项目结构:
postman测试结果:
getAll
getById
delete
update
save
getPage
设计表现层返回结果的模型类, 用于后端与前端进行数据格式统一,也称为前后端数据协议
表现层接口统一返回值类型结果
小结:
项目结构:(项目地址:JavaEE: 练习的代码 - Gitee.com)
运行结果:
前后端分离结构设计中页面归属前端服务器
单体工程中页面放置在resources目录下的static目录中(建议执行clean)
前端发送异步请求,调用后端接口
小结:
列表页
运行结果:
小结:
弹出添加窗口
清除数据
添加
取消添加
小结:
删除
小结:
弹出修改窗口
删除消息维护
小结:
修改
取消添加和修改
小结:
业务操作成功或失败返回数据格式
后台代码BUG导致数据格式不统一性
对异常进行统一处理,出现异常后,返回指定信息
修改表现层返回结果的模型类,封装出现异常后对应的信息
页面消息处理,没有传递消息加载默认消息,传递消息后加载指定消息
可以在表现层Controller中进行消息统一处理
目的:国际化
页面消息处理
小结:
页面使用el分页组件添加分页功能
定义分页组件需要使用的数据并将数据绑定到分页组件
替换查询全部功能为分页功能
分页查询
使用路径参数传递分页数据或封装对象传递数据
加载分页数据
分页页码值切换
小结:
对查询结果进行校验,如果当前页码值大于最大页码值,使用最大页码值作为当前页码值重新查询
小结:
查询条件数据封装
页面数据模型绑定
组织数据成为get请求发送的数据
条件参数组织可以通过条件判定书写的更简洁
Controller接收参数
业务层接口功能开发
Controller调用业务层分页条件查询接口
页面回显数据
小结:
基于SpringBoot的SSMP整合案例
项目地址:JavaEE: 练习的代码 - Gitee.com