SpringBoot3整合MyBatis
一、MyBatis整合步骤:
(1).导入依赖:在Spring Boot项目的构建文件(如pom.xml)中添加MyBatis和数据库驱动的相关依赖。例如,如果使用MySQL数据库,您需要添加MyBatis和MySQL驱动的依赖。
(2).配置数据源:在application.properties或application.yml中配置数据库连接信息,包括数据库URL、用户名、密码、mybatis的功能配置等。
(3).创建实体类:创建与数据库表对应的实体类。
(4).创建Mapper接口:创建与数据库表交互的Mapper接口。
(5).创建Mapper接口SQL实现:可以使用mapperxml文件或者注解方式
(6).创建程序启动类
(7).注解扫描:在Spring Boot的主应用类上添加@MapperScan注解,用于扫描和注册Mapper接口。
(8).使用Mapper接口:在需要使用数据库操作的地方,通过依赖注入或直接实例化Mapper接口,并调用其中的方法进行数据库操作。
二、MyBatis整合实践:
1.创建项目
2.导入依赖:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.5</version>
</parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.1</version></dependency><!-- 数据库相关配置启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- druid启动器的依赖 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.18</version></dependency><!-- 驱动类--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies>
3.配置文件:
server:port: 80servlet:context-path: /
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:url: jdbc:mysql:///day01username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Drivermybatis:configuration: # setting配置auto-mapping-behavior: fullmap-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.slf4j.Slf4jImpltype-aliases-package: com.atguigu.pojo # 配置别名mapper-locations: classpath:/mapper/*.xml # mapperxml位置
4.实体类准备:
package com.atguigu.pojo;public class User {private String account ;private String password ;private Integer id ;public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Overridepublic String toString() {return "User{" +"account='" + account + '\'' +", password='" + password + '\'' +", id=" + id +'}';}
}
5.Mapper接口准备:
public interface UserMapper {List<User> queryAll();
}
6.Mapper接口实现(XML)
位置:resources/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace = 接口的全限定符 -->
<mapper namespace="com.atguigu.mapper.UserMapper"><select id="queryAll" resultType="user">select * from users</select></mapper>
7.编写三层架构代码:
controller:
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/list")@ResponseBodypublic List<User> getUser(){List<User> userList = userService.findList();log.info("查询的user数据为:{}",userList);return userList;}}
service:
@Slf4j
@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> findList(){List<User> users = userMapper.queryAll();log.info("查询全部数据:{}",users);return users;}
}
8.启动类和接口扫描:
@MapperScan("com.atguigu.mapper") //mapper接口扫描配置
@SpringBootApplication
public class MainApplication {public static void main(String[] args) {SpringApplication.run(MainApplication.class,args);}
}
9.启动测试