博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:SpringBoot专栏(每日更新)
SpringBoot入门案例-阿里云版和纯手工版:点击查看
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起学习!一起加油!
详细步骤操作 点击直接查看
不同版本的搭建项目在我springboot专栏里,本次项目采用的是阿里云版。模块的创建就不用多说了。
⭐注意:选择需要的技术
⭐Lombok开发工具可以简化实体类的开发。
导入druid的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
设置服务器端口号:
server:
port: 80
Lombok注解
常用注解: @Data @setter @Getter
Book
package com.jkj.domain;
import lombok.*;
//@Setter
//@Getter
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
编写yml
server:
port: 80
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
编写Dao层(采用MyBatis-Plus技术)
package com.jkj.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jkj.domain.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookMapper extends BaseMapper<Book> {
}
测试MyBatis-Plus的其他操作
package com.jkj.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.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest()
public class SsmpTest {
@Autowired
private BookMapper bookMapper;
@Test
void findById() {
System.out.println(bookMapper.selectById(2));
}
@Test
void insert() {
Book book=new Book();
book.setName("springboot");
book.setType("框架");
book.setDescription("好用");
bookMapper.insert(book);
}
@Test
void updateById() {
Book book=new Book();
book.setId(4);
book.setDescription("666");
bookMapper.updateById(book);
}
@Test
void delete() {
bookMapper.deleteById(4);
}
@Test
void findAll() {
bookMapper.selectList(null);
}
}
注意:在进行添加操作过程中,会爆id自增的问题在yml文件中添加这行代码: id-type: auto
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
id-type: auto
编辑yml
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
测试类
@Test
void findAll() {
bookMapper.selectList(null);
}
编写拦截器MPConfig类
package com.jkj.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(){
//定义MP拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
分页测试
@Test
void page(){
IPage page = new Page(2,5);
bookMapper.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 ByCondition1(){
String name="一";
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
//支持动态拼写查询条件
lqw.like(name!=null,Book::getName,name);
bookMapper.selectList(lqw);
}
@Test
void ByCondition(){
QueryWrapper<Book> wrapper = new QueryWrapper<>();
wrapper.like("name","一");
bookMapper.selectList(wrapper);
}
package com.jkj.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jkj.domain.Book;
import java.util.List;
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delete(Integer id);
Book selectById(Integer id);
List<Book> SelectAll();
IPage<Book> getPage(int currentPage,int pageSize);
}
实现类
package com.jkj.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.dao.BookMapper;
import com.jkj.domain.Book;
import com.jkj.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 BookMapper bookMapper;
@Override
public Boolean save(Book book) {
return bookMapper.insert(book)>0;
}
@Override
public Boolean update(Book book) {
return bookMapper.updateById(book)>0;
}
@Override
public Boolean delete(Integer id) {
return bookMapper.deleteById(id)>0;
}
@Override
public Book selectById(Integer id) {
return bookMapper.selectById(id);
}
@Override
public List<Book> SelectAll() {
return bookMapper.selectList(null);
}
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookMapper.selectPage(page,null);
return page;
}
}
测试类
package com.jkj.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class BookServiceTest {
@Autowired(required = true)
private BookService bookService;
@Test
void testGetById(){
System.out.println(bookService.selectById(1));
}
@Test
void testSave(){
Book book = new Book();
book.setType("测试");
book.setName("测试");
book.setDescription("测试");
bookService.save(book);
}
@Test
void testUpdate(){
Book book = new Book();
book.setId(11);
book.setType("测试1");
book.setName("测试1");
book.setDescription("测试1");
bookService.update(book);
}
@Test
void testDelete(){
bookService.delete(14);
}
@Test
void testGetAll(){
bookService.SelectAll();
}
@Test
void testGetPage(){
IPage<Book> page = new Page<Book>(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());
}
}
package com.jkj.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jkj.domain.Book;
public interface IBookService extends IService<Book> {
}
实现类
package com.jkj.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkj.dao.BookMapper;
import com.jkj.domain.Book;
import com.jkj.service.IBookService;
import org.springframework.stereotype.Service;
@Service
public class IBookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
}
测试类
package com.jkj.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class IBookServiceTest {
@Autowired
private IBookService iBookService;
@Test
void save(){
Book b=new Book();
b.setType("文学");
b.setName("白鹿原");
b.setDescription("好看");
iBookService.save(b);
}
@Test
void update(){
Book b=new Book();
b.setId(15);
b.setType("文学");
b.setName("白鹿原");
b.setDescription("666");
iBookService.updateById(b);
}
@Test
void delete(){
iBookService.removeById(15);
}
@Test
void findById(){
System.out.println(iBookService.getById(1));
}
@Test
void findAll(){
iBookService.list();
}
@Test
void page(){
IPage<Book> page = new Page<Book>(2,5);
iBookService.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());
}
}
编写Controller类
package com.jkj.controller;
import com.jkj.domain.Book;
import com.jkj.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/*@RestController
@RequestMapping("/book")*/
public class BookController {
@Autowired
private BookService bookService;
@GetMapping
public List<Book> selectAll(){
return bookService.SelectAll();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.update(book);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id){
return bookService.delete(id);
}
@GetMapping("{id}")
public Book selectById(@PathVariable Integer id){
return bookService.selectById(id);
}
}
启动postman测试
⭐查全部
⭐通过id查询
⭐添增
⭐修改
修改之后再查询一次
⭐删除
编写R.java
package com.jkj.controller.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;
}
}
编写IController类
package com.jkj.controller;
import com.jkj.domain.Book;
import com.jkj.controller.utils.R;
import com.jkj.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/book")
public class IBookController {
@Autowired
private IBookService iBookService;
@GetMapping
public R selectAll(){
return new R(true,iBookService.list());
}
@PostMapping
public R save(@RequestBody Book book){
/*R r = new R();
boolean flag = iBookService.save(book);
r.setFlag(flag);*/
return new R(iBookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book){
return new R(iBookService.updateById(book));
}
@DeleteMapping("{id}")
public R delete(@PathVariable Integer id){
return new R(iBookService.removeById(id));
}
@GetMapping("{id}")
public R selectById(@PathVariable Integer id){
return new R(true,iBookService.getById(id));
}
}
postman测试,如上。