引入依赖项

在 pom.xml 文件中引入 mybatis-spring-boot-starter 的依赖:

  1. <!-- mysql驱动 -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>8.0.22</version>
  6. </dependency>
  7. <!-- mybatis依赖 -->
  8. <dependency>
  9. <groupId>org.mybatis.spring.boot</groupId>
  10. <artifactId>mybatis-spring-boot-starter</artifactId>
  11. <version>2.1.3</version>
  12. </dependency>

配置 MySQL 连接数据

在 application.yml 文件中配置数据库连接:

  1. # MySQL连接配置
  2. spring:
  3. datasource:
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. url: jdbc:mysql://172.30.88.71:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  6. username: root
  7. password: root

Mapper 配置

方式一:【推荐】在启动类上添加包扫描注解 @MapperScan(“com.example.mapper”)

  1. @SpringBootApplication
  2. @MapperScan("com.example.mapper")
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

方式二:在每个 Mapper 类上面添加 @Mapper 注解

Mapper 编写

官方文档对注解功能的说明:https://mybatis.org/mybatis-3/zh/java-api.html

User 实体类:

  1. @Data
  2. @Builder
  3. public class User implements Serializable {
  4. private static final long serialVersionUID = -1602774207630314992L;
  5. private Integer id;
  6. private String username;
  7. private String password;
  8. }

t_user 用户表 sql:

  1. CREATE TABLE `t_user` (
  2. `id` int NOT NULL AUTO_INCREMENT,
  3. `username` varchar(255) DEFAULT NULL,
  4. `password` varchar(255) DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

增删改查示例:

  1. // mapper
  2. public interface UserMapper {
  3. @Select("select * from t_user where id=#{id}")
  4. @Results({
  5. @Result(property = "id", column = "id", javaType = Integer.class),
  6. @Result(property = "username", column = "username", javaType = String.class),
  7. @Result(property = "password", column = "password", javaType = String.class)
  8. })
  9. User selectById(Integer id);
  10. @Select("select * from t_user limit #{limit} offset #{offset}")
  11. List<User> selectAll(@Param("limit") int limit ,@Param("offset") int offset);
  12. @Insert("insert into t_user (id,username,password) values (#{id},#{username},#{password})")
  13. @SelectKey(statement="select last_insert_id()", keyProperty="id", before=false, resultType=int.class)
  14. int insert(User user);
  15. @Update("update t_user set username=#{username},password=#{password} where id=#{id}")
  16. int update(User user);
  17. @Delete("delete from t_user where id=#{tid}")
  18. int delete(@Param("tid") int id);
  19. }

测试:

  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. public class ApplicationTest {
  4. @Resource
  5. private UserMapper userMapper;
  6. // 查
  7. @Test
  8. public void test() {
  9. User user = userMapper.selectById(1);
  10. System.out.println(user);
  11. }
  12. // 查询全部
  13. @Test
  14. public void testSelectAll() {
  15. List<User> users = userMapper.selectAll(2, 2);
  16. users.forEach(System.out::println);
  17. }
  18. // 增
  19. @Test
  20. public void testInsert() {
  21. User user = User.builder().username("张三").password("dfsfknl").build();
  22. int i = userMapper.insert(user);
  23. System.out.println(i);
  24. System.out.println(user);
  25. }
  26. // 更新
  27. @Test
  28. public void testUpdate() {
  29. User user = User.builder().username("张三").password("dfsfknl").build();
  30. int i = userMapper.update(user);
  31. System.out.println(i);
  32. }
  33. // 删除
  34. @Test
  35. public void testDelete() {
  36. int i = userMapper.delete(2);
  37. System.out.println(i);
  38. }
  39. }