首先看一下整个项目的文件结构
接下来我们一步一步完成这个结构
首先新建一个spring boot的项目
为项目添加3个依赖,分别是web、jdbc、MySQL driver
到https://mvnrepository.com/找到mybatis的依赖
加到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>2.3.4.RELEASE</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>11</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.1.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在properties文件里面配置好datasource的四项
为了测试配置是否成功,在test文件夹的测试类中添加代码
package com.example.springbootmybatis;
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 SpringbootMybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
运行没有报错,说明配置成功。
下面新建一个pojo包,在里面建一个实体类user
package com.example.springbootmybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author KNOE
* @date 2020-10-17 16:33
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
在user类中,添加三个私有属性,与tbl表中的属性完全对应
注意到在类中完全没有编写构造器,是因为使用了Lombok的注解
两个注解分别为类自动生成了无参构造和有参构造
建立实体类后,先添加一个数据源
数据库选之前一直用的testdb
可以看到在schemas中有我们的testdb表。属性跟user类一致。
双击tbl可以看到完整的数据内容。这么看idea其实可以当一个MySQL的client用
右键数据库,点open console
可以正常执行sql语句
下面新建一个mapper包,为user类建立mapper接口
package com.example.springbootmybatis.mapper;
import com.example.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author KNOE
* @date 2020-10-17 16:47
*/
@Mapper // 表示这是一个mybatis的mapper类
@Repository
public interface UserMapper {
List<User> queryList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
其中共包含5个方法,之后要逐个实现这些方法
下面要正式开始用mybatis了
在resource里面新建一个mybatis/mapper目录
在下面建立一个mapper的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">
<mapper namespace="com.example.springbootmybatis.mapper.UserMapper">
<select id="queryList" resultType="User">
select * from tbl
</select>
<select id="queryUserById" resultType="User">
select * from tbl where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into tbl (id, name, pwd) values (#{id}, #{name}, #{pwd})
</insert>
<update id="updateUser" parameterType="User">
update tbl set name=#{name}, pwd=#{pwd} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from tbl where id=#{id}
</delete>
</mapper>
其中开头的部分是固定的,就是在mapper标签的内部用xml实现了sql语句
namespace要写对mapper接口的路径
可以看到不同的语句对应的标签是不一样的。其中id必须与mapper接口中定义的方法名完全一样。
到这里还不算完,还需要配置mybatis的properties
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.example.springbootmybatis.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
一个写pojo路径,一个mybatis xml路径,注意这个从resources下面开始写,开头没有/不要乱加
这里idea显示解析不了MySQL包,不去管他
最后就是要看到我们的成果,写一个controller
package com.example.springbootmybatis.controller;
import com.example.springbootmybatis.mapper.UserMapper;
import com.example.springbootmybatis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author KNOE
* @date 2020-10-18 15:22
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/queryList")
public List<User> queryList() {
List<User> userList = userMapper.queryList();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
@GetMapping("/queryUserById")
public User queryUserById(@RequestParam(value = "id", defaultValue = "666") String str) {
int id = Integer.parseInt(str);
return userMapper.queryUserById(id);
}
@GetMapping("/addUser")
public String addUser() {
userMapper.addUser(new User(2020, "Knight", "123456"));
return "ok";
}
@GetMapping("/updateUser")
public String updateUser() {
userMapper.updateUser(new User(2020, "Knight", "654321"));
return "ok";
}
@GetMapping("/deleteUser")
public String deleteUser() {
userMapper.deleteUser(2020);
return "ok";
}
}
其中有个带参的方法
可以运行springboot看到结果了,非常舒适 。