Springboot 集成整合mybatis plus 操作全集

MybatisPlus 学习笔记

学习了MybatisPlus 之后,可以让我们告别繁琐又没有技术含量的增删改查,其实我们工作中大部分接口几乎都是此类工作,所以用它就完了,效率杠杠的。

SpringBoot 集成

  • MAVEN
   
       <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

      <!-- 数据库连接池 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

<!-- 测试类-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

<!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

  • application.yml
# mysql连接信息
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/testMyBatisPlus?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
    username: root
    password: root
  thymeleaf:
    cache: false  #设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
#修改端口号默认是8080
server:
  port: 8888
#mybatis-plus相关配置
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml #扫描mapper下的所有xml文件
  type-aliases-package: com.zwl.entity   #扫描实体类包/配置别名
  • 创建实体类
//数据库表自己建
@Data
public class Student {
    
    

    private Integer id;
    private String name;
    private Integer age;
    private String desc;

}

  • mapper继承
// 集成BaseMapper接口 类型为实体类类型
public interface StudentMapper extends BaseMapper<Student> {
    
    
}

Mybatis plus 基本用法

  • service封装一层mapper ,使用起来更加便捷
//iservice接口
public interface IStudentService extends IService<Student> {
    
    
}
//实现类
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    
    

}

@RestController
public class StudentController {
    
    
   //使用  直接注入即可
    @Autowired
    private IStudentService studentService;

}



  • 此时的service 已经继承了mybatis plus 所有封装函数。如图,顺带可以看下目录结构

在这里插入图片描述

内置函数操作示例

GET

//  查询内置函数有点多 不一一说了 会用几个关键的就行 
default T getOne(Wrapper<T> queryWrapper) 
default int count()   // 总条数
default int count(Wrapper<T> queryWrapper)  //条件筛选后的总条数
default List<T> list() // 全部数据
default List<T> list(Wrapper<T> queryWrapper) //条件筛选后的全部数据
default <E extends IPage<T>> E page(E page) // 分页查询
default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper)  // 分页条件查询
count
    @GetMapping("getCount")
    public void getCount(){
    
    
        int count = studentService.count();
        System.out.println(count);
    }

countByTerm
    @GetMapping("getCountByTerm")
    public void getCountByTerm(){
    
    
    // 查询 年龄大于8岁 and 姓名为小明的总数
        QueryWrapper qw = new QueryWrapper();
        qw.gt("age","8");
        qw.eq("name","小明");
        int count = studentService.count(qw);
        System.out.println(count);
    }
 //注: wrapper内置函数说明:eq相等 ne不相等 gt大于 lt小于 ge大于等于 le 小于等于

getOne
    @GetMapping("getOne")
    public void getOne(){
    
    
        QueryWrapper qw = new QueryWrapper();
        qw.eq("name","小八");
     // getOne  只返回一个  多条报错 TooManyResultsException
        Student student = studentService.getOne(qw);
        System.out.println(student);
    }
list
    @GetMapping("get")
    public void get(){
    
    
        List<Student> list = studentService.list();
        for (Student student : list) {
    
    
            System.out.println(student.toString());
        }
    }

listByWrapper
    @GetMapping("listByWrapper")
    public void listByWrapper(){
    
    
        QueryWrapper<Student> qw = new QueryWrapper();
        qw.eq("age","10");
        List<Student> list = studentService.list(qw);
        for (Student student : list) {
    
    
            System.out.println(student);
        }
    }
page
//配置类加上
@Configuration
public class MybatisConfig {
    
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

    @GetMapping("get")
    public void get(){
    
    
    // 页数 1  每页10
        Page<Student> page = new Page<>(1,10);
        Page<Student> page1 = studentService.page(page);
        List<Student> records = page1.getRecords();
        for (Student record : records) {
    
    
            System.out.println(record);
        }
    }

//page条件查询一样 new个QueryWrapper对象 直接用

INSERT

//直接save函数即可  service类已经指定了实体类类型

    @GetMapping("insert")
    public void insert(){
    
    
        
        Student student = new Student();
        student.setAge(10);
        student.setName("小王");
        student.setDesc("小王是一名学生");
        studentService.save(student);
       
    }


UPDATE

updateById
//通过Id 修改数据

    @GetMapping("updateById")
    public void updateById(){
    
    
        Student student = new Student();
        student.setId(1);
        student.setDesc("小明是一个坏学生");
        studentService.updateById(student);
    }

updateByTerm
// 通过指定条件更改 

//例如将全部name为小明的数据age修改为10
    @GetMapping("updateByTerm")
    public void updateByTerm(){
    
    
 // wrapper函数 说明:eq相等 ne不相等 gt大于 lt小于 ge大于等于 le 小于等于
        UpdateWrapper<Student> uw = new UpdateWrapper();
        uw.eq("name","小明");
        Student student = new Student();
        student.setAge(10);
        studentService.update(student,uw);
    }

updateBatch
// 批量修改
    @GetMapping("updateBatch")
    public void updateBatch(){
    
    
        List<Student> studentList = new ArrayList<>();
        Student student = new Student();
        student.setDesc("小王是一名好学生");
        student.setId(2);
        studentList.add(student);
        Student student1=  new Student();
        student1.setDesc("小明是体育老师");
        student1.setId(1);
        studentList.add(student1);
        studentService.updateBatchById(studentList);
    }

DELETE

// mybatis plus 提供四个 remove 函数
    default boolean removeById(Serializable id) 
    default boolean removeByMap(Map<String, Object> columnMap) 
    default boolean remove(Wrapper<T> queryWrapper) 
    default boolean removeByIds(Collection<? extends Serializable> idList) 

removeById
    @GetMapping("removeById")
    public void removeById(){
    
    
        studentService.removeById(1);
    }
removeByIds

    @GetMapping("removeByIds")
    public void removeByIds(){
    
    
        List<Integer> tarList = new ArrayList<>();
        tarList.add(1);
        tarList.add(2);
        tarList.add(3);
        studentService.removeByIds(tarList);
    }

removeByMap

    @GetMapping("removeByMap")
    public void removeByMap(){
    
    

// 属性与值的映射
        Map<String,Object> tarMap = new HashMap<>();
        tarMap.put("name","小明");

        studentService.removeByMap(tarMap);
    }
    
remove
// 指定条件进行删除

// 示例 删除姓名叫小明 且年龄大于10岁的数据。 
    @GetMapping("removeByWrapper")
    public void removeByWrapper(){
    
    
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name","小明");
        wrapper.gt("age","10");
        studentService.remove(wrapper);
    }



项目源码

链接:https://pan.baidu.com/s/1IG8ouS4QF53pvZNsW0OuYw
提取码:6kwa

猜你喜欢

转载自blog.csdn.net/pgcdnameming/article/details/128282612