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
功能 - 快速開發項目
補充
感謝
感謝您的閱讀,有收穫?希望兄弟姐妹三叔六嬸大姨大媽阿公阿婆來個三連擊,給更多的同學看到這篇文章,感謝
你的每一次回眸都是永恆,每一次鼓勵都是我前進的動力,每一次分享都是對我的認可。