SpringBoot 集成 MyBatis 框架 SpringCloud

Posted 早起的年轻人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 集成 MyBatis 框架 SpringCloud相关的知识,希望对你有一定的参考价值。

引入依赖
Spring Boot对于MyBatis的支持需要引入mybatis-spring-boot-starter的pom文件。

搜索 mvnrepository

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>3.0.1</version>
</dependency>

以及mysql的依赖

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.32</version>
</dependency>

2 添加MyBatis配置

添加MyBatis配置类,配置相关扫描路径,包括DAO,Model,XML映射文件的扫描。

3 添加数据源配置

打开 application.yml ,添加MySQL数据源连接信息。

3 基本操作

3.1 创建表

CREATE TABLE `t_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `config_name` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='基本配置表';

然后插入一条数据

INSERT INTO `spring_boot_study`.`t_config` (`id`, `config_name`, `create_time`) VALUES (1, '测试', '2023-03-08 00:00:00');

3.2 Controller

import club.bigleading.admin.service.CustomConfigService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("config")
public class CustomConfigController 
    @Resource
    private CustomConfigService customConfigService;
    @GetMapping(value="/list")
    public Object hello() 
        return customConfigService.findList();
    

3.3 Service

CustomConfigService.java

public interface CustomConfigService 
    Object findList();

CustomConfigServiceImple .java

import club.bigleading.admin.dao.CustomConfigMapper;
import club.bigleading.admin.service.CustomConfigService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class CustomConfigServiceImple implements CustomConfigService 
    @Resource
    private CustomConfigMapper customConfigMapper;
    @Override
    public Object findList() 
        return customConfigMapper.selectAll();
    


3.4 Mapper 资源

CustomConfigMapper.java

public interface CustomConfigMapper 

    /**
     * 查询所有的配置
     * @return
     */
    List<CustomConfigEntiy> selectAll();

CustomConfigMapper.xml

<?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="club.bigleading.admin.dao.CustomConfigMapper">
    <resultMap id="BaseResultMap" type="club.bigleading.admin.model.CustomConfigEntiy">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="config_name" jdbcType="VARCHAR" property="configName"/>
    </resultMap>

    <select id="selectAll" resultMap="BaseResultMap">
        select id, config_name, create_time
        from t_config
    </select>

</mapper>
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
public class CustomConfigEntiy  implements Serializable 
    Integer id;
    String configName;
    LocalDateTime createTime;


4 打包资源

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- 打包时拷贝MyBatis的映射文件 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/sqlmap/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>  
                <directory>src/main/resources</directory>  
                    <includes> 
                        <include>**/*.*</include>  
                    </includes> 
                    <filtering>true</filtering>  
            </resource> 
        </resources>
    </build>

然后运行项目

在浏览器中访问
http://localhost:8899/config/list

以上是关于SpringBoot 集成 MyBatis 框架 SpringCloud的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot--集成mybatis映射框架

springboot+mybatis+mysql集成搭建

SpringBoot+Mybatis集成搭建

springboot微服务之集成mybatis和mysql整合完整详细版

SpringBoot | 第九章:Mybatis-plus的集成和使用

springboot mybatis 后台框架平台 集成代码生成器 shiro 权限