zoukankan      html  css  js  c++  java
  • SpringBoot

    前言

    Mybatis-PlusMybatis的增强,Mybatis-PlusMybatis的基础上借鉴了很多JPA的做法,记录下SpringBoot下的整合方法。


    环境

    SpringBoot2.53 + Mybatis-Plus3.3.0


    具体实现

    项目结构

    在这里插入图片描述


    项目配置

    • pom.xml
    <!-- web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    
    <!-- mybatis-plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.0</version>
    </dependency>
    
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
    
    • application.yml
    spring:
      application:
        name: mybatis-learn
      # 文件编码 UTF8
      mandatory-file-encoding: UTF-8
      # 数据源配置,请修改为你项目的实际配置
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/cms?useSSL=false&serverTimezone=UTC&characterEncoding=UTF8
        username: root
        password: sunday
    
    # mybatis-plus配置
    mybatis-plus:
      configuration:
        # 开启下划线转驼峰
        map-underscore-to-camel-case: true
      # mapper路径位置
      mapper-locations: classpath:mapper/*.xml
    
    server:
      port: 8084
    
    • Table
    CREATE TABLE `product` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
      `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
    
    

    实现代码

    • 启动类
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan(basePackages = "com.coisini.mybatisplus.mapper") // mapper扫描位置
    public class MybatisplusApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MybatisplusApplication.class, args);
        }
    
    }
    
    • ProductController.java
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import java.util.List;
    
    @RequestMapping("/product")
    @RestController
    public class ProductController {
    
        @Autowired
        private ProductService productService;
    
        /**
         * 查询Products
         * @return
         */
        @GetMapping("/select")
        public List<Product> selectProducts() {
            return productService.getProducts();
        }
    
    }
    
    • ProductService.java
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    
    @Service
    public class ProductService {
    
        @Autowired
        private ProductMapper productMapper;
    
        /**
         * 查询products
         * @return
         */
        public List<Product> getProducts() {
            return productMapper.selectList(null);
        }
    
    }
    
    
    • ProductMapper.java
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface ProductMapper extends BaseMapper<Product> {
    	// 继承 BaseMapper,与JPA类似
    }
    
    • Product.java
    import com.baomidou.mybatisplus.annotation.TableName;
    import lombok.Getter;
    import lombok.Setter;
    import java.util.Date;
    
    @Getter
    @Setter
    @TableName("product")
    public class Product {
    
        private Integer id;
    
        private String title;
    
        private Date createTime;
    
    }
    

    测试

    在这里插入图片描述


    - End -
    梦想是咸鱼
    关注一下吧
    以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
    作者:Maggieq8324
    本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。
  • 相关阅读:
    固态硬盘和机械硬盘的比较和SQLSERVER在两种硬盘上的性能差异
    带您理解SQLSERVER是如何执行一个查询的
    SQL Server 2000中的并行处理和执行计划中的位图运算符
    SQL2005解密已经被加密的存储过程
    使用SQLServer 2008的CDC功能实现数据变更捕获
    like语句百分号前置会使用到索引吗?
    再说一下表分区
    SQLSERVER中的元数据锁
    验证非法电话号码
    OpenCV 2.2版本号以上显示图片到 MFC 的 Picture Control 控件中
  • 原文地址:https://www.cnblogs.com/maggieq8324/p/15237199.html
Copyright © 2011-2022 走看看