一篇超详细的Spring|一篇超详细的Spring Boot整合Mybatis文章
目录
- 配置文件形式
- pom.xml
- application.yml:
- UserMapper.xml
- UserMapper
- 配置springboot整合mybatis
- 在运行类上添加@MapperScan注解
- 测试类
- 效果
- 总结
配置文件形式
![一篇超详细的Spring|一篇超详细的Spring Boot整合Mybatis文章](https://img.it610.com/image/info11/06d3a832139343df8088aba01277f6e0.jpg)
文章图片
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.4.4 com.keafmd spring-boot-09-mybatis0.0.1-SNAPSHOT spring-boot-09-mybatis Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 mysql mysql-connector-javaruntimeorg.springframework.boot spring-boot-starter-testtestorg.springframework.boot spring-boot-maven-plugin
配置数据源
在yml文件中配置数据源。
application.yml:
server:port: 80# 配置数据源spring:datasource:url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=falsedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 18044229# 整合mybatismybatis:#typeAliasesPackage: com.neuedu.entitymapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
UserMapper.xml
这里注意!!!:一定是和UserMapper相同的目录,是个三级目录,创建时仿照这样创建com/keafm/mapper(正确的) 别这样com.keafam.mapper(错误的),这样错误的创建的话,是个一级目录,不是三级的,后面运行的时候可能会提示找不到Mapper。
select * from user
UserMapper
package com.keafmd.mapper; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * Keafmd * * @ClassName: UserMapper * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 16:09 * @Blog: https://keafmd.blog.csdn.net/ */public interface UserMapper {List list(); }
配置springboot整合mybatis
在application.yml中配置:
# 整合mybatismybatis:#typeAliasesPackage: com.neuedu.entitymapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
在运行类上添加@MapperScan注解
SpringBoot09MybatisApplication:
package com.keafmd; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication@MapperScan("com.keafmd.mapper")public class SpringBoot09MybatisApplication {public static void main(String[] args) {SpringApplication.run(SpringBoot09MybatisApplication.class, args); }}
测试类
UserMapperTest :
package com.keafmd.mapper; import com.keafmd.SpringBoot09MybatisApplication; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest(classes = SpringBoot09MybatisApplication.class)class UserMapperTest {@AutowiredUserMapper userMapper; @Testvoid list(){List list = userMapper.list(); for (Object o : list) {System.out.println(o); }}}
效果
![一篇超详细的Spring|一篇超详细的Spring Boot整合Mybatis文章](https://img.it610.com/image/info11/b49615114ca642929a4dd8007d3f95a5.jpg)
文章图片
【一篇超详细的Spring|一篇超详细的Spring Boot整合Mybatis文章】
总结 本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
推荐阅读
- 由伐木工引发的陈述
- 随笔一篇
- 我为什么写不出一篇读书分享稿()
- 2018-06-29|2018-06-29 这是我的第一篇文章 markdown
- 《如何写出一篇好文章》之赠送礼物的训练
- 为了女儿,我尝试画画——DIY绘本之《一支铅笔的梦想》(绘画小白的第一篇作品)
- 自得其乐
- 适用于小白(VSCode搭建Vue项目,最详细的搭建步骤哦)
- 开工平安
- 用|用 Python 写网络编程(二)