Maven 仓库
导入 mybatis-spring-boot-starter 的 jar 包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
新建 Web 项目:
Mysql 驱动:
pom.xml 导入依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</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>
application.properties:
spring.datasource.username=root
spring.datasource.password=root
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
Maven 默认导入 8 以上版本的 mysql
先测试下能不能连接成功
package com.demo.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());
}
}
运行,数据源默认 Hikari,jdbc 连接成功
连接 Mysql 数据库
点击 Schemas 选择自己要连的数据库
右侧展示连接的内容
为了方便使用,pom.xml 导入 Lombok 依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
pojo 包下创建 User 类,与 user 表字段保持一致
package com.demo.springbootmybatis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
Structure 结构:
加注解后包含有参构造、无参构造、get/set 方法、toString() 方法
mapper 包下创建 UserMapper 接口:
package com.demo.springbootmybatis.mapper;
import com.demo.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper //@Mapper注解表示这是一个mybatis的mapper类:Dao
@Repository
public interface UserMapper {
//查询
List<User> queryUserList();
//根据ID查询
User queryUserById(int id);
//添加
int addUser(User user);
//修改
int updateUser(User user);
//删除
int deleteUser(int id);
}
@Mapper 注解表示这是一个 mybatis 的 mapper 类:Dao
也可以通过扫描包的形式,在主程序上方添加注解
@MapperScan("com.demo.springbootmybatis.mapper")
application.properties 整合 mybatis:
spring.datasource.username=root
spring.datasource.password=root
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
mybatis.type-aliases-package=com.demo.springbootmybatis.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
MybatisProperties.class 源码,下述有的都能配置
public static final String MYBATIS_PREFIX = "mybatis";
private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
private String configLocation;
private String[] mapperLocations;
private String typeAliasesPackage;
private Class<?> typeAliasesSuperType;
private String typeHandlersPackage;
private boolean checkConfigLocation = false;
private ExecutorType executorType;
private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
private Properties configurationProperties;
private CoreConfiguration configuration;
resources 目录下创建 mybatis 包,包下创建 mapper 包,再创建 UserMapper.xml 文件
Mybatis 入门
UserMapper.xml:
mapper namespace 改成自己的 mapper 接口
以标签的形式写 sql 语句,id 与接口名一致
<?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.demo.springbootmybatis.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" resultType="User">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="User">
update user set name=#{name},pwd=#{pwd} where id = #{id}
</update>
<delete id="deleteUser" parameterType="User">
delete from user where id = #{id}
</delete>
</mapper>
controller 包下创建 UserController 类:
自动装配 UserMapper,实现 CRUD
package com.demo.springbootmybatis.controller;
import com.demo.springbootmybatis.mapper.UserMapper;
import com.demo.springbootmybatis.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(5,"张三5","123"));
return "ok";
}
//修改用户
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(5,"李四","456"));
return "ok";
}
//根据ID删除用户
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(5);
return "ok";
}
}
测试运行即可!(记得地址加后缀)
总结:
1. 导入包(整合包:mybatis-spring-boot-starter)
2. 配置文件
3. mybatis 配置
4. 编写 sql
5. service 层调用 dao 层
6. controller 层调用 service 层