springboot 整合 mybatis-plus
简介
mybatis-plus
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。现最新版本为 3.3.0
添加依赖 pom.xml
- 引入 Spring Boot Starter 父工程:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</dependency>
- 引入 spring-boot-starter、spring-boot-starter-test、mybatis-plus-boot-starter、lombok、mysql 依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置
- application.yml 或 application.properties 配置文件中添加 mysql数据库的相关配置:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/8timer_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=CONVERT_TO_NULL
driver-class-name: com.mysql.cj.jdbc.Driver
- Spring Boot 启动类中添加 @MapperScan 注解:
/* @MapperScan
* 作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
* 添加位置:是在Springboot启动类上面添加,
*/
@MapperScan("cn.timer.api.dao") // 扫描的mapper
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
代码
创建实体类
People
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class People {
private Integer id;
private String name;
private Integer age;
private String hobby;
private String motto;
}
创建Mapper类
public interface PeopleMapper extends BaseMapper<People> {
}
调试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {
@Autowired
private PeopleMapper peopleMapper;
//查询
@Test
public void selectList() {
System.out.println(("selectList start"));
// peopleMapper.selectList(queryWrapper);
// queryWrapper 是 MP 提供的 实体对象封装操作类,为 null 则无条件查询
List<People> peopleList = peopleMapper.selectList(null);
Assert.assertEquals(6, userList.size());
for (People people : peopleList) {
System.out.println(people);
}
}
}
控制台
selectList start
People(id=1001, name=Cxk, age=18, hobby=打篮球, motto=鸡你太美1)
People(id=1002, name=Cxk, age=18, hobby=嘻哈, motto=鸡你太美2)
People(id=1003, name=Cxk, age=18, hobby=唱, motto=鸡你太美3)
People(id=1004, name=Cxk, age=18, hobby=跳, motto=鸡你太美4)
People(id=1005, name=Cxk, age=18, hobby=rap, motto=鸡你太美5)
People(id=1006, name=Cxk, age=18, hobby=鸡, motto=鸡你太美6)
CRUD
新增
People p1 = People.builder().name("Cxk").age(18).hobby("打篮球").motto("鸡你太美1").build();
peopleMapper.insert(p1);
删除
peopleMapper.deleteById(id)
查询
People p3 = peopleMapper.selectById(id)
修改
People p4 = People.builder().id(1).hobby("唱").motto("ccccc").build();
peopleMapper.updateById(p4);
更多API请看官网: Mybatis Plus
总结
- 整合简易
- 不需要写
SQL
和XML
就可以实现简单的CRUD
功能 - 快速开发项目
补充
感谢
感谢您的阅读,有收获?希望兄弟姐妹三叔六婶大姨大妈阿公阿婆来个三连击,给更多的同学看到这篇文章,感谢
你的每一次回眸都是永恒,每一次鼓励都是我前进的动力,每一次分享都是对我的认可。