如何使用MybatisPlus快速进行增删改查详解
前言:
mybatisplus 可以说是对mybatis更好的拓展,一些简单的增删改查的操作已经被作者实现,我们只需引用即可。
1.数据库建表
这里使用的是MySQL数据库,表名为student
2.新建一个springboot项目
这里使用的idea
(1)、引入相应的jar包
修改一下springboot的版本 最好与此一致,其他版本不确定是否兼容
这里如有需要复制时,注意空白格,直接复制可能会报错
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </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> </dependencies>
(2)、快速启动项目
在此之前,先看看一看我的项目结构
新建一个controller包,在controller包下新建一个HelloController.java
package com.zhu.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello"; } }
springboot启动类,运行main即可
package com.zhu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MybatisplusDemoApplication { public static void main(String[] args) { SpringApplication.run(MybatisplusDemoApplication.class, args); } }
在浏览器(这里使用的谷歌浏览器)中输入地址: http://localhost:8080/test/hello
至此,一个springboot项目快速启动完成,下面我们需要引入mybatisplus相关依赖
3.springboot结合mybatisplus
(1)、引入mybatisplus以及其他依赖
<!--mybatisplus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--数据库连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
(2)、创建application.yml文件,修改配置
# DataSource Config spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/此处为你的数据库名?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: 你的数据库用户名 password: 你的数据库密码 mybatis-plus: # xml文件扫描 mapper-locations: classpath*:/mapper/**Mapper.xml
(3)、创建mybaisplus配置类
package com.zhu.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @MapperScan("com.zhu.mapper")//mapper接口扫描注解 @EnableTransactionManagement public class MyBatisPlusConfig {//分页配置,本博客不展示分页操作 @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
(4)、创建实体类
package com.zhu.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; /** * <p> * * </p> * * @author xiaozhu * @since 2022-04-13 */ //使用lombok,简化了代码,不用书写set get等方法 @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class Student implements Serializable { private static final long serialVersionUID = 1L; /** * 自动递增 */ @TableId(value = "sno", type = IdType.AUTO) private Integer sno; private String sname; private String sex; private Integer age; private Integer clas; }
(5)、创建mapper接口
package com.zhu.mapper; import com.zhu.entity.Student; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * <p> * Mapper 接口 * </p> * * @author xiaozhu * @since 2022-04-13 */ public interface StudentMapper extends BaseMapper<Student> { }
(6)、创建service接口及其实现类
package com.zhu.service; import com.zhu.entity.Student; import com.baomidou.mybatisplus.extension.service.IService; /** * <p> * 服务类 * </p> * * @author xiaozhu * @since 2022-04-13 */ public interface StudentService extends IService<Student> { }
package com.zhu.service.impl; import com.zhu.entity.Student; import com.zhu.mapper.StudentMapper; import com.zhu.service.StudentService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; /** * <p> * 服务实现类 * </p> * * @author xiaozhu * @since 2022-04-13 */ @Service public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService { }
(7)、创建controller
package com.zhu.controller; import com.zhu.entity.Student; import com.zhu.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * <p> * 前端控制器 * </p> * * @author xiaozhu * @since 2022-04-13 */ @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; //返回所有学生 @GetMapping("/allStudent") public List<Student> findAllStudent(){ return studentService.list(); } //根据学号查询学生 @GetMapping("/findBySno/{sno}") public Student findBySno(@PathVariable("sno") Integer sno){ return studentService.getById(sno); } //根据学号删除学生信息(此方法可以使用软件postman进行测试) @DeleteMapping("/deleteBySno/{sno}") public boolean deleteBySno(@PathVariable("sno") Integer sno){ return studentService.removeById(sno); } //增加一个学生信息(此方法可以使用软件postman进行测试),注意学号自增 @PostMapping("/add") public boolean add(@RequestBody Student student){ return studentService.save(student); } //根据学号修改学生信息(此方法可以使用软件postman进行测试),注意学号自增 @PutMapping("/update") public boolean update(@RequestBody Student student){ return studentService.updateById(student); } }
至此,我们完成了mybatisplus的快速的入门级别的增删改查
总结
到此这篇关于如何使用MybatisPlus快速进行增删改查的文章就介绍到这了,更多相关MybatisPlus增删改查内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://blog.csdn.net/qq_54650406/article/details/124160403
相关文章
MyBatisPlus-QueryWrapper多条件查询及修改方式
这篇文章主要介绍了MyBatisPlus-QueryWrapper多条件查询及修改方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2022-06-27- yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2使用中的一些基本的增删改查操作。 User::find()->all(); //返回所有用户数据; User::findOne($id); //返回 主键...2015-11-24
- 这篇文章主要介绍了MybatisPlus实现逻辑删除功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-25
解决mybatisplus插入报错argument type mismatch的问题
这篇文章主要介绍了解决mybatisplus插入报错argument type mismatch的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-25MybatisPlus实现分页查询和动态SQL查询的示例代码
本文主要介绍了MybatisPlus实现分页查询和动态SQL查询的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-30- 增删改查操作,其中增删改操作被称为数据操作语言 DML,相对来说简单一点。 查操作相对来说复杂一点,涉及到很多子句,所以这篇先讲增删改操作,以例子为主,后面再讲查操作...2020-07-11
- 这篇文章主要介绍了搭建MyBatis-Plus框架并进行数据库增删改查,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-12
Django开发RESTful API实现增删改查(入门级)
这篇文章主要介绍了Django开发RESTful API实现增删改查(入门级),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-10- 这篇文章主要介绍了Mybatis实现动态增删改查功能的示例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-02
- 这篇文章主要介绍了Mybatis基于xml配置实现单表的增删改查,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-12
一篇超详细的SpringBoot整合MybatisPlus的文章
这篇文章主要介绍了springboot整合Mybatis-plus的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-07-27- 本文章来为各位介绍一篇Laravel对数据库增删改查的例子了,这个是基于查询构建器来实现了,具体的如下所示。 获取查询构建器很简单,还是要依赖DB门面,我们使用DB门面的...2016-11-25
- 这篇文章主要介绍了Yii针对添加行的增删改查操作,涉及Yii结合jQuery针对页面元素的动态操作及数据库增删改查等操作相关实现技巧,需要的朋友可以参考下...2016-10-20
- 这篇文章主要为大家详细介绍了C#访问SQLServer增删改查代码实例,感兴趣的小伙伴们可以参考一下...2020-06-25
springboot+mybatis plus实现树形结构查询
实际开发过程中经常需要查询节点树,根据指定节点获取子节点列表,本文主要介绍了springboot+mybatis plus实现树形结构查询,感兴趣的可以了解一下...2021-07-22- 这篇文章主要介绍了微信小程序本地缓存数据增删改查实例详解的相关资料,需要的朋友可以参考下...2017-05-27
- 这篇文章主要给大家介绍了关于.NET连接数据库以及基本的增删改查操作教程的相关资料,对于刚入门的新手们来说是个很好的入门教程,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。...2021-09-22
- 这篇文章主要给大家介绍了关于Spring Boot如何实现定时任务的动态增删启停的相关资料,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-07-19
- 这篇文章主要介绍了Spring框架+jdbcTemplate实现增删改查功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
- 这篇文章主要介绍了Python中tkinter+MySQL实现增删改查,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-09