导言
一、准备工作:快速创建项目
1. 创建项目
2. 项目细节
3. 勾选项目依赖(重点)
4. 项目结构
我这里修改了一个 banner.txt。
5. 修改 application.yml 配置文件
- 修改
application.properties
配置文件后缀为:application.yml
- 配置 数据源 内容,否则报错。
spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://IP:3306/feng?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
6. 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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.feng.mybatis</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_mybatis</name>
<description>springboot_mybatis</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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.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>
二、mybatis 环境配置
1. mybatis 依赖:mybatis-springboot-starter
a、添加依赖
导入mybatis-springboot-starter
依赖
<!-- mybatis-springboot-starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
b、全部依赖
<?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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.feng.mybatis</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_mybatis</name>
<description>springboot_mybatis</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- mybatis-springboot-starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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.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>
2. 版本对应
3. 编写application.yml配置文件
a、添加配置
# 整合mybatis
mybatis:
type-aliases-package: com.feng.springboot_mybatis.bean # 给pojo实体类起别名
mapper-locations: classpath:mybatis/mapper/*.xml # 接口的配置文件的位置 我这里接口配置文件是UserMapper.xml
b、全部配置
spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://IP:3306/feng?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
# 整合mybatis
mybatis:
type-aliases-package: com.feng.springboot_mybatis.bean # 给pojo实体类起别名
mapper-locations: classpath:mybatis/mapper/*.xml # 接口的配置文件的位置 我这里接口配置文件是UserMapper.xml
4. 总结
到此结束,springboot整合mybatis的环境已经搭建好了,即:基础框架就已经搭建好了,直接开始进行案例的操作就可以了。
三、测试案例
1. 项目整体结构(全)
2. bean包下的实体类UserData
package com.feng.springboot_mybatis.bean;
public class UserData {
private Integer user_id;
private String user_name;
private Integer gender;
private String birthday;
private String total_traval_road;
private String total_traval_time;
public UserData(Integer user_id, String user_name, Integer gender, String birthday, String total_traval_road, String total_traval_time) {
this.user_id = user_id;
this.user_name = user_name;
this.gender = gender;
this.birthday = birthday;
this.total_traval_road = total_traval_road;
this.total_traval_time = total_traval_time;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getTotal_traval_road() {
return total_traval_road;
}
public void setTotal_traval_road(String total_traval_road) {
this.total_traval_road = total_traval_road;
}
public String getTotal_traval_time() {
return total_traval_time;
}
public void setTotal_traval_time(String total_traval_time) {
this.total_traval_time = total_traval_time;
}
@Override
public String toString() {
return super.toString();
}
}
3. 编写接口UserDataMapper
package com.feng.springboot_mybatis.mapper;
import com.feng.springboot_mybatis.bean.UserData;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
public interface UserDataMapper {
List<UserData> queryUserList();
UserData queryUserById(int id);
int addUser(UserData user);
int updateUser(UserData user);
int deleteUser(int id);
}
4. 编写接口实现配置文件UserDataMapper.xml
注意点:
- 命名空间
namespace
要写全路径,指定到具体的mapper路径。 id
要与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.feng.springboot_mybatis.mapper.UserDataMapper">
<select id="queryUserList" resultType="UserData">
select * from feng.user_data
</select>
<select id="queryUserById" resultType="UserData">
select *
from feng.user_data
where id=#{id};
</select>
<insert id="addUser" parameterType="UserData">
insert into
feng.user_data(id,name,pwd)
values(#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="UserData">
update feng.user_data
set name=#{name},pwd=#{pwd}
where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete
from feng.user_data
where id=#{id};
</delete>
</mapper>
5. 编写contoller
package com.feng.springboot_mybatis.controller;
import com.feng.springboot_mybatis.bean.UserData;
import com.feng.springboot_mybatis.mapper.UserDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserDataController {
@Autowired
private UserDataMapper userMapper;
@GetMapping("/queryUserList")
public List<UserData> queryUserList(){
List<UserData> userList = userMapper.queryUserList();
for(UserData user:userList){
System.out.println(user);
}
return userList;
}
@GetMapping("/queryUserById")
public UserData queryUserById(){
UserData user = userMapper.queryUserById(1);
return user;
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new UserData(2,"阿毛",1,"20220712","12", "23"));
return "添加成功";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new UserData(1,"秃驴",1, "20220723", "22", "23"));
return "更改成功";
}
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(1);
return "删除成功";
}
}
至此,springboot整合mybatis 并且进行增删改查操作就全部结束了。