easy-mybatis easy-mybatis
首页
  • 框架初衷与诞生
  • 介绍
  • 快速上手
  • 配置说明
  • 统一约定
  • MapperRepository
  • JsonObject
  • JsonArray
API文档 (opens new window)
💖支持
作者博客 (opens new window)
Gitee (opens new window)
GitHub (opens new window)
首页
  • 框架初衷与诞生
  • 介绍
  • 快速上手
  • 配置说明
  • 统一约定
  • MapperRepository
  • JsonObject
  • JsonArray
API文档 (opens new window)
💖支持
作者博客 (opens new window)
Gitee (opens new window)
GitHub (opens new window)
  • 指南

    • 框架初衷与诞生
    • 介绍
    • 快速上手
      • 安装
      • 配置
      • 操作数据库(例子)
    • 配置说明
    • 统一约定
目录

快速上手

提示

本框架(插件)基于mybatis-spring-boot环境。

# 安装

  • 安装mybatis-spring-boot环境
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>${spring-boot.version}</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot.version}</version>
            </dependency>
        </dependencies>
    
        plugins {
        id 'org.springframework.boot' version '${springBootVersion}'
        id 'io.spring.dependency-management' version '${springManagementVersion}'
        id 'java'
        }
    
        dependencies {
        implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:${mybatisSpringVersion}'
        }
    
    // Make sure to add code blocks to your code group
    • 安装本框架(插件)
      
      <!-- https://mvnrepository.com/artifact/top.zuoyu.mybatis/easy-mybatis-spring-boot-starter -->
      <dependency>
          <groupId>top.zuoyu.mybatis</groupId>
          <artifactId>easy-mybatis-spring-boot-starter</artifactId>
          <version>1.0.0</version>
      </dependency>
      
      
      
      // https://mvnrepository.com/artifact/top.zuoyu.mybatis/easy-mybatis-spring-boot-starter
      implementation 'top.zuoyu.mybatis:easy-mybatis-spring-boot-starter:1.0.0'
      
      
      // Make sure to add code blocks to your code group

      # 配置

      这里以MySQL数据库为例,Oracle数据库配置请参考配置说明

      1. 配置spring-boot-jdbc数据库
      
      spring:
        datasource:
          type: com.zaxxer.hikari.HikariDataSource
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://172.0.0.1:3306/xxxx
          username: xxxx
          password: xxxx
      
      

      关于springBoot的配置,这里不多赘述,更多移步springBoot官网 (opens new window)。

      1. 配置easy-mybatis支持的表名(例子)
      
      easy-mybatis:
        table-names: teacher, student
      
      

      这里的table-names配置,表示需要easy-mybatis框架支持的数据表名,多个表名使用逗号隔开。

      即可使用easy-mybatis框架操作teacher和student两个数据表,如果需要支持其他数据表,需要在此配置。

      # 操作数据库(例子)

      
      @SpringBootTest
      class DemoApplicationTests {
      
          // 表示该接口用来操作名称为'teacher'的数据表
          @Magic("teacher")
          private MapperRepository teacherRepository;
      
          // 表示该接口用来操作名称为'student'的数据表
          @Magic("student")
          private MapperRepository studentRepository;
      
      
          // 查询teacher表下所有数据
          @Test
          void teacherTest() {
              teachertRepository.selectList().forEach(System.out::println);
          }
      
          // 查询student表下符合特定条件的数据
          @Test
          void studentTest() {
              studentRepository.selectListByExample(
                new JsonObject().put("birthday", "2009/12/12 12:12:12")
                ).forEach(System.out::println);
          }
      
      }
      
      

      使用MapperRepository接口对数据库进行操作,需要使用@Magic("表名称")标记该接口的数据表归属。

      在本例中,@Magic("teacher")表示该MapperRepository为"teacher"数据表的操作接口,可以通过teacherRepository调用一系列方法完成对"teacher"数据表的操作。

      介绍
      配置说明

      ← 介绍 配置说明→

      最近更新
      更多文章>
      Theme by Vdoing | Copyright © 2021-2022 zuoyu | MIT License | © 豫ICP备19014153号-1
      • 跟随系统
      • 浅色模式
      • 深色模式
      • 阅读模式