首頁 > Java > java教程 > 主體

SpringBoot怎麼整合mybatis+mybatis-plus

WBOY
發布: 2023-05-10 19:55:12
轉載
1454 人瀏覽過

準備工作

1、準備如下一個資料表

CREATE TABLE `student` (
  `id` varchar(32) NOT NULL,
  `gender` varchar(32) DEFAULT NULL,
  `age` int(12) DEFAULT NULL,
  `nick_name` varchar(32) DEFAULT NULL,
  `name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
登入後複製

2、插入幾個測試資料

SpringBoot怎麼整合mybatis+mybatis-plus

整合步驟

工程的完整套件結構如圖所示

SpringBoot怎麼整合mybatis+mybatis-plus

1、導入maven依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <mysql-connector-java.version>8.0.11</mysql-connector-java.version>
        <commons-lang3.version>3.7</commons-lang3.version>
        <fastjson.version>1.2.47</fastjson.version>
        <mybatis-plus-boot-starter.version>3.3.0</mybatis-plus-boot-starter.version>
        <mybatis-plus-generator.version>3.3.0</mybatis-plus-generator.version>
        <druid.version>1.1.14</druid.version>
        <lombok.version>1.18.0</lombok.version>
        <dubbo-spring-boot-starter.version>2.0.0</dubbo-spring-boot-starter.version>
        <swagger.version>2.9.2</swagger.version>
        <swagger-bootstrap-ui.version>1.9.6</swagger-bootstrap-ui.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
 
        <!--阿里巴巴fastjosn依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
 
        <!--阿里巴巴数据库连接池依赖-->
        <!-- Druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
 
        <!-- MyBatis增强工具-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>${mybatis-plus-generator.version}</version>
        </dependency>
 
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
 
        <!--swagger-ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
 
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>${swagger-bootstrap-ui.version}</version>
        </dependency>
 
    </dependencies>
登入後複製

2、設定檔 application.yml

server:
  port: 8083
logging:
  config: classpath:logback-spring.xml  #日志
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://IP:3306/school?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: root
    druid:
      max-active: 100
      initial-size: 10
      max-wait: 60000
      min-idle: 5
  #设置单个文件最大上传大小
  servlet:
    multipart:
      max-file-size: 20MB
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  global-config:
    db-column-underline: true  #开启驼峰转换
    db-config:
      id-type: uuid
      field-strategy: not_null
    refresh: true
  configuration:
    map-underscore-to-camel-case: true
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句便于调试
登入後複製

3、為了方便後面調試接口,增加一個swagger的配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
/**
 * swagger文档,项目启动后,浏览器访问:http://localhost:8083/swagger-ui.html
 */
@Configuration
@EnableSwagger2
public class ApiSwagger2 {
 
    @Bean
    public Docket createRestBmbsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("users")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.congge.controller"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("后端相关API")
                .version("1.0")
                .build();
    }
 
}
登入後複製

4、實體類

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
 
@Data
public class Student {
 
    @TableField("id")
    private String id;
 
    @TableField("name")
    private String name;
 
    @TableField("gender")
    private String gender;
 
    @TableField("age")
    private int age;
 
    @TableField("nick_name")
    private String nickName;
 
}
登入後複製

5、dao接口,裡面添加一個查詢所有數據的方法

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.congge.entity.Student;
import org.apache.ibatis.annotations.Mapper;
 
import java.util.List;
 
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
 
    List<Student> queryAll();
 
}
登入後複製

6、mybatis層,寫sql的檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.congge.dao.StudentMapper">
 
    <select id="queryAll" resultType="com.congge.entity.Student">
		select * from student
	</select>
 
</mapper>
登入後複製

7、業務實作類別

在本次的業務實作中,同時可以使用mybatis的方式以及mybatis- plus的方式進行,具體使用的時候結合自身的需求進行選擇;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.congge.dao.StudentMapper;
import com.congge.entity.Student;
import com.congge.service.StudentService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
 
@Service
public class StudentServiceImpl implements StudentService {
 
    @Resource
    private StudentMapper studentMapper;
 
    @Override
    public List<Student> queryAllStudent() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper();
        List<Student> students = studentMapper.selectList(queryWrapper);
        return students;
        //return studentMapper.queryAll();
    }
 
    @Override
    public List<Student> getByName(String name) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper();
        queryWrapper.like("name",name);
        return studentMapper.selectList(queryWrapper);
    }
 
    public Student getById(String id) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper();
        queryWrapper.like("id",id);
        return studentMapper.selectOne(queryWrapper);
    }
}
登入後複製

8、新增一個測試介面

@RestController
public class StudentController {
 
    @Autowired
    private StudentService studentService;
 
    @GetMapping("/getAll")
    public List<Student> getAll(){
        return studentService.queryAllStudent();
    }
 
    @GetMapping("/getByName")
    public List<Student> getByName(@RequestParam String name){
        return studentService.getByName(name);
    }
 
    @GetMapping("/getById")
    public Student getById(@RequestParam String id){
        return studentService.getById(id);
    }
}
登入後複製

9、啟動類別

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class App {
 
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
 
}
登入後複製

接下來,將工程運行起來做一下測試吧

10、啟動之後,開啟swagger介面

SpringBoot怎麼整合mybatis+mybatis-plus

SpringBoot怎麼整合mybatis+mybatis-plus

不妨隨機測試兩個介面吧,測試下取得所有學生的資料介面

SpringBoot怎麼整合mybatis+mybatis-plus

#

以上是SpringBoot怎麼整合mybatis+mybatis-plus的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板