MyBatis-Spring-Boot-Starter快速开始

本文介绍了如何使用MyBatis-Spring-Boot-Starter构建应用,包括创建工程、添加Maven依赖、设置mapper和entity文件、数据库及yml配置,以及创建启动类。通过此模块,可以减少XML配置并实现几乎零代码的搭建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搬运一下 MyBatis-Spring-Boot-Starter 的官网介绍:

The MyBatis-Spring-Boot-Starter help you build quickly MyBatis applications on top of the Spring Boot.
By using this module you will achieve:
Build standalone applications
Reduce the boilerplate to almost zero
Less XML configuration

MyBatis-Spring-Boot-Starter可以做什么?:

  1. 会自动扫描带有@mapper注解的mappers,当然也可以用@mapperscan手动指定

我主要讲一下按照官方的指南如何做下去

1. 建工程、添加maven依赖

建工程的时候按照Intellij建工程的流程走就可以,记得把mybatis也加到依赖包里边。

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

2.mapper,entity文件

在包名如 com.example.test 下建立两个名为mapper、entity的文件夹

mapper/fundmapper.java

package com.example.testmybatis.mapper;

import com.example.testmybatis.entity.fund;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface fundmapper {
    @Select("SELECT * FROM fundkey WHERE fundcode = #{fundcode}")
    fund findByState(@Param("fundcode") String state);
}

entity/fund.java 这里我用了@Data注解,所以不要写getter、setter方法

package com.example.testmybatis.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("fundkey")
public class fund {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String fundcode;
    private String fundname;
    private String fundtype;
}

3.数据库、yml文件准备

数据库我是这么建立的:

CREATE TABLE `fundkey`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `fundcode` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `fundname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `fundtype` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 139 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

yml文件里加上了数据库相关的配置:

server:
        port: 8080

spring:
    datasource:
        username: root
        password: root
        url: jdbc:mysql://localhost:3307/fund?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
        driver-class-name: com.mysql.jdbc.Driver

mybatis:
        mapper-locations: classpath:mapping/*Mapper.xml
        type-aliases-package: com.example.testmybatis.entity

4.增加启动类

直接在com.example.testmybatis下增加启动类,基本就是复制官网的再改一改

package com.example.testmybatis;

import com.example.testmybatis.mapper.fundmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleMybatisApplication implements CommandLineRunner {
    @Autowired
    private final fundmapper fundmapper;

    public SampleMybatisApplication(fundmapper fundmapper) {
        this.fundmapper = fundmapper;
    }

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

    @Override
    public void run(String... args) throws Exception {
        System.out.println(this.fundmapper.findByState("004851"));
    }

}

然后run一下这个启动类就可以看到结果了
在这里插入图片描述

官网地址:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值