2024.5.15 Wednesday
目录
13.整合MyBatis框架
官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Maven仓库地址:
https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.2.2
我这里选择的是2.2.2版本(要和springboot版本匹配)
13.1.整合测试
13.1.1.新建springboot-05-mybatis项目
添加maven支持(在Project Structure->Modules点击加号添加对应项目),按照惯例修改settings中的maven,jdk和Java版本,Project Structure中的jdk和Java版本。修改pom中的springframework版本到2.7.13,重新加载Maven。
删除多余文件
详见https://download.csdn.net/download/2401_83329143/89303117
13.1.2.导入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>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-05-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-05-mybatis</name>
<description>springboot-05-mybatis</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-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>com.mysql</groupId>
<artifactId>mysql-connector-j</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>
13.1.3.配置数据库连接信息
13.1.3.1.修改application.properties
spring.application.name=springboot-05-mybatis
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
13.1.3.2.修改Springboot05MybatisApplicationTests.java并测试
package com.P33;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
13.1.4.新建pojo文件夹
13.1.4.1.新建User.java
package com.P33.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
13.1.4.2.修改pom.xml
在dependencies中添加lombok支持
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
13.1.5.新建mapper文件夹
13.1.5.1.新建UserMapper.java
package com.P33.mapper;
import com.P33.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper //这个注解表示了这是一个MyBatis的mapper类,或者在Springboot05MybatisApplication中添加@MapperScan("com.P33.mapper")
@Repository //储存库
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
13.1.6.新建UserMapper.xml
<?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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.P33.mapper.UserMapper">
<!-- 这里使用的数据库是自定义的 -->
<!--写<cache></cache>开启缓存-->
<select id="queryUserList" resultType="User">
SELECT * FROM p37jdbc.users;
</select>
<select id="queryUserById" resultType="User">
SELECT * FROM p37jdbc.users WHERE id = #{id};
</select>
<insert id="addUser" parameterType="User">
INSERT INTO p37jdbc.users (id, NAME, PASSWORD) VALUES (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="User">
UPDATE p37jdbc.users SET NAME=#{name},PASSWORD = #{pwd} WHERE id = #{id};
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM p37jdbc.users WHERE id = #{id}
</delete>
</mapper>
13.1.7.修改application.properties
配置文件不能写错!很容易因为一些小错误报500错。而且注释的#号只能在新的一行中才生效,不能接在配置行的后面(看颜色变灰了才可以)。
spring.application.name=springboot-05-mybatis
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合MyBatis aliases:别名
mybatis.type-aliases-package=com.P33.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
#classpath表示总的目录,后面加:表示其子文件夹,如果是/则为同级文件夹
13.1.8.新建controller文件夹
新建UserController.java
package com.P33.controller;
import com.P33.mapper.UserMapper;
import com.P33.pojo.User;
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 UserController {
@Autowired
private UserMapper userMapper;
//查询
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
for (User user : userList){
System.out.println(user);
}
return userList;
}
//添加一个用户
@GetMapping("/addUser")
public String addUser() {
userMapper.addUser(new User(7,"ZzzZzz","123456"));
return "finish";
}
//修改一个用户
@GetMapping("/updateUser")
public String updateUser() {
userMapper.updateUser(new User(7,"ZzzZzz","999999"));
return "finish";
}
//删除一个用户
@GetMapping("/deleteUser")
public String deleteUser() {
userMapper.deleteUser(7);
return "finish";
}
}
13.1.9.重启并运行
http://localhost:8080/queryUserList
http://localhost:8080/updateUser
http://localhost:8080/deleteUser