Springboot整合mybatis-plus
1:引入mp的包
<!--mybatis关键的两个包--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency>
一起使用的数据库包,根据需求添加,我这里就用下面这几个即可
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> <!--mybatis关键的两个包--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--web启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.5.RELEASE</version> </dependency>
2:使用代码生成器,先把已经建立好的数据库表生成代码(代码生成器自行百度,可以在github上,随便拉个生成代码的项目就可以)
代码生成后,放入项目,具体如下
如果没有生成的条件,也可以手动写,具体操作如下:
2.1:首先是手写entity,字段名和数据库保持一致,然后增加注解绑定数据库表:
@TableName("user")
2.2:手写service接口,然后继承mp的service
public interface TestUserService extends IService<TestUserEntity> { }
2.3:手写实现类,继承mp的实现类并实现上面的接口
@Service public class
TestUserServiceImpl extends ServiceImpl<TestUserDao,TestUserEntity> implements TestUserService
{ }
2.4:手写do接口,然后继承mp的接口
public interface TestUserDaoextends BaseMapper<TestUserEntity> { }
2.5:然后controller层 就自己,注入service,直接使用mp的方法,就返回数据了
3:启动类上添加扫描注解
@SpringBootApplication @MapperScan("com.text.mp.demo.dao") //扫描dao public class TestMPApplication { public static void main(String[] args) { SpringApplication.run(TestMPApplication.class, args); } }
4:在controller中找个方法进行测试
package com.text.mp.demo.controller; import com.text.mp.demo.bean.PageVo; import com.text.mp.demo.entity.TestUserEntity; import com.text.mp.demo.service.TestUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.List; /** * * * @author LiQingBi * @email 1130274947@qq.com * @date 2022-05-27 13:24:48 */ @RestController @RequestMapping("demo/testuser") public class TestUserController { @Autowired private TestUserService testUserService; /** * 列表 */ @GetMapping("/list") public Object list() { List<TestUserEntity> list = testUserService.list(); return list; } /** * 信息 */ @GetMapping("/info/{id}") public TestUserEntity info(@PathVariable("id") Integer id){ TestUserEntity testUser = testUserService.getById(id); return testUser; } /** * 保存 */ @PostMapping("/save") public Object save(@RequestBody TestUserEntity testUser){ return testUserService.save(testUser); } /** * 修改 */ @PostMapping("/update") public Object update(@RequestBody TestUserEntity testUser){ return testUserService.updateById(testUser); } /** * 删除 */ @PostMapping("/delete") public Object delete(@RequestBody Integer[] ids){ return testUserService.removeByIds(Arrays.asList(ids)); } }
测试成功