Mybatis不啟動項目直接測試Mapper的實現(xiàn)方法
前言
在項目開發(fā)過程中,有時候一個龐大的SpringBoot 項目的啟動時間可能要幾分鐘的時間,這時候我們?nèi)绻霚y試自己寫的某個mybatis的Mapper的方法,要浪費大量時間在等待項目啟動上。
本文通過一個Main方法和一個Mybatis配置類實現(xiàn)無需啟動項目直接測試Mapper功能。
本文的工程目錄結(jié)構(gòu)如下:

1. 依賴
由于現(xiàn)在很多項目都是直接用mybatis-plus,所以本文依賴也是直接選用了mybatis-plus。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
<dependency>2. 數(shù)據(jù)庫
數(shù)據(jù)庫建表語句:
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `score` int(11) NOT NULL, `age` int(5) NOT NULL, `gender` int(4) NOT NULL, `oms_order_no` varchar(255) NOT NULL COMMENT 'oms單號', `warehouse_code` varchar(64) NOT NULL COMMENT '倉庫編碼', PRIMARY KEY (`id`), UNIQUE KEY `idx_unique_oms_order_warehouse_code` (`oms_order_no`,`warehouse_code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
數(shù)據(jù)庫中數(shù)據(jù):

3. 實體類
package com.whut.mybatis.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "student")
public class Student {
/**
* id
*/
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
/**
* 名字
*/
@TableField("name")
private String name;
/**
* 分數(shù)
*/
@TableField("score")
private Integer score;
/**
* 年齡
*/
@TableField("age")
private Integer age;
/**
* 性別
*/
@TableField("gender")
private Integer gender;
/**
* oms單號
*/
@TableField("oms_order_no")
private String omsOrderNo;
/**
* 倉庫號
*/
@TableField("warehouse_code")
private String warehouseCode;
}
4. Mapper文件
mapper接口:
public interface StudentMapper extends BaseMapper<Student> {
@Select("select age from student")
List<Integer> getAllage();
Student findByName(@Param("name") String name);
}
對應(yīng)的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.whut.mybatis.mapper.StudentMapper">
<select id="findByName" resultType="com.whut.mybatis.domain.Student">
select * from student where name=#{name} limit 1
</select>
</mapper>
5. 配置類
package com.whut.mybatis.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.whut.mybatis.mapper")
public class TestMybatisConfig {
@Bean(name = "testDataSource")
public DataSource dbDataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
driverManagerDataSource.setPassword("whut123456");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setUrl("jdbc:mysql://123.60.223.167:3306/mybatis?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
return driverManagerDataSource;
}
@Bean(name = "testSqlSessionFactory")
public SqlSessionFactory dbSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource,
@Value("classpath*:mapper/*Mapper.xml") Resource[] mapperLocations) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(mapperLocations);
//https://blog.csdn.net/weixin_41785851/article/details/119739897
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true);
// 配置打印sql語句
configuration.setLogImpl(StdOutImpl.class);
bean.setConfiguration(configuration);
return bean.getObject();
}
@Bean(name = "testTransactionManager")
public DataSourceTransactionManager dbTransactionManager(@Qualifier("testDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
6. Main方法
這個Main方法的原理是是使用了一種注解方式的SpringBoot容器AnnotationConfigApplicationContext,這個容器里面只存放了配置BeanTestMybatisConfig,因此容器的啟動速度非???。
package com.whut.mybatis;
import com.whut.mybatis.config.TestMybatisConfig;
import com.whut.mybatis.domain.Student;
import com.whut.mybatis.mapper.StudentMapper;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestMybatisConfig.class);
Student student = ac.getBean(StudentMapper.class).findByName("111");
System.out.println(student);
}
}啟動后控制臺打印如下:
JDBC Connection [com.mysql.jdbc.JDBC4Connection@33f676f6] will not be managed by Spring
==> Preparing: select * from student where name=? limit 1
==> Parameters: 111(String)
<== Columns: id, name, score, age, gender, oms_order_no, warehouse_code
<== Row: 3, 111, 100, 25, 1, 5416161, 4841851515
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6e535154]
Student(id=3, name=111, score=100, age=25, gender=1, omsOrderNo=5416161, warehouseCode=4841851515)
綜上所述,本文通過一個Main方法和一個Mybatis配置類實現(xiàn)無需啟動項目直接測試Mapper功能。
雖然我們沒有啟動項目,但是實際上還是啟動了一個SpringBoot容器,只是這個容器內(nèi)的Bean非常少,所以啟動速度非常快。
當(dāng)然這個容器也是必須要有的,不然Mybatis也無法正常工作。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring學(xué)習(xí)之動態(tài)代理(JDK動態(tài)代理和CGLIB動態(tài)代理)
本篇文章主要介紹了Spring學(xué)習(xí)之動態(tài)代理(JDK動態(tài)代理和CGLIB動態(tài)代理),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
通過實例學(xué)習(xí)Spring @Required注釋原理
這篇文章主要介紹了通過實例學(xué)習(xí)Spring @Required注釋原理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Java對象轉(zhuǎn)JSON時動態(tài)的增刪改查屬性詳解
這篇文章主要介紹了Java對象轉(zhuǎn)JSON時如何動態(tài)的增刪改查屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java獲取當(dāng)前操作系統(tǒng)的信息實例代碼
這篇文章主要介紹了Java獲取當(dāng)前操作系統(tǒng)的信息實例代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
詳解Spring batch 入門學(xué)習(xí)教程(附源碼)
本篇文章主要介紹了Spring batch 入門學(xué)習(xí)教程(附源碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
如何解決Idea沒有elementui標(biāo)簽的代碼提示問題
這篇文章主要介紹了如何解決Idea沒有elementui標(biāo)簽的代碼提示問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
用SpringBoot框架來接收multipart/form-data文件方式
這篇文章主要介紹了用SpringBoot框架來接收multipart/form-data文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
JVM內(nèi)存管理之JAVA語言的內(nèi)存管理詳解
下面小編就為大家?guī)硪黄狫VM內(nèi)存管理之JAVA語言的內(nèi)存管理詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

