springBoot学习笔记(2)—— 整合mybatis
一、引入jar包和配置文件
1. 引入jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2.完整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>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. application.yml配置文件内容
spring:
datasource:
# 数据源基本配置
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml #配置mapper文件的映射位置
server:
port: 8082 # 配置项目启动端口
二、具体代码内容
1.创建表格
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int NOT NULL,
`user_name` varchar(255) NOT NULL,
`idCardNumber` varchar(255) NOT NULL,
`phone_number` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
2.创建实体类
@Data
public class User {
/**
* 主键id
*/
private Long id;
/**
* 用户名
*/
private String userName;
/**
* 身份证号码
*/
private String idCardNumber;
/**
* 手机号码
*/
private String phoneNumber;
}
说明:
- 此处的@Data注解使用的是lombok需要下载lombok插件才不会报错,作用是在编译项目是自动生成get,set方法。
3.Controller控制层方法
@RestController
@RequestMapping("userController")
public class UserController {
@Resource
UserService userService;
/***
* description: 通过用户id查询用户数据集
* version: 1.0 ->
* date: 2021/12/28 14:42
* author: xiaYZ
* iteration: 迭代说明
* @param userId
* @return com.example.springbootmybatis.entity.User
*/
@ResponseBody
@RequestMapping("findUserById")
public String findUserById(Long userId){
User user = new User();
try{
user = userService.findUserById(userId);
}catch (Exception e){
e.printStackTrace();
System.out.println("查询用户错误");
return "查询用户错误";
}
return user.toString();
}
}
4.service服务层方法
@Service
@Mapper
public interface UserDao {
/**
* description: 通过用户id查询用户信息
* version: 1.0
* date: 2021/12/28 14:36
* author: xiaYZ
* iteration: 迭代说明
* @param userId
* @return
*/
User findUserById(Long userId);
}
5.dao持久层方法
@Mapper
public interface UserDao {
/**
* description: 通过用户id查询用户信息
* version: 1.0
* date: 2021/12/28 14:36
* author: xiaYZ
* iteration: 迭代说明
* @param userId
* @return
*/
User findUserById(Long userId);
}
说明:
- @Mapper注解是说明此类为一个持久层接口,如果没有这个注解的话SpringBoot项目会扫码不到接口方法报错
- 当然你也可以选择在SpringBoot项目的main方法上面加一个@MapperScan(value = “com.example.springbootmybatis.dao”),两个方法任选其一即可。
6.mapper方法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootmybatis.dao.UserDao">
<select id="findUserById" resultType="com.example.springbootmybatis.entity.User">
select id, user_name userName, idcardNumber, phone_number phoneNumber
from user
where id = #{userId}
</select>
</mapper>
可也使用@select注解在接口上面写sql语句,如:
@Select("select * from user where id = #{userId}")
User findUserById(Long userId);
两张方法任选其一即可,不过当sql语句十分复杂时建议使用第一种方法。