Java SpringBoot 整合 MyBatis 小案例
基础配置(注意版本号,容易报错)
- pom.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
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 123456
1、创建数据库
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 数据模型层
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 逻辑层
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 服务层
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、实现接口
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 控制层
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);
}
}
查询成功
http://localhost:8080/findUserById?id=1