feat(BAEL-3044) Implement MyBatis with Spring/SpringBoot module (#7507)
This commit is contained in:
committed by
KevinGilmore
parent
7f7fd337a7
commit
8ff8628008
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,3 @@
|
||||
TRUNCATE TABLE ARTICLES;
|
||||
INSERT INTO ARTICLES
|
||||
VALUES (1, 'Working with MyBatis in Spring', 'Baeldung');
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE IF NOT EXISTS `ARTICLES`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY,
|
||||
`title` VARCHAR(100) NOT NULL,
|
||||
`author` VARCHAR(100) NOT NULL
|
||||
);
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user