springboot整合mybatis
1.创建项目
这里使用的ider版本是2023.3.4
springboot版本是3.3
jdk是21
2.配置依赖
注意:mybatis的版本要是3.0.3或者可以不写版本,因为在 Maven 的 pom.xml 文件中,如果你不指定某个依赖的版本号(<version>
标签),那么 Maven 会按照以下顺序来确定版本号:
- **父 POM 中定义的版本:**如果当前项目有一个父 POM,并且父 POM 中定义了该依赖的版本号,则会使用父 POM 中定义的版本号。
- **项目自身的依赖管理:**如果项目自身在
<dependencyManagement>
中定义了该依赖的版本号,则会使用这个版本号。 - **默认版本:**如果没有在任何地方定义版本号,则会使用该依赖的最新版本或默认版本。
因此,如果你不设置版本号,通常情况下会使用父 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版本不兼容导致,提高其版本便可,可看前面配置依赖的解释