文章目录

什么是MyBatis?如何使用MyBatis引人Mybatis的相关依赖配置Mybatis(数据库连接信息)编写SQL语句(注解/XML)单元测试

打印日志

什么是MyBatis?

MyBatis是⼀款优秀的持久层框架,⽤于简化JDBC的开发。

持久层:指的就是持久化操作的层,通常指数据访问层(dao),是⽤来操作数据库的. 简单来说 MyBatis 是更简单完成程序和数据库交互的框架,也就是更简单的操作和读取数据库⼯具 接下来,我们就通过⼀个⼊⻔程序,让⼤家感受⼀下通过Mybatis如何来操作数据库

如何使用MyBatis

引人Mybatis的相关依赖

项⽬⼯程创建完成后,在pom.xml⽂件中,导⼊Mybatis依赖和MySQL驱动依赖

Mybatis 是⼀个持久层框架, 具体的数据存储和数据操作还是在MySQL中操作的, 所以需要添加 MySQL驱动

SpringBoot 3.X对⽤MyBatis版本为3.X

对应关系参考:https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

org.mybat

mybati

3.0.3

com.mysql

mysql-connector-j

runtime

配置Mybatis(数据库连接信息)

Mybatis中要连接数据库,需要数据库相关参数配置

MySQL驱动类登录名密码数据库连接字符串

如果是application.yml⽂件, 配置内容如下:

# 数据库连接配置

spring:

datasource:

url: jdbc:mysql://127.0.0.1:3306/mybatis_test?

characterEncoding=utf8&useSSL=false

username: root

password: root

driver-class-name: com.mysql.cj.jdbc.Driver

注意事项: 如果使⽤ MySQL 是 5.x 之前的使⽤的是"com.mysql.jdbc.Driver",如果是⼤于 5.x 使⽤的 是“com.mysql.cj.jdbc.Driver”.

如果是application.properties⽂件, 配置内容如下

#驱动类名称

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#数据库连接的url

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?

characterEncoding=utf8&useSSL=false

#连接数据库的⽤⼾名

spring.datasource.username=root

#连接数据库的密码

spring.datasource.password=root

编写SQL语句(注解/XML)

在项⽬中, 创建持久层接⼝UserInfoMapper

import com.example.demo.model.UserInfo;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper

public interface UserInfoMapper {

//查询所有⽤⼾

@Select("select username, `password`, age, gender, phone from userinfo")

public List queryAllUser();

}

Mybatis的持久层接⼝规范⼀般都叫 XxxMapper

@Mapper注解:表⽰是MyBatis中的Mapper接⼝ • 程序运⾏时, 框架会⾃动⽣成接⼝的实现类对象(代理对象),并给交Spring的IOC容器管理 • @Select注解:代表的就是select查询,也就是注解对应⽅法的具体实现内容.

单元测试

在创建出来的SpringBoot⼯程中,在src下的test⽬录下,已经⾃动帮我们创建好了测试类 ,我们可以 直接使⽤这个测试类来进⾏测试.

import com.example.demo.mapper.UserInfoMapper;

import com.example.demo.model.UserInfo;

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 DemoApplicationTests {

@Autowired

private UserInfoMapper userInfoMapper;

@Test

void contextLoads() {

List userInfoList = userInfoMapper.queryAllUser();

System.out.println(userInfoList);

}

}

测试类上添加了注解 @SpringBootTest,该测试类在运⾏时,就会⾃动加载Spring的运⾏环境. 我们通过@Autowired这个注解, 注⼊我们要测试的类, 就可以开始进⾏测试了

使⽤Idea ⾃动⽣成测试类 除此之外, 也可以使⽤Idea⾃动⽣成测试类

在需要测试的Mapper接⼝中, 右键 -> Generate -> Test

2. 选择要测试的⽅法, 点击 OK

3. 书写测试代码

1 import com.example.demo.model.UserInfo;

2 import org.junit.jupiter.api.Test;

3 import org.springframework.beans.factory.annotation.Autowired;

4 import org.springframework.boot.test.context.SpringBootTest;

5

6 import java.util.List;

7

8 @SpringBootTest

9 class UserInfoMapperTest {

10 @Autowired

11 private UserInfoMapper userInfoMapper;

12

@Test

void queryAllUser() {

List userInfoList = userInfoMapper.queryAllUser();

System.out.println(userInfoList);

}

}

打印日志

在Mybatis当中我们可以借助⽇志, 查看到sql语句的执⾏、执⾏传递的参数以及执⾏结果 在配置⽂件中进⾏配置即可

mybatis:

configuration: # 配置打印 MyBatis⽇志

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

如果是application.properties, 配置内容如下

#指定mybatis输出⽇志的位置, 输出控制台

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

⭕总结 感谢大家的阅读,希望得到大家的批评指正,和大家一起进步,与君共勉!

文章来源

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。