总述:MyBatisPlus是 将Mybatis + 通用Mapper + PageHelper升级成 MyBatis Plus
技术升级示意图
(1)pom.xml文件,导入相关依赖jar包
org.springframework.boot
spring-boot-starter-parent
2.3.5.RELEASE
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
com.baomidou
mybatis-plus-boot-starter
3.4.0
(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 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; @Mapper public interface CustomerMapper extends BaseMapper{ }
(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 list = customerMapper.selectList(null);
list.forEach(System.out::println);
}
}



