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
相关推荐
captain3762 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z2 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
文丑颜不良啊3 小时前
LeetCode1481~1500题目解析
java·leetcode
2601_962885723 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓3 小时前
调用openrouter的免费API模型
java·前端·javascript
步行cgn4 小时前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊4 小时前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every4 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
captain3764 小时前
MyBatis(进阶1)
mybatis
萧瑟余晖4 小时前
Spring Batch核心模型与快速上手详解
java·spring·batch