• SpringBoot整合Mybatis(使用c3p0数据源)


    在Idea中创建SpringBoot项目,勾选需要的依赖

    可以在创建时勾选,也可以在创建项目时手动添加依赖
    在这里插入图片描述
    手动添加JSON数据处理的包

    
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.10.0version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-coreartifactId>
                <version>2.10.0version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-annotationsartifactId>
                <version>2.10.0version>
            dependency>
            <dependency>
                <groupId>jakarta.annotationgroupId>
                <artifactId>jakarta.annotation-apiartifactId>
                <version>1.3.5version>
            dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    加入c3p0的依赖

     <dependency>
                <groupId>com.mchangegroupId>
                <artifactId>c3p0artifactId>
                <version>0.9.5.2version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在配置文件中配置数据源等配置

    这里使用yml格式

    server:
      port: 端口号
      servlet:
        context-path: /自定义地址名
    spring:
      datasource:
        type: com.mchange.v2.c3p0.ComboPooledDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
        username: 用户名
        password: "密码"
        maxPoolSize: 20
        minPoolSize: 2
        initialPoolSize: 5
    mybatis:
      type-aliases-package: 实体类地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    根据MySQL表创建实体类

    在这里插入图片描述

    package pringbootmybatis.demo.entity;
    
    import lombok.Data;
    
    import java.util.Date;
    
    /**
     * @author Una
     * @date 2022/8/22 13:27
     * @description:
     */
    
    @Data
    @ToString
    public class Book {
        private Integer bookId;
        private String bookName;
        private String bookAuthor;
        private Date bookDate;
        private Integer bookPrice;
    
        public Book() {
        }
    
        public Book(Integer bookId, String bookName, String bookAuthor, Date bookDate, Integer bookPrice) {
            this.bookId = bookId;
            this.bookName = bookName;
            this.bookAuthor = bookAuthor;
            this.bookDate = bookDate;
            this.bookPrice = bookPrice;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    创建对应方法的接口,在接口中实现结果集的映射

    在BookMapper中,创建findAll()方法对应"select * from book";因为实体类属性名与表中列名有区别,则使用**@Results进行映射;使用@Mapper**标注整个类,将其标注为映射文件

    package pringbootmybatis.demo.mapper;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Result;
    import org.apache.ibatis.annotations.Results;
    import org.apache.ibatis.annotations.Select;
    import pringbootmybatis.demo.entity.Book;
    
    import java.util.List;
    
    /**
     * @author Una
     * @date 2022/8/22 13:30
     * @description:
     */
    
    @Mapper
    public interface BookMapper {
        @Select("select * from book")
        @Results({
                @Result(id = true,property = "bookId",column = "book_id"),
                @Result(property = "bookName",column = "book_name"),
                @Result(property = "bookAuthor",column = "book_author"),
                @Result(property = "bookDate",column = "book_date"),
                @Result(property = "bookPrice", column = "book_price")
        })
        public List<Book> findAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在服务层创建接口

    package pringbootmybatis.demo.service;
    
    import pringbootmybatis.demo.entity.Book;
    
    import java.util.List;
    
    /**
     * @author Una
     * @date 2022/8/22 13:47
     * @description:
     */
    
    public interface BookService {
        public List<Book> getBooks();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    实现接口

    使用**@Service**标注类,将其标注为服务层
    使用 @Autowired自动注入
    重写getBooks(),接收findAll()方法

    package pringbootmybatis.demo.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import pringbootmybatis.demo.entity.Book;
    import pringbootmybatis.demo.mapper.BookMapper;
    import pringbootmybatis.demo.service.BookService;
    
    import java.util.List;
    
    /**
     * @author Una
     * @date 2022/8/22 13:40
     * @description:
     */
    
    @Service
    public class BookServiceImpl implements BookService {
        @Autowired
        private BookMapper bookMapper;
    
        @Override
        public List<Book> getBooks() {
            return bookMapper.findAll();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    编写Controller控制器

    使用@RestController将类标注为Controller
    使用 @Autowired自动注入
    使用@GetMapping

    package pringbootmybatis.demo.controller;
    
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import pringbootmybatis.demo.entity.Book;
    import pringbootmybatis.demo.service.BookService;
    
    import java.util.List;
    
    /**
     * @author Una
     * @date 2022/8/22 13:49
     * @description:
     */
    
    
    @RestController
    public class BookController {
        @Autowired
        private BookService bookService;
        @GetMapping("/all")
        public String books() throws JsonProcessingException {
            List<Book> list=bookService.getBooks();
            ObjectMapper objectMapper=new ObjectMapper();
            String books=objectMapper.writeValueAsString(list);
            return books;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    测试

    在这里插入图片描述

  • 相关阅读:
    WEB自动化_文件上传(input 标签和非input 标签)
    Kali生成windows木马程序
    游戏服务器该如何选择
    RocketMQ为什么这么快?我从源码中扒出了10大原因!
    【SpringMVC】执行流程
    基于SpringBoot编写starter,自己写个类库引入使用
    使用Python调用Linux下的动态链接库
    Qt解决中文乱码QTextCodec
    APP自动化_操作微信小程序实现自动化_多终端并行
    WIFI版本云音响设置教程腾讯云平台版本
  • 原文地址:https://blog.csdn.net/m0_50744075/article/details/126463331