总述:MyBatisPlus是 将Mybatis + 通用Mapper + PageHelper
升级成 MyBatis Plus
技术升级示意图
(1)pom.xml文件,导入相关依赖jar包
<!--确定spring boot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<!-- web 开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--支持lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
(2)application.yml文件,配置对应的数据库
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud_db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 1234
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出日志
(3)编写启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestMyBatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(TestMyBatisPlusApplication.class,args);
}
}
(4)编写对应的javaBean类以及添加对应的注解
@Data//setget构造
@NoArgsConstructor//无参构造
@AllArgsConstructor//全参构造
@TableName("tmp_customer") //对应数据库的表名
public class Customer {
@TableId(type = IdType.AUTO)//明确主键 并且主键是自动增长的
private Integer cid;
private String cname;
private String password;
private String telephone;
private String money;
private Integer version;
@TableField(exist = false)//声明不是数据库中的字段名
private List<Integer> ids;
}
(5)编写dao
package com.czxy.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.mp.domain.Customer;
import org.apache.ibatis.annotations.Mapper;
/**
* 注意:
* 如果是MyBatis需要继承Mapper<Customer>
* 此处使用MyBatisPlus则要继承BaseMapper<Customer>
*/
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {
}
(6)编写测试类
import com.czxy.mp.MybatisPlusApplication;
import com.czxy.mp.domain.Customer;
import com.czxy.mp.mapper.CustomerMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusApplication.class)
public class TestDemo01 {
@Resource
private CustomerMapper customerMapper;
@Test
public void testFindAll() {
//查询所有
List<Customer> list = customerMapper.selectList(null);
list.forEach(System.out::println);
}
}