springboot整合mybatis

1.创建项目

这里使用的ider版本是2023.3.4
springboot版本是3.3
jdk是21
在这里插入图片描述
在这里插入图片描述

2.配置依赖

注意:mybatis的版本要是3.0.3或者可以不写版本,因为在 Maven 的 pom.xml 文件中,如果你不指定某个依赖的版本号(<version> 标签),那么 Maven 会按照以下顺序来确定版本号:

  1. **父 POM 中定义的版本:**如果当前项目有一个父 POM,并且父 POM 中定义了该依赖的版本号,则会使用父 POM 中定义的版本号。
  2. **项目自身的依赖管理:**如果项目自身在 <dependencyManagement> 中定义了该依赖的版本号,则会使用这个版本号。
  3. **默认版本:**如果没有在任何地方定义版本号,则会使用该依赖的最新版本或默认版本。

因此,如果你不设置版本号,通常情况下会使用父 POM 或者项目自身定义的版本号。如果没有这些定义,则会使用该依赖的最新版本或默认版本。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.Y</groupId>
    <artifactId>springboot1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot1</name>
    <description>springboot1</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

<!--        mybattis起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

<!--        mysql起步依赖-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.配置application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456


4.编写代码

popj

package org.y.springboot1.popj;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private Short age;
    private Short gender;
    private String phone;
}

UserService

package org.y.springboot1.service;

import org.y.springboot1.popj.User;

public interface UserService {
    public User getUserById(Integer id);
}

UserServicelmpl

package org.y.springboot1.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.y.springboot1.mapper.UserMapper;
import org.y.springboot1.popj.User;
import org.y.springboot1.service.UserService;

@Service
public class UserServicelmpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }
}

UserMapper

package org.y.springboot1.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.y.springboot1.popj.User;

@Mapper
public interface UserMapper {
    //根据id查询用户
    @Select("select * from user where id = #{id}")
    public User getUserById(Integer id);
}

UserController

package org.y.springboot1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.y.springboot1.popj.User;
import org.y.springboot1.service.UserService;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUserById")
    public User getUserById(Integer id) {
        return userService.getUserById(id);
    }
}

5.可能报错情况

在这里插入图片描述
是因为mybatis版本不兼容导致,提高其版本便可,可看前面配置依赖的解释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨不会编程(っ◞‸◟c)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值