首页 > 其他分享 >SpringBoot 整合mybatis-plus

SpringBoot 整合mybatis-plus

时间:2022-11-10 16:36:05浏览次数:37  
标签:mapper SpringBoot boot plus org mybatis import com public

SpringBoot 整合mybatis-plus

1、导入Maven依赖

<dependencies>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis-plus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--数据库驱动依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--druid连接池依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.9</version>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

2、application.yml配置

spring:
  datasource:
    druid:
      url: jdbc:mysql://127.0.0.1:3306/learn?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456

mybatis-plus:
  mapper-locations: classpath*:mapper/**/*Mapper.xml   # 存放sql语句的xml文件目录
  type-aliases-package: com.hu.entity # 实体类包路径
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl   # 放开则开启SQL打印
    map-underscore-to-camel-case: true # 开启驼峰法则
  global-config:
    db-config:
      table-prefix: tbl_ # 数据库表前缀

3、项目结构

image-20221110161814169

4、entity

import lombok.Data;

import java.io.Serializable;

@Data
public class Book implements Serializable {
   private Integer id;
   private String type;
   private String name;
   private String description;
}

5、mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hu.entity.Book;

import java.util.List;

public interface BookMapper extends BaseMapper<Book> {
     List<Book> getBookAll();//查询全部
}

6、resource下的mapper

<?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.hu.mapper.BookMapper">
    <select id="getBookAll" resultType="Book">
        select * from tbl_Book;
    </select>
</mapper>

7、启动类

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.hu.mapper") // 扫描mapper接口包,不用在接口上加@Mapper注解
@SpringBootApplication
public class MyBatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyBatisApplication.class, args);
    }
}

8、service

import com.baomidou.mybatisplus.extension.service.IService;
import com.hu.entity.Book;

import java.util.List;

public interface BookService extends IService<Book> {
    List<Book> getBookAll();
}

9、impl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.mapper.BookMapper;
import com.hu.entity.Book;
import com.hu.service.BookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public List<Book> getBookAll() {
        return bookMapper.getBookAll();
    }
}

10、controller

@RestController
@ResponseBody
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bookService;


    @GetMapping
    public List<Book> getAll(){
        return bookService.getBookAll();
    }
}

时间:2022-11-10 下午

标签:mapper,SpringBoot,boot,plus,org,mybatis,import,com,public
From: https://www.cnblogs.com/huxiaoan1/p/16877527.html

相关文章

  • vue3:elementPlus的dailog组件title标题位置设置
     按照官网打开弹出窗想要的是这种效果但自己出现的是下面这种,title跑中间了,body区唯一一个控件,也跑到下面了   设置el-dialog__header高度后,高度的确发生了变......
  • SpringBoot启用Https(二十六)
    死亡的日子终究会到来,在那一天到来之前,别忘记,来我坟前再看一眼上一章简单介绍了SpringBoot自定义日志Starter(二十五),如果没有看过,​​请观看上一章​​关于这一章节的内......
  • SpringBoot整合Redis(十九)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了多数据源配置MyBatisPlus(十八),如果没有看过,​​请观看上一章​​一.Redis的介绍和安装......
  • SpringBoot整合Velocity(十二)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合FreeMarker(十一),如果没有看过,​​请观看上一章​​学习整合之前,可以看一......
  • SpringBoot通过Cors解决跨域问题(三十一)
    上一章简单介绍了SpringBoot全局异常处理(三十),如果没有看过,​​请观看上一章​​本章节参考江南一点雨大神的文章:​​SpringBoot2系列教程(十四)CORS解决跨域问题......
  • SpringBoot自定义Starter(二十四)
    即使有一天,我放弃了自己的身体,也请你,不要放弃我,我亲爱的灵魂.上一章简单介绍了Spring_Session解决Session共享的问题(二十三),如果没有看过,​​请观看上一章​​一.自定义......
  • SpringBoot上传和下载文件(二十七)
    当死亡来临,每一个人都不会接受自己的命运,他们会反抗.上一章简单介绍了SpringBoot启用Https(二十六),如果没有看过,​​请观看上一章​​文件上传和下载,是常用的功能可以看老......
  • SpringBoot全局异常处理(三十)
    生活打了我们一巴掌,我们,一定要想办法再打回来上一章简单介绍了SpringBoot上传文件到远程服务器(二十九),如果没有看过,​​请观看上一章​​一.为什么要实现异常信息自定义......
  • SpringBoot自定义日志Starter(二十五)
    即使有一天,我放弃了自己的身体,也请你,不要放弃我,我亲爱的灵魂.上一章简单介绍了SpringBoot自定义Starter(二十四),如果没有看过,​​请观看上一章​​一.AOP实现日志功能......
  • SpringBoot整合Ehcache缓存(二十二)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合Cache缓存技术(二十一),如果没有看过,​​请观看上一章​​一.Ehcache关于......