一、配置依赖
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<!-- junit测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok依赖 为了简化实体类的编写代码量 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
不知道为什么,maven配置的远程仓库(阿里云)一直找不到org.mybatis.spring.boot: mybatis-spring-boot-starter,报如下错误:
Cannot resolve org.mybatis.spring.boot:mybatis-spring-boot-starter:unknown
最终的解决方案是使用MyBatis-plus来替代MyBatis,所以最终使用的是如下依赖信息:
<!-- MyBatis Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- junit测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok依赖 为了简化实体类的编写代码量 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
二、配置文件信息
如果是SpringBoot,在resources目录下创建application.yml文件,这个文件中存放mybaits的相关配置信息。
# 配置数据库的连接字符串
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/你的数据库?characterEncoding=utf8
username: 你的数据库用户名
password: 你的数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #实现mapper.java接口和mapper.xml的绑定
type-aliases-package: com.xj.dao.model #解释参考里有说明
configuration: # 配置打印 MyBatis 执行的 SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #需要lombok框架支持,可以添加
# 配置打印 MyBatis 执行的 SQL
logging:
level:
com:
example:
demo: debug
三、项目举例
1.创建一个Spring Boot项目
参考:Spring Boot之创建一个Spring Boot项目(一)_<plugin> <groupid>org.springframework.boot</groupi-CSDN博客
2.项目架构
3.代码实现
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xj</groupId>
<artifactId>SpringBootStudyProject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--导入JDBC的场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--jquery引入-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<!--Thymeleaf 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--导入数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- MyBatis Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- junit测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok依赖 为了简化实体类的编写代码量 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!--build标签描述了如何来编译及打包项目,而具体的编译和打包工作是通过build中配置的 plugin 来完成-->
<build>
<plugins>
<!--使用SpringBoot的打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
application.yml:
#默认配置
server:
port: 8080
spring:
datasource: #数据源连接信息
username: root
password: 12345
url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
profiles:
active: dev #切换配置,指定使用哪个profile
# 设置 Mybatis 的 xml 保存路径
mybatis-plus:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.xj.dao.model
configuration: # 配置打印 MyBatis 执行的 SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #需要lombok框架支持,可以添加
# 配置打印 MyBatis 执行的 SQL
logging:
level:
com:
example:
demo: debug
---
#开发环境
server:
port: 8081
spring:
config:
activate:
on-profile: dev
---
#测试环境
server:
port: 8082
spring:
config:
activate:
on-profile: test
---
#生产环境
server:
port: 8083
spring:
config:
activate:
on-profile: prod
logging:
config: classpath:logback-spring.xml #指定使用哪个日志配置文件
Student.java:
package com.xj.dao.model;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import org.springframework.data.relational.core.mapping.Column;
import java.io.Serializable;
/**
* @Author: xjfu
* @Create: 2024/03/25 16:20
* @Description:
*/
@Data
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "name")
private String name;
@Column(value = "age")
private Integer age;
}
StudentMapper.java:
package com.xj.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xj.dao.model.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author: xjfu
* @Create: 2024/03/25 16:26
* @Description:
*/
@Repository
public interface StudentMapper extends BaseMapper<Student> {
public List<Student> selectAll();
}
StudentMapper.xml:
<?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.xj.dao.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.xj.dao.model.Student">
<result column="name" property="name" />
<result column="age" property="age" />
</resultMap>
<select id="selectAll" resultType="com.xj.dao.model.Student">
SELECT *
FROM student;
</select>
</mapper>
MainApplication.java:
package com.xj.main;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @Author : xjfu
* @Date : 2022/6/8 8:38
* @Description :Spring Boot 启动类
*/
@ComponentScan("com.xj")
@SpringBootApplication
@MapperScan("com.xj.dao.mapper")
public class MainApplication {
public static void main(String[] args) {
try{
SpringApplication.run(MainApplication.class, args);
}catch (Exception e){
e.printStackTrace();
}
}
}
MyBatisTest:
package com.xj.main;
import com.xj.dao.mapper.StudentMapper;
import com.xj.dao.model.Student;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.sql.SQLException;
import java.util.List;
/**
* @Author : xjfu
* @Date : 2024/3/35 16:34
* @Description : MyBatis 测试类
*/
@SpringBootTest(classes = MainApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyBatisTest {
private static final Logger log = LoggerFactory.getLogger(MyBatisTest.class);
@Autowired
StudentMapper studentMapper;
@Test
void testJdbc() throws SQLException{
List<Student> students = studentMapper.selectAll();
for (Student s : students){
System.out.println(s.toString());
}
}
}
数据库:
CREATE TABLE `student` (
`name` varchar(255) DEFAULT NULL,
`age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
4.运行结果
四、参考
1.在 SpringBoot 中 初步使用 MyBatis_springboot mybatis-CSDN博客
2.SpringBoot结合MyBatis 【超详细】_springboot mybatis-CSDN博客
3.Cannot resolve org.mybatis.spring.bootmybatis-spring-boot-_cannot resolve org.mybatis.spring.boot:mybatis-spr-CSDN博客
4.SpringBoot:CONDITIONS EVALUATION REPORT-CSDN博客
5.@MapperScan注解使用-CSDN博客
6.mybatis的mapper-locations配置_mapperlocations属性怎么配置-CSDN博客
7.type-aliases-package的用法-CSDN博客
8.什么是MyBatis-CSDN博客
9.mybatis – MyBatis 3 | XML 映射器