springboot整合mybatis plus

第一步,首先新建一个springboot项目,需要到的插件如下图
springboot整合mybatis plus_第1张图片
第二步导入mybatis plus 和 Druid 相关依赖包

  <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>

第三步,编写配置文件application.yml

server:
  port: 10086
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
    username: root
    password: 4321
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  #全局配置
  global-config:
  #数据库配置
    db-config:
    #主键类型
      id-type: auto
      table-underline: true

  configuration:
  #将下划线转化为驼峰命名法
    map-underscore-to-camel-case: true
    #空值不做映射
    call-setters-on-nulls: true
    #打印sql
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

第四步,项目目录如下:
springboot整合mybatis plus_第2张图片
首先编写pojo下面的实体类Test

package com.sprongboot.boot_mp.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
@Data
@TableName("test_test")
public class Test implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
}

编写mapper层

package com.sprongboot.boot_mp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sprongboot.boot_mp.pojo.Test;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TestMapper  extends BaseMapper<Test> {
}

编写service层

package com.sprongboot.boot_mp.server;

import com.baomidou.mybatisplus.extension.service.IService;
import com.sprongboot.boot_mp.pojo.Test;

public interface TestService extends IService<Test> {

}

编写serviceimpl层

package com.sprongboot.boot_mp.ImService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprongboot.boot_mp.mapper.TestMapper;
import com.sprongboot.boot_mp.pojo.Test;
import com.sprongboot.boot_mp.server.TestService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ImTestService extends ServiceImpl<TestMapper,Test>  implements TestService {
   
}

编写config配置类,实现分页查询

package com.sprongboot.boot_mp.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.aop.interceptor.PerformanceMonitorInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisConfig {
    //配置分页插件
    @Bean
    public  PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
    //配置开发环境使用可以监测sql执行效率的插件
    @Bean
    public PerformanceMonitorInterceptor performanceMonitorInterceptor(){
        return new PerformanceMonitorInterceptor();
    }
}

编写controller层

package com.sprongboot.boot_mp.controller;

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.sprongboot.boot_mp.pojo.Test;
import com.sprongboot.boot_mp.server.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private TestService testService;
    @GetMapping("/selOne/{id}")
    public Test getOne(@PathVariable("id") Integer id){
        return testService.getById(id);
    }
    @GetMapping("/selAll")
    public List<Test> selAll(){
        QueryWrapper<Test> queryWrapper=new QueryWrapper();
        queryWrapper.lambda().eq(Test::getAge,16);
        return testService.list(queryWrapper);
    }
    @GetMapping("/selpage")
    public IPage<Test> selByPage(){
        return testService.page(new Page<Test>(2,1));
    }
    @PostMapping ("/save")
    public Boolean insetOne(){
        Test test=new Test();
        test.setAge(4);
        test.setName("校长");
        test.setSex("男");
        return testService.saveOrUpdate(test);
    }
    @DeleteMapping("/del/{id}")
    public Boolean delOne(@PathVariable Integer id){
        return testService.removeById(id);
    }
    @PutMapping("put")
    public Boolean putOne(){
        Test test=new Test();
        test.setId(1);
        test.setSex("无");
        boolean b = testService.updateById(test);
        return b;
    }
    @GetMapping("get")
  public  List<Test> getlist(){
     Map<String ,Object> map=new HashMap<>();
     map.put("age",16);
     return testService.listByMap(map);
  }
}

你可能感兴趣的:(springboot整合mybatis plus)