赞
踩
最近在学习springboot框架,利用springboot结合数据库实现基本的增删改查分页
首先是数据库的字段 id,name,type,description,id设置自增,因为是学习的基础的没有加version,deleted,create_time,update_time等字段
其次是导入依赖,因为在创建springboot工程的时候可以选择导入一些坐标,比如
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
结合mybatisplus能使开发更加快速,所以导入其坐标
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.1</version>
- </dependency>
数据源druid坐标
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.2.3</version>
- </dependency>
在application.yml下还要加入配置
#关于连接数据库的
spring: datasource: druid: password: 1234 username: root url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus: global-config: db-config: table-prefix: tb_ id-type: auto configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#设置端口为80
server: port: 80
1.实体类 entity Book
- package com.lh.entity;
-
-
- import lombok.Data;
-
- @Data
- public class Book {
-
- private Integer id;
- private String type;
- private String name;
- private String description;
-
- }
2.dao层 BookDao接口
结合mybatisplus,BookDao继承BaseMapper<Book> 开发更加快速,BaseMapper接口中写了若干种方法 增删改查等等
- package com.lh.dao;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.lh.entity.Book;
- import org.apache.ibatis.annotations.Mapper;
-
-
- @Mapper
- public interface BookDao extends BaseMapper<Book> {
- }
2.1 测试dao层代码是否正确
使用@springBootTest , @Test, @Autowired 注解
要测试BookDao 所以自动注入BookDao
- package com.lh.dao;
-
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.lh.entity.Book;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class testbookdao {
-
-
- @Autowired
- private BookDao bookDao;
-
- @Test
- void testupdate() {
- Book book=new Book();
- book.setId(45);
- book.setName("信503系统");
- book.setType("通3036信");
- book.setDescription("学习666基础");
- bookDao.updateById(book);
- }
-
- @Test
- void getall(){
- bookDao.selectList(null);
- }
-
- @Test //分页查询
- void testgetpage(){
- IPage page=new Page(2,3);
- bookDao.selectPage(page,null);
- System.out.println(page.getCurrent());
- System.out.println(page.getPages());
- System.out.println(page.getRecords());
- System.out.println(page.getSize());
- System.out.println(page.getTotal());
-
- }
-
-
- @Test//条件查询
- void testgetby(){
- String name="信";
- LambdaQueryWrapper<Book> lqw=new LambdaQueryWrapper<>();
- lqw.like(name!=null,Book::getName,name);
- bookDao.selectList(lqw);
- }
- }
2.1.1
这里直接测查询分页显示的还是全部数据
我们要加入一个拦截器
在config包下创建mabatisplusconfig类
- package com.lh.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 MybaitisplusConfig {
-
-
- @Bean //创建bean 交给spring管理
- //直接使用分页 控制台得不出来数据 需要创建拦截器
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
- MybatisPlusInterceptor mp=new MybatisPlusInterceptor();
- mp.addInnerInterceptor(new PaginationInnerInterceptor());
- return mp;
- }
-
- }
3.service层
3.1基础操作 BookService 编写各种方法 ,增删改查
- package com.lh.service;
-
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.lh.entity.Book;
- import java.util.List;
-
- public interface BookService {
-
- boolean save(Book book);
- boolean delete(Integer id);
- boolean update(Book book);
- Book getById(Integer id);
- List<Book> getall();
- IPage<Book> getpage(int current, int size);
- }
下面是BookServiceimpl 实现BookService
- package com.lh.service.impl;
-
-
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.lh.dao.BookDao;
- import com.lh.entity.Book;
- import com.lh.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 delete(Integer id) {
- return bookDao.deleteById(id)>0;
- }
-
- @Override
- public boolean update(Book book) {
- return bookDao.updateById(book)>0;
- }
-
- @Override
- public Book getById(Integer id) {
- return bookDao.selectById(id);
- }
-
- @Override
- public List<Book> getall() {
- return bookDao.selectList(null);
- }
-
- @Override
- public IPage<Book> getpage(int current, int size) {
- IPage<Book> page=new Page<>(current,size);
- return bookDao.selectPage(page,null);
- }
- }
3.2 利用mybatisplus快速开发 IBookService1 继承IService<Book>
接口下面可以继续写方法,但是尽量不要覆盖IService里面写的方法,避免加重负担
- package com.lh.service;
-
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.lh.entity.Book;
-
- public interface ibookservice1 extends IService<Book> {
-
- IPage<Book> getpage(Integer current,Integer size);
- }
下面是IBookServiceimpl1
这里我重写了一个查询分页的操作
- package com.lh.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.lh.dao.BookDao;
- import com.lh.entity.Book;
- import com.lh.service.ibookservice1;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
-
- @Service//业务层快速开发
- public class ibooserviceimpl1 extends ServiceImpl<BookDao, Book> implements ibookservice1 {
-
-
- @Autowired
- private BookDao bookDao;
-
- //可以重载或增加 不要覆盖原始操作
- @Override
- public IPage<Book> getpage(Integer current, Integer size) {
- IPage page=new Page<>(current,size);
- bookDao.selectPage(page,null);
- return page;
- }
- }
3.3测试service层
3.3.1 测试BookService 创建测试类testbookservice
- package com.lh.service;
-
-
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.lh.entity.Book;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- public class testbookservice {
-
- @Autowired
- private BookService bookService;
-
-
- @Test
- void testsave(){
- Book book=new Book();
- book.setName("电子技术");
- book.setType("通信");
- book.setDescription("学习666电子技术基础");
- bookService.save(book);
- }
-
- @Test
- void testdelete(){
- bookService.delete(46);
- }
-
- @Test
- void testupdate(){
- Book book=new Book();
- book.setId(45);
- book.setName("模拟电子技术");
- book.setType("通信");
- book.setDescription("学习555电子技术基础");
- bookService.update(book);
- }
- @Test
- void testgetbyid(){
- System.out.println(bookService.getById(45));
- }
-
- @Test
- void testgetpage(){
- IPage<Book> page = bookService.getpage(1, 3);
- System.out.println(page.getTotal());
- System.out.println(page.getSize());
- System.out.println(page.getCurrent());
- System.out.println(page.getPages());
- }
- }
3.3.2 测试ibookservice
- package com.lh.service;
-
- import com.lh.entity.Book;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- public class testibooservice1 {
-
- @Autowired
- private ibookservice1 ibookservice1;
-
- @Test
- void testinsert(){
- Book book=new Book();
- book.setName("单片adadaadadad机原理");
- book.setType("通信");
- book.setDescription("学习777rggrr电子技术基础");
- ibookservice1.save(book);
-
- }
-
- @Test
- void testdelete(){
- ibookservice1.removeById(49);
- }
-
- @Test
- void testupdate(){
- Book book=new Book();
- book.setId(50);
- book.setName("大学物理");
- book.setType("通信");
- book.setDescription("学习77技术基础");
- ibookservice1.updateById(book);
- }
-
- }
4.controller层 在页面上展示数据
4.1因为要让前端减少工作量,我们不能返回任意一数据,所以我们可以与前端约定返回的数据格式
所以可以创建一个类R,设置返回的数据格式
- package com.lh.controller.untils;
-
-
- import lombok.Data;
-
- /***
- * 前后端数据协议
- */
- @Data
- public class R {
-
- private boolean flag;
- private Object date;
-
- public R(){}
-
- public R(boolean flag){
- this.flag=flag;
-
- }
-
- public R(boolean flag,Object date){
- this.flag=flag;
- this.date=date;
-
- }
-
- }
4.2创建BookController 结合restful风格开发
- package com.lh.controller;
-
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.lh.controller.untils.R;
- import com.lh.entity.Book;
- import com.lh.service.ibookservice1;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- /***
- * 加入前后端数据协议
- */
-
- @RestController
- @RequestMapping("/books")
- public class BookController {
-
- @Autowired
- private ibookservice1 ibookservice1;
-
- @GetMapping
- public R selectAll(){
- return new R(true,ibookservice1.list());
- }
-
- @GetMapping("/{id}")
- public R selectById(@PathVariable Integer id){
-
- return new R(true,ibookservice1.getById(id));
- }
-
- @DeleteMapping("/{id}")
- public R delete(@PathVariable Integer id){
-
- return new R(ibookservice1.removeById(id));
- }
-
- @PutMapping
- public R update(@RequestBody Book book){
- return new R(ibookservice1.updateById(book));
- }
-
- @PostMapping ()
- public R save(@RequestBody Book book){
- return new R(ibookservice1.save(book));
- }
-
- @GetMapping("/{current}/{size}")
- public R getpage(@PathVariable Integer current,@PathVariable Integer size){
- return new R(true,ibookservice1.getpage(current, size));
-
- }
- }
4.3 启动引导类 springbootssmapplication利用postman测试BookController
- package com.lh;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class SpringbootssmApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringbootssmApplication.class, args);
- }
-
- }
4.3.1测试查全部
4.3.2测试查分页
4.3.3测试新增
4.3.4 测试更新
首先我们先随便插入一个数据
然后在进行更改
更改成功!!!
springboot,mybatisplus和注解开发真的很强,增强了开发效率!!!
欢迎大佬教我学习springboot!!!
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。