feat(BAEL-3044) Implement MyBatis with Spring/SpringBoot module (#7507)

This commit is contained in:
FrancoCorleone 2019-08-08 14:40:43 +02:00 committed by KevinGilmore
parent 7f7fd337a7
commit 8ff8628008
13 changed files with 282 additions and 0 deletions

View File

@ -0,0 +1,113 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-mybatis</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-mybatis</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- SpringBoot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- Persistence -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${spring-mybatis.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-mybatis</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- Spring -->
<org.springframework.version>5.1.8.RELEASE</org.springframework.version>
<!-- persistence -->
<spring-mybatis.version>2.0.2</spring-mybatis.version>
<mybatis.version>3.5.2</mybatis.version>
<mybatis-spring-boot-starter.version>2.1.0</mybatis-spring-boot-starter.version>
<h2.version>1.4.197</h2.version>
<!-- testing -->
<assertj-core.version>3.8.0</assertj-core.version>
</properties>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.mybatis.spring;
public class Article {
private Long id;
private String title;
private String author;
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.mybatis.spring;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface ArticleMapper {
@Select("SELECT * FROM ARTICLES WHERE id = #{id}")
Article getArticle(@Param("id") Long id);
}

View File

@ -0,0 +1,14 @@
package com.baeldung.mybatis.spring;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.baeldung.mybatis" }, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { PersistenceConfig.class })
})
public class PersistenceAutoConfig {
}

View File

@ -0,0 +1,37 @@
package com.baeldung.mybatis.spring;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
@Configuration
@MapperScan("com.baeldung.mybatis")
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class })
public class PersistenceConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.build();
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource());
return factoryBean.getObject();
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="schema.sql"/>
<jdbc:script location="data.sql"/>
</jdbc:embedded-database>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="articleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.baeldung.mybatis.spring.ArticleMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>

View File

@ -0,0 +1,3 @@
TRUNCATE TABLE ARTICLES;
INSERT INTO ARTICLES
VALUES (1, 'Working with MyBatis in Spring', 'Baeldung');

View File

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS `ARTICLES`
(
`id` INTEGER PRIMARY KEY,
`title` VARCHAR(100) NOT NULL,
`author` VARCHAR(100) NOT NULL
);

View File

@ -0,0 +1,13 @@
package com.baeldung.mybatis.spring;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = PersistenceAutoConfig.class)
public class ArticleMapperBootIntegrationTest extends ArticleMapperCommonTest {
}

View File

@ -0,0 +1,22 @@
package com.baeldung.mybatis.spring;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.assertj.core.api.Assertions.assertThat;
class ArticleMapperCommonTest {
@Autowired
ArticleMapper articleMapper;
@Test
public void whenRecordsInDatabase_shouldReturnArticleWithGivenId() {
Article article = articleMapper.getArticle(1L);
assertThat(article).isNotNull();
assertThat(article.getId()).isEqualTo(1L);
assertThat(article.getAuthor()).isEqualTo("Baeldung");
assertThat(article.getTitle()).isEqualTo("Working with MyBatis in Spring");
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.mybatis.spring;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = PersistenceConfig.class)
public class ArticleMapperIntegrationTest extends ArticleMapperCommonTest {
}

View File

@ -0,0 +1,11 @@
package com.baeldung.mybatis.spring;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/beans.xml")
public class ArticleMapperXMLIntegrationTest extends ArticleMapperCommonTest {
}

View File

@ -879,6 +879,7 @@
<module>spring-freemarker</module>
<module>persistence-modules/spring-hibernate-3</module>
<module>persistence-modules/spring-hibernate4</module>
<module>persistence-modules/spring-mybatis</module>
<module>spring-integration</module>
<module>spring-jenkins-pipeline</module>
<module>spring-jersey</module>