目录
1、起步依赖
文档SpringBoot版本为2.5.4
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2、数据库表脚本
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
INSERT INTO `user` VALUES (1, '百里慕溪', 20);
SET FOREIGN_KEY_CHECKS = 1;
3、配置数据源
spring:
datasource:
url: jdbc:mysql:///mylean #jdbc:mysql:/// == jdbc:mysql://localhost:3306
username: dbuser
password: root
driver-class-name: com.mysql.jdbc.Driver
4、代码测试
1、注解开发方式
1.实体类
package com.itlean.domain;
/**
* @describe:
* @author: weny.yang
* @date: 2021-09-06 19:38
*/
public class User {
private int id;
private String name;
private int age;
get/set...
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.Mapper接口
package com.itlean.mapper;
import com.itlean.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
public List<User> findAll();
}
3.测试类
package com.itlean;
import com.itlean.domain.User;
import com.itlean.mapper.UserMapper;
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 ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void findAll() {
List<User> userList = userMapper.findAll();
System.out.println(userList);
}
}
输出:
说明整合ok,但是控制台提醒`com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.意思说我们使用的mysql驱动太老本淘汰了,建议我们使用最新的,于是乎我们换上最新的,则不再报提醒:
spring:
datasource:
url: jdbc:mysql:///mylean #jdbc:mysql:/// == jdbc:mysql://localhost:3306
username: dbuser
password: root
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver版本老旧,淘汰
如果报如下错误,说明是时区问题,并且控制台也有对应的提示,图片太大,分开截图如下:
使用提示内给出的字段名,为mysql数据源进行配置如下即可:
spring:
datasource:
url: jdbc:mysql:///mylean?serverTimezone=UTC #jdbc:mysql:/// == jdbc:mysql://localhost:3306 UTC:标准时区
username: dbuser
password: root
driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver版本老旧,淘汰
2、XML开发方式
1.Mapper接口类
package com.itlean.mapper;
import com.itlean.domain.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserXmlMapper { //名字随意取的,为了和上文注解开发区分开
public List<User> findAll();
}
2.XML配置
在resources目录下新建文件夹,命名mapper,在mapper下面新建UserMapper.xml,mybatis的基本配置语法和规范本文不再赘述,如有欠缺请自行补课:
<?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.itlean.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from user
</select>
</mapper>
3.添加Mybatis配置
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件路径
type-aliases-package: com.itlean.domain #别名对应类所在包路径
# config-location: # 指定mybatis的核心配置文件,本次是简单演示,并没有配置
4.测试类
package com.itlean;
import com.itlean.domain.User;
import com.itlean.mapper.UserMapper;
import com.itlean.mapper.UserXmlMapper;
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 ApplicationTests {
@Autowired
private UserXmlMapper userXMLMapper;
@Test
void findAll() {
List<User> userList = userXMLMapper.findAll();
System.out.println(userList);
}
}
输出:
到此,SpringBoot整合Mybatis进行注解开发、xml配置开发就全部介绍完了。
希望对对你有帮助,学到就点个赞吧!