运行环境:
在Spring 官网提供的在线生成器,生成 SpringBoot 项目,下载解压
地址:https://start.spring.io/
选择JDK,填写相关信息,添加 JDBC依赖,MySQL驱动依赖,SpringWeb依赖
Group:com.serious Artifact:springboot-mybatis Name:springboot-mybatis Description:Demo Package name:com.serious Packaging:Jar JDK:11
点击Generate,下载生成的文件
IDEA打开解压的项目
在Maven仓库找到 mybatis整合SpringBoot的依赖:
<!-- mybatis-spring-boot-starter: 整合 --> <!-- 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.4</version> </dependency>
写到项目的pom.xml文件中
创建数据库springboot_mybatis
create database springboot_mybatis; use springboot_mybatis;
创建 user 表
create table `user`( id int primary key , `name` varchar(20), pwd varchar(20) );
向 user 表中插入数据
insert into user(id, `name`, pwd) values (1, '刘备', 'lb'); insert into user(id, `name`, pwd) values (2, '孙尚香', 'ssx'); insert into user(id, `name`, pwd) values (3, '孙权', 'sq');
编写数据库连接
打开application.properties文件,顺便在resources目录下新建一个文件夹名为mybatis,然后在mybatis文件夹下再建一个文件夹,名为mapper,后面写的XXXmapper.xml文件就应该放到这个目录下
# 配置数据库连接 spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mybatis?useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # mybatis 整合 # 设置别名 mybatis.type-aliases-package=com.serious.pojo # 扫描mapper对应的xml文件 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
编写测试类
package com.serious; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; @SpringBootTest class SpringbootMybatisApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() { System.out.println("数据源是:" + dataSource.getClass()); } }
运行测试类
在com.serious包下创建一个pojo包
在pojo包下创建一个User类
package com.serious.pojo; public class User { private Integer id; private String name; private String pwd; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
package com.serious.mapper; import com.serious.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { // 查询所有用户 public List<User> queryAllUser(); }
<?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.serious.mapper.UserMapper"> <select id="queryAllUser" resultType="User"> select * from user </select> </mapper>
package com.serious.service; import com.serious.pojo.User; import java.util.List; public interface UserService { // 查询所有用户 public List<User> queryAllUser(); }
UserServiceImpl类
package com.serious.service; import com.serious.mapper.UserMapper; import com.serious.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService{ @Autowired UserMapper userMapper; @Override public List<User> queryAllUser() { return userMapper.queryAllUser(); } }
11.编写测试类
package com.serious; import com.serious.pojo.User; import com.serious.service.UserServiceImpl; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class SpringbootMybatisApplicationTests { @Autowired UserServiceImpl userService; @Test void contextLoads() { List<User> userList = userService.queryAllUser(); for (User user : userList){ System.out.println(user); } } }
User{id=1, name='刘备', pwd='lb'} User{id=2, name='孙尚香', pwd='ssx'} User{id=3, name='孙权', pwd='sq'}