今天针对SpringBoot中使用junit进行测试整合MyBatis进行数据库操作展开了学习1.在使用junit进行单元测试的时候要注意junit的测试单元所在包要与主程序中入口类的包层级相同或者在其子包中要不然就需要使用SpringBootTest(classes Day4SpringBootJuintApplication.class)来指明入口类的位置2.Spring整合MyBatis对应的pom.xml文件?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdorg.example/groupId artifactIdday04_spring_boot_mybatis/artifactId version0.0.1-SNAPSHOT/version nameday04_spring_boot_mybatis/name descriptionday04_spring_boot_mybatis/description properties java.version11/java.version project.build.sourceEncodingUTF-8/project.build.sourceEncoding project.reporting.outputEncodingUTF-8/project.reporting.outputEncoding spring-boot.version2.6.13/spring-boot.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version2.2.2/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-devtools/artifactId scoperuntime/scope optionaltrue/optional /dependency dependency groupIdcom.mysql/groupId artifactIdmysql-connector-j/artifactId scoperuntime/scope /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdio.springfox/groupId artifactIdspringfox-boot-starter/artifactId version3.0.0/version /dependency dependency groupIdcom.github.pagehelper/groupId artifactIdpagehelper-spring-boot-starter/artifactId version1.4.1/version /dependency /dependencies dependencyManagement dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-dependencies/artifactId version${spring-boot.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId version3.8.1/version configuration source11/source target11/target encodingUTF-8/encoding /configuration /plugin plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId version${spring-boot.version}/version configuration mainClasscom.djw.Day04SpringBootMybatisApplication/mainClass skiptrue/skip /configuration executions execution idrepackage/id goals goalrepackage/goal /goals /execution /executions /plugin /plugins /build /project接下来需要配置yml文件在yml文件中配置数据源的相关信息spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?serverTimezoneGMT%2B8 username: 账户名 password: 密码 mvc: pathmatch: matching-strategy: ant_path_matcher mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl并且通过log-impl也可以配置好日志管理编写对应MapperMapper.xml,Service,entity类我就不放上来了这个都是固定的主要的是我们整合MyBatis和SpringBoot的时候需要配置Mapper的扫描有两种方式1.通过在入口类使用MapperScan(basePackagesmapper所在包)package com.djw; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication MapperScan(basePackages com.djw.mapper) public class Day04SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(Day04SpringBootMybatisApplication.class, args); } }2.在Mapper接口处使用Mapperpackage com.djw.mapper; import com.djw.entity.Student; import org.apache.ibatis.annotations.*; import java.util.List; /** * author djw */ Mapper public interface StudentMapper { Select(select * from student) ListStudent selectList(); Insert(insert into student values(null,#{name},#{age},#{gender})) void insert(Student student); Update(update student set name #{name},age #{age},gender #{gender} where id #{id}) void update(Student student); Delete(delete student where id #{id}) void delete(int id); }3.MyBatis中引入MyBatis-Plus我们知道MyBatis在使用时要编写对应的mapper有一些格式化的SQL编写起来会浪费时间所以我们引入MyBatis-Plus可以直接给我们的一些简单的固定化的SQL来进行实现对应的pom.xml文件?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdorg.example/groupId artifactIdday04_mybatis_mybatis-plus/artifactId version1.0-SNAPSHOT/version properties maven.compiler.source11/maven.compiler.source maven.compiler.target11/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties dependencies dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.28/version /dependency dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version3.5.9/version /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId version1.18.22/version /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.13.2/version scopetest/scope /dependency dependency groupIdorg.slf4j/groupId artifactIdslf4j-log4j12/artifactId version2.0.0-alpha7/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-core/artifactId version5.3.15/version /dependency dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus/artifactId version3.5.1/version /dependency /dependencies build resources resource directorysrc/main/java/directory includes include**/*.properties/include include**/*.xml/include /includes /resource /resources /build /project在使用时我们原来需要在mapper接口进行方法的注册但是如果我们想获取到MyBatis-Plus为我们提供的方法的话我们可以直接让mapper接口继承BaseMapper实体类名package com.djw.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.djw.entity.Student; import org.apache.ibatis.annotations.Select; import java.util.List; /** * author djw */ public interface StudentMapper extends BaseMapperStudent { }然后我们在创建SqlSession的时候MyBatis顶层的需要一个SqlSessionFactoryBuilder我们如果使用MyBatisPlus提供的方法的话我们需要使用SqlSessionFactoryBuilder的子类MyBatisSqlSessionFactoryBuilder这样下来我们就可以直接获取到MyBatis-plus为我们提供的方法了package com.djw.test; import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder; import com.djw.entity.Student; import com.djw.mapper.StudentMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.util.List; /** * author djw */ public class Test { public static void main(String[] args) { SqlSessionFactoryBuilder sfb new MybatisSqlSessionFactoryBuilder(); try { SqlSession sqlSession sfb.build(Resources.getResourceAsStream(sqlConfig.xml)).openSession(); StudentMapper mapper sqlSession.getMapper(StudentMapper.class); ListStudent students mapper.selectList(null); students.forEach(System.out::println); sqlSession.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
SpringBoot学习日记——DAY04(整合junit,myBatis)
发布时间:2026/5/22 6:57:15
今天针对SpringBoot中使用junit进行测试整合MyBatis进行数据库操作展开了学习1.在使用junit进行单元测试的时候要注意junit的测试单元所在包要与主程序中入口类的包层级相同或者在其子包中要不然就需要使用SpringBootTest(classes Day4SpringBootJuintApplication.class)来指明入口类的位置2.Spring整合MyBatis对应的pom.xml文件?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdorg.example/groupId artifactIdday04_spring_boot_mybatis/artifactId version0.0.1-SNAPSHOT/version nameday04_spring_boot_mybatis/name descriptionday04_spring_boot_mybatis/description properties java.version11/java.version project.build.sourceEncodingUTF-8/project.build.sourceEncoding project.reporting.outputEncodingUTF-8/project.reporting.outputEncoding spring-boot.version2.6.13/spring-boot.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version2.2.2/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-devtools/artifactId scoperuntime/scope optionaltrue/optional /dependency dependency groupIdcom.mysql/groupId artifactIdmysql-connector-j/artifactId scoperuntime/scope /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdio.springfox/groupId artifactIdspringfox-boot-starter/artifactId version3.0.0/version /dependency dependency groupIdcom.github.pagehelper/groupId artifactIdpagehelper-spring-boot-starter/artifactId version1.4.1/version /dependency /dependencies dependencyManagement dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-dependencies/artifactId version${spring-boot.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId version3.8.1/version configuration source11/source target11/target encodingUTF-8/encoding /configuration /plugin plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId version${spring-boot.version}/version configuration mainClasscom.djw.Day04SpringBootMybatisApplication/mainClass skiptrue/skip /configuration executions execution idrepackage/id goals goalrepackage/goal /goals /execution /executions /plugin /plugins /build /project接下来需要配置yml文件在yml文件中配置数据源的相关信息spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?serverTimezoneGMT%2B8 username: 账户名 password: 密码 mvc: pathmatch: matching-strategy: ant_path_matcher mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl并且通过log-impl也可以配置好日志管理编写对应MapperMapper.xml,Service,entity类我就不放上来了这个都是固定的主要的是我们整合MyBatis和SpringBoot的时候需要配置Mapper的扫描有两种方式1.通过在入口类使用MapperScan(basePackagesmapper所在包)package com.djw; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication MapperScan(basePackages com.djw.mapper) public class Day04SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(Day04SpringBootMybatisApplication.class, args); } }2.在Mapper接口处使用Mapperpackage com.djw.mapper; import com.djw.entity.Student; import org.apache.ibatis.annotations.*; import java.util.List; /** * author djw */ Mapper public interface StudentMapper { Select(select * from student) ListStudent selectList(); Insert(insert into student values(null,#{name},#{age},#{gender})) void insert(Student student); Update(update student set name #{name},age #{age},gender #{gender} where id #{id}) void update(Student student); Delete(delete student where id #{id}) void delete(int id); }3.MyBatis中引入MyBatis-Plus我们知道MyBatis在使用时要编写对应的mapper有一些格式化的SQL编写起来会浪费时间所以我们引入MyBatis-Plus可以直接给我们的一些简单的固定化的SQL来进行实现对应的pom.xml文件?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdorg.example/groupId artifactIdday04_mybatis_mybatis-plus/artifactId version1.0-SNAPSHOT/version properties maven.compiler.source11/maven.compiler.source maven.compiler.target11/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties dependencies dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.28/version /dependency dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version3.5.9/version /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId version1.18.22/version /dependency dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.13.2/version scopetest/scope /dependency dependency groupIdorg.slf4j/groupId artifactIdslf4j-log4j12/artifactId version2.0.0-alpha7/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-core/artifactId version5.3.15/version /dependency dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus/artifactId version3.5.1/version /dependency /dependencies build resources resource directorysrc/main/java/directory includes include**/*.properties/include include**/*.xml/include /includes /resource /resources /build /project在使用时我们原来需要在mapper接口进行方法的注册但是如果我们想获取到MyBatis-Plus为我们提供的方法的话我们可以直接让mapper接口继承BaseMapper实体类名package com.djw.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.djw.entity.Student; import org.apache.ibatis.annotations.Select; import java.util.List; /** * author djw */ public interface StudentMapper extends BaseMapperStudent { }然后我们在创建SqlSession的时候MyBatis顶层的需要一个SqlSessionFactoryBuilder我们如果使用MyBatisPlus提供的方法的话我们需要使用SqlSessionFactoryBuilder的子类MyBatisSqlSessionFactoryBuilder这样下来我们就可以直接获取到MyBatis-plus为我们提供的方法了package com.djw.test; import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder; import com.djw.entity.Student; import com.djw.mapper.StudentMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.util.List; /** * author djw */ public class Test { public static void main(String[] args) { SqlSessionFactoryBuilder sfb new MybatisSqlSessionFactoryBuilder(); try { SqlSession sqlSession sfb.build(Resources.getResourceAsStream(sqlConfig.xml)).openSession(); StudentMapper mapper sqlSession.getMapper(StudentMapper.class); ListStudent students mapper.selectList(null); students.forEach(System.out::println); sqlSession.close(); } catch (IOException e) { throw new RuntimeException(e); } } }