步骤:
1.坐标
<dependency>
<groupId>com.baimidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactld>
<version>3.4.3</version>
</dependency>
注意:mp坐标添加后,mybatis坐标移除
2.编写注解配置实体类与关系表映射关系(truncate清楚表以及主键)
@TableName(value="关系表名称")=======================》修在类
@TableField(value="关联字段名称")=====================》修饰在属性
exist="忽略字段"
@TableId(type="指定主键生成策略,默认雪花算法”)==========》修饰在属性
AUTO(0),
NONE(1),
INPUT(2),
ASSIGN_ID(3),
ASSIGN_UUID(4);
3.使用
BaseMapper=======================================>公共的数据访问层
IService/ServiceImp=================================>公共的业务层
4.编写配置文件.YML
以下为具体实例
package com.ly.springboot_mybatis_01.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ly.springboot_mybatis_01.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
@Select("select * from student")
public List<Student> findall();
}
package com.ly.springboot_mybatis_01.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
//注入mp拦截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//实例化拦截器
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
package com.ly.springboot_mybatis_01.pojo;
import com.baomidou.mybatisplus.annotation.*;
import org.springframework.web.multipart.MultipartFile;
//mybatisplus的注解可以省略,也已写上,但特殊的属性必须要写
@TableName(value = "student")
public class Student {
@TableId(value = "stu_id",type = IdType.AUTO)
private int stuId;
@TableField(value = "stu_name")
private String stuName;
@TableField(value = "nick_name")
private String nickName;
@TableField(value = "stu_age")
private int stuAge;
//mybatis逻辑状态(0 表示未删,1 表示已删)
@TableLogic(value = "0",delval = "1")
private int isDelete;
//数据库不存在该字段 用exist=false
@TableField(exist = false)
private MultipartFile file;
public Student(String stuName, String nickName, int stuAge) {
this.stuName = stuName;
this.nickName = nickName;
this.stuAge = stuAge;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", nickName='" + nickName + '\'' +
", stuAge=" + stuAge +
'}';
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public Student() {
}
public Student(int stuId, String stuName, String nickName, int stuAge) {
this.stuId = stuId;
this.stuName = stuName;
this.nickName = nickName;
this.stuAge = stuAge;
}
}
package com.ly.springboot_mybatis_01;
import com.ly.springboot_mybatis_01.mapper.StudentMapper;
import com.ly.springboot_mybatis_01.pojo.Student;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.List;
@SpringBootApplication
//@MapperScan(basePackages = "com.ly.springboot_mybatis_01.mapper")
public class SpringbootMybatis01Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatis01Application.class, args);
}
}
测试二
package com.ly.springboot_mybatis_01;
import com.ly.springboot_mybatis_01.mapper.StudentMapper;
import com.ly.springboot_mybatis_01.pojo.Student;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatis01ApplicationTests {
@Autowired(required = false)
StudentMapper studentMapper;
@Test
void contextLoads() {
List<Student> findall = studentMapper.findall();
for (Student student : findall) {
System.out.println(student);
}
}
}
测试三
package com.ly.springboot_mybatis_01;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.springboot_mybatis_01.mapper.StudentMapper;
import com.ly.springboot_mybatis_01.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.List;
/**
* @author 李妍
* @Version 1.0
* @since 2024/9/20
*/
@SpringBootTest
public class Test1 {
@Autowired(required = false)
StudentMapper mapper;
@Test
public void show1(){
Student student=new Student("依依","不晚",18);
int i = mapper.insert(student);
System.out.println("主键回填:"+student.getStuId());
System.out.println(i);
}
//修改ID
@Test
public void show2(){
Student student=new Student();
student.setStuId(5);
student.setStuName("小郭");
student.setNickName("玊尔");
student.setStuAge(18);
int i = mapper.updateById(student);
System.out.println(i);
}
//修改name
@Test
public void show3(){
//1.修改数据
Student student=new Student();
student.setNickName("晚风");
student.setStuAge(20);
//2.创建条件
QueryWrapper<Student> wrapper=new QueryWrapper<Student>();
wrapper.eq("stu_name","依依");
mapper.update(student,wrapper);
}
//查询id
@Test
public void show4(){
Student student=mapper.selectById(5);
System.out.println(student);
}
//查询IDS
@Test
public void show5() {
List<Student> students = mapper.selectBatchIds(Arrays.asList(1, 2, 5));
for (Student student : students) {
System.out.println(student);
}
}
//聚合函数查询(null 表示全查)
@Test
public void show6(){
QueryWrapper<Student> wrapper=new QueryWrapper<Student>();
wrapper.eq("stu_age",18);
Integer integer = mapper.selectCount(wrapper);
System.out.println("年龄为18的个数:"+integer+"个");
}
//全查
@Test
public void show7(){
QueryWrapper<Student> wrapper=new QueryWrapper<Student>();
List<Student> students = mapper.selectList(null);
for (Student student : students) {
System.out.println(student);
}
}
/**
* mp分页使用
* 注意:
* 1.page.setCurrent(2);当前页码从1开始
* 2.分页需要配置插件
* */
//分页
@Test
public void show8(){
//定义分页规则
Page<Student> page=new Page<Student>();
page.setSize(2);//每页记录数
page.setCurrent(3);//当前页码
//查询条件
QueryWrapper<Student> queryWrapper=new QueryWrapper<Student>();
queryWrapper.eq("stu_age",18);
IPage<Student> ipage = mapper.selectPage(page, null);
List<Student> list = ipage.getRecords();
System.out.println("总记录数:"+ipage.getTotal());
System.out.println("总计页数:"+ipage.getPages());
for (Student student : list) {
System.out.println(student);
}
}
//查询delete
//物理查询,逻辑查询
@Test
public void show9(){
mapper.deleteById(6);
}
}
测试四
package com.ly.springboot_mybatis_01;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ly.springboot_mybatis_01.mapper.StudentMapper;
import com.ly.springboot_mybatis_01.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class Test02 {
@Autowired(required = false)
StudentMapper studentMapper;
@Test
public void show1(){
LambdaQueryWrapper<Student> studentLambdaQueryWrapper=new LambdaQueryWrapper<>();
studentLambdaQueryWrapper.ge(Student::getStuAge,18);
List<Student> students = studentMapper.selectList(studentLambdaQueryWrapper);
for (Student student : students) {
System.out.println(student);
}
}
}
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/kj03?serverTimezone=GMT
username:
password:
mybatis:
configuration:
map-underscore-to-camel-case: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true