Java SpringBoot 整合 MyBatis 小案例

Java SpringBoot 整合 MyBatis 小案例




基础配置(注意版本号,容易报错)

  • pom.xml
xml 复制代码
<?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.1.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhong</groupId>
    <artifactId>SpringBootUseMyBatisDome</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootUseMyBatisDome</name>
    <description>SpringBootUseMyBatisDome</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!--MySQL依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <!--MyBatis 依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

</project>
  • application.yml
yml 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis
    username: root
    password: 123456

1、创建数据库

sql 复制代码
create database if not exists mybatis;
use mybatis;
create table IF NOT EXISTS user (
	id int unsigned primary key auto_increment comment 'ID',
	name varchar(100) comment '姓名',
	age tinyint unsigned comment '年龄',
	gender tinyint unsigned comment '性别,1:男,2:女',
	phone varchar (11) comment '手机号'
)comment '用户表';

insert into user(id, name, age, gender, phone) VALUES (null, '白眉鹰王', 55, '1', '18800000000');
insert into user(id, name, age, gender, phone) VALUES (null, '金毛狮王', 45, '1', '18800000001');
insert into user(id, name, age, gender, phone) VALUES (null, '青翼蝠王', 38, '1', '18800000002');
insert into user(id, name, age, gender, phone) VALUES (null, '紫衫龙王', 42, '2', '18800000003');
insert into user(id, name, age, gender, phone) VALUES (null, '光明左使', 37, '1', '18800000004');
insert into user(id, name, age, gender, phone) VALUES (null, '光明右使', 48, '1', '18800000005');

2、创建 pojo 数据模型层

java 复制代码
package com.zhong.springbootusemybatisdome.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @ClassName : User
 * @Description : 学生实体类
 * @Author : zhx
 * @Date: 2024-02-26 18:19
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private Short age;
    private Short gender;
    private String phone;
}

3、创建 mapper 逻辑层

java 复制代码
package com.zhong.springbootusemybatisdome.mapper;

import com.zhong.springbootusemybatisdome.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * @ClassName : UserMapper
 * @Description : 学生操作接口
 * @Author : zhx
 * @Date: 2024-02-26 18:21
 */
@Mapper
public interface UserMapper {
    @Select("select * from user where id = #{id}")
    public User findUserById(Integer id);
}

4、创建 service 服务层

java 复制代码
package com.zhong.springbootusemybatisdome.service;

import com.zhong.springbootusemybatisdome.pojo.User;

/**
 * @ClassName : UserService
 * @Description : User 服务类
 * @Author : zhx
 * @Date: 2024-02-26 18:25
 */
public interface UserService {
    public User findUserById(Integer id);
}

5、实现接口

java 复制代码
package com.zhong.springbootusemybatisdome.service.impl;

import com.zhong.springbootusemybatisdome.mapper.UserMapper;
import com.zhong.springbootusemybatisdome.pojo.User;
import com.zhong.springbootusemybatisdome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName : UserServicelmpl
 * @Description : 服务实现类
 * @Author : zhx
 * @Date: 2024-02-26 18:26
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }
}

5、创建 controller 控制层

java 复制代码
package com.zhong.springbootusemybatisdome.controller;

import com.zhong.springbootusemybatisdome.pojo.User;
import com.zhong.springbootusemybatisdome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName : UserController
 * @Description : 学生控制类
 * @Author : zhx
 * @Date: 2024-02-26 18:29
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;

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

查询成功

java 复制代码
http://localhost:8080/findUserById?id=1
相关推荐
王网aaa13 分钟前
堆结构和堆排序
java·算法·排序算法
无名指的等待71237 分钟前
SpringBoot实现一个Redis限流注解
spring boot·redis·后端
计算机-秋大田1 小时前
基于Spring Boot的小区疫情购物系统的设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·课程设计
loveking61 小时前
SpringBoot调用华为云短信实现发短信功能
java·spring boot·华为云
小马爱打代码1 小时前
Spring Boot 事务管理:实战案例解析
spring boot
李长渊哦1 小时前
Spring Boot 约定大于配置:实现自定义配置
java·spring boot·后端
上官美丽1 小时前
单一责任原则在Java设计模式中的深度解析
java·开发语言·设计模式
橙序研工坊1 小时前
Java基础语法练习42(基本绘图-基本的事件处理机制-小坦克的绘制-键盘控制坦克移动)
java·开发语言
等什么君!2 小时前
学习springboot 的自动配置原理
java·spring boot·学习
白晨并不是很能熬夜2 小时前
【JVM】性能监控与调优概述篇
java·jvm·经验分享·后端·面试·求职招聘