一篇超詳細(xì)的Spring Boot整合Mybatis文章
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.4</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.keafmd</groupId> <artifactId>spring-boot-09-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-09-mybatis</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId></dependency><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>1.3.2</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>
配置數(shù)據(jù)源
在yml文件中配置數(shù)據(jù)源。
application.yml:server: port: 80# 配置數(shù)據(jù)源spring: datasource: url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 18044229# 整合mybatismybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xmlUserMapper.xml
這里注意!!!:一定是和UserMapper相同的目錄,是個(gè)三級(jí)目錄,創(chuàng)建時(shí)仿照這樣創(chuàng)建com/keafm/mapper(正確的) 別這樣com.keafam.mapper(錯(cuò)誤的),這樣錯(cuò)誤的創(chuàng)建的話,是個(gè)一級(jí)目錄,不是三級(jí)的,后面運(yùn)行的時(shí)候可能會(huì)提示找不到Mapper。
<?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.keafmd.mapper.UserMapper'> <select resultType='map'>select * from user </select></mapper>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.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml在運(yùn)行類上添加@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); }}測(cè)試類
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 { @Autowired UserMapper userMapper; @Test void list(){List list = userMapper.list();for (Object o : list) { System.out.println(o);} }}效果
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作2. Java Media Framework 基礎(chǔ)教程3. Python 忽略文件名編碼的方法4. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis5. 解決vue頁(yè)面刷新,數(shù)據(jù)丟失的問(wèn)題6. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)7. 在Mac中配置Python虛擬環(huán)境過(guò)程解析8. python如何實(shí)現(xiàn)word批量轉(zhuǎn)HTML9. 利用單元測(cè)試對(duì)PHP代碼進(jìn)行檢查10. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊
