springboot集成mybatis

1.按照“springboot连接mysql数据库”文章内容进行配置

2.以上配置成功后,pom.xml文件中添加新的依赖

<!-- mybatis-spring-boot-starter -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>

3.在程序主入口Application.java中添加注解@MapperScan("com.szcatic.dao"),扫描DAO数据接口

springboot集成mybatis

4.在application.properties文件中添加mybatis映射文件扫描路径

mybatis.mapper-locations=classpath:mapper/*.xml

5.编写数据接口类UserDao.java

package com.szcatic.dao;

import java.util.List;

import com.szcatic.entity.User;

public interface UserDao {
	
	List<User> findUsers();
}

6.在配置的映射文件路径下建立映射文件userDao.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.szcatic.dao.UserDao">
	<sql id="userColumns">
		f_username AS 'userName', 
		f_password AS 'password'
	</sql>
	
	<select id="findUsers" resultType="com.szcatic.entity.User">
		SELECT 
			<include refid="userColumns"/> 
		FROM t_user
	</select>
	
</mapper>

7.编写测试类UserDaoTest.java

package com.szcatic.test;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.szcatic.Application;
import com.szcatic.dao.UserDao;

@Configuration
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class UserDaoTest {
	
	@Autowired
	private UserDao UserDao;
	
	@Test
	void testUserDao() {
		System.out.println(UserDao.findUsers());
	}
	
}

8.运行测试方法testUserDao(),控制台输出结果如下

15:15:59.995 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
15:16:00.028 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
15:16:00.070 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.szcatic.test.UserDaoTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
15:16:00.084 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.szcatic.test.UserDaoTest], using SpringBootContextLoader
15:16:00.089 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.szcatic.test.UserDaoTest]: class path resource [com/szcatic/test/UserDaoTest-context.xml] does not exist
15:16:00.096 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.szcatic.test.UserDaoTest]: class path resource [com/szcatic/test/UserDaoTestContext.groovy] does not exist
15:16:00.096 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.szcatic.test.UserDaoTest]: no resource found for suffixes {-context.xml, Context.groovy}.
15:16:00.194 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.szcatic.test.UserDaoTest]
15:16:00.480 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.szcatic.test.UserDaoTest]: using defaults.
15:16:00.481 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
15:16:00.521 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [or[email protected]343570b7, org.springframework.test[email protected]157853da, org.spri[email protected]71c3b41, org.springframework.boot.test.a[email protected]236e3f4e, org.springfra[email protected]3cc1435c, org.springframew[email protected]6bf0219d, org.sp[email protected]dd0c991, org.springf[email protected]5f16132a, org.springframework[email protected]69fb6037, org.springframework.boot.test.autoconfi[email protected]36d585c, org.springframework.boot.test.autoconfi[email protected]87a85e1, org.springframework.boo[email protected]671a5887]
15:16:00.526 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [[email protected] testClass = UserDaoTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [[email protected] testClass = UserDaoTest, locations = '{}', classes = '{class com.szcatic.Application, class com.szcatic.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springfr[email protected]1e730495, org.springframework.boot.test.json.DuplicateJsonObje[email protected]60704c, org.[email protected]0, org.springf[email protected]7b227d8d, org.springframework.boot[email protected]0, org.springframework.boot.test.autocon[email protected]4df50bcc], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
15:16:00.562 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-11-28 15:16:01.409  INFO 252 --- [           main] com.szcatic.test.UserDaoTest             : Starting UserDaoTest on zsx with PID 252 (started by admin in F:\workspace4.7\springboot)
2018-11-28 15:16:01.411  INFO 252 --- [           main] com.szcatic.test.UserDaoTest             : No active profile set, falling back to default profiles: default
2018-11-28 15:16:04.139  INFO 252 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-11-28 15:16:04.144  INFO 252 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2018-11-28 15:16:04.221  INFO 252 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 38ms. Found 0 repository interfaces.
2018-11-28 15:16:05.211  INFO 252 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$2f7b86ba] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-28 15:16:14.564  INFO 252 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-28 15:16:15.832  INFO 252 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService
2018-11-28 15:16:16.484  INFO 252 --- [           main] com.szcatic.test.UserDaoTest             : Started UserDaoTest in 15.905 seconds (JVM running for 17.827)
2018-11-28 15:16:17.087  INFO 252 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-11-28 15:16:17.678  INFO 252 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
[User [userName=zhangsan, password=1234], User [userName=lisi, password=1234]]
2018-11-28 15:16:18.003  INFO 252 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService
2018-11-28 15:16:18.014  INFO 252 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2018-11-28 15:16:18.015  INFO 252 --- [       Thread-3] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2018-11-28 15:16:18.052  INFO 252 --- [       Thread-3] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

springboot集成mybatis

从以上输出结果可以看出集成mybatis成功 

9.项目目录

springboot集成mybatis