├── .gitignore ├── README.md ├── pom.xml ├── spring-boot-mybatis-parent └── pom.xml ├── spring-boot-mybatis-sample ├── pom.xml ├── spring-boot-mybatis-sample-default │ ├── .gitignore │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── sample │ │ │ │ └── springboot │ │ │ │ └── mybatis │ │ │ │ └── defaultconfig │ │ │ │ ├── SampleApplication.java │ │ │ │ ├── service │ │ │ │ ├── Member.java │ │ │ │ └── MemberMapper.java │ │ │ │ └── web │ │ │ │ └── MemberController.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── data.sql │ │ │ └── schema.sql │ │ └── test │ │ └── java │ │ └── sample │ │ └── springboot │ │ └── mybatis │ │ └── defaultconfig │ │ └── SpringDemoApplicationTests.java ├── spring-boot-mybatis-sample-groovy │ ├── .gitignore │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── sample │ │ │ │ └── springboot │ │ │ │ └── mybatis │ │ │ │ └── groovymapper │ │ │ │ ├── SampleApplication.java │ │ │ │ ├── service │ │ │ │ ├── Member.java │ │ │ │ └── MemberMapper.groovy │ │ │ │ └── web │ │ │ │ └── MemberController.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── data.sql │ │ │ └── schema.sql │ │ └── test │ │ └── java │ │ └── sample │ │ └── springboot │ │ └── mybatis │ │ └── groovymapper │ │ └── SpringDemoApplicationTests.java ├── spring-boot-mybatis-sample-hybrid │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── sample │ │ │ │ └── springboot │ │ │ │ └── mybatis │ │ │ │ └── hybrid │ │ │ │ ├── SampleApplication.java │ │ │ │ ├── service │ │ │ │ ├── Member.java │ │ │ │ ├── MemberAnnotationMapper.java │ │ │ │ ├── MemberXmlMapper.java │ │ │ │ └── mapper.xml │ │ │ │ └── web │ │ │ │ └── MemberController.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── data.sql │ │ │ ├── mybatis-configuration.xml │ │ │ └── schema.sql │ │ └── test │ │ └── java │ │ └── sample │ │ └── springboot │ │ └── mybatis │ │ └── hybrid │ │ └── SpringDemoApplicationTests.java ├── spring-boot-mybatis-sample-simple │ ├── .gitignore │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── sample │ │ │ │ └── springboot │ │ │ │ └── mybatis │ │ │ │ └── defaultconfig │ │ │ │ └── SampleApplication.java │ │ └── resources │ │ │ ├── application.yml │ │ │ ├── data.sql │ │ │ └── schema.sql │ │ └── test │ │ └── java │ │ └── sample │ │ └── springboot │ │ └── mybatis │ │ └── defaultconfig │ │ └── SpringDemoApplicationTests.java └── spring-boot-mybatis-sample-xml │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── sample │ │ │ └── springboot │ │ │ └── mybatis │ │ │ └── xmlconfig │ │ │ ├── SampleApplication.java │ │ │ ├── service │ │ │ ├── Member.java │ │ │ ├── MemberMapper.java │ │ │ └── mapper.xml │ │ │ └── web │ │ │ └── MemberController.java │ └── resources │ │ ├── application.yml │ │ ├── data.sql │ │ └── schema.sql │ └── test │ └── java │ └── sample │ └── springboot │ └── mybatis │ └── xmlconfig │ └── SpringDemoApplicationTests.java └── spring-boot-mybatis ├── META-INF └── spring.factories ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── springframework │ │ └── boot │ │ └── mybatis │ │ └── autoconfigure │ │ ├── Mapper.java │ │ ├── MyBatisAutoConfiguration.java │ │ ├── MyBatisConfigurer.java │ │ ├── MyBatisConfigurerAdapter.java │ │ ├── MyBatisMapperScannerRegistrar.java │ │ ├── MyBatisProperties.java │ │ └── SqlSessionFactoryUpdateConfigurationBean.java └── resources │ └── META-INF │ └── spring.factories └── test └── java └── org └── sbcoba └── mymodule ├── AppTest.java └── TestClass.java /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | *.sw? 3 | .#* 4 | *# 5 | *~ 6 | /build 7 | /code 8 | .classpath 9 | .project 10 | .settings 11 | .metadata 12 | .factorypath 13 | .recommenders 14 | bin 15 | build 16 | lib/ 17 | target 18 | .factorypath 19 | .springBeans 20 | interpolated*.xml 21 | dependency-reduced-pom.xml 22 | build.log 23 | _site/ 24 | .*.md.html 25 | manifest.yml 26 | MANIFEST.MF 27 | settings.xml 28 | activemq-data 29 | overridedb.* 30 | *.iml 31 | *.ipr 32 | *.iws 33 | .idea 34 | *.jar 35 | .DS_Store 36 | .factorypath 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot MyBatis 2 | 3 | ## 버전 4 | - Spring Boot 1.3.0.M5 기준 5 | 6 | ## 소개 7 | - Spring Boot Project 에서 기본적으로 MyBatis 지원되지 않기 때문에 만듦 8 | - 간단히 Dependency 추가로 인해 Spring Boot 에서 MyBatis를 사용할 수 있도록 지원 9 | - 기존의 xml 설정없이 모든 기능을 확장할 수 있도록 구현 10 | 11 | ## 실행조건 12 | - JDK 1.6 이상 환경 ( JDK 1.7 이상 추천 ) 13 | - Maven 설치 (추후 Gradle 지원 예정) 14 | - Spring Boot 개발 환경 15 | 16 | ## 소스 내려받기 17 | ```sh 18 | $ git clone https://github.com/sbcoba/spring-boot-mybatis 19 | ``` 20 | 21 | ## 샘플 실행 22 | ```sh 23 | $ cd spring-boot-mybatis 24 | $ mvn clean install 25 | # 샘플 실행 26 | $ cd ../spring-boot-mybatis-sample 27 | $ mvn -pl spring-boot-mybatis-sample-default spring-boot:run 28 | $ mvn -pl spring-boot-mybatis-sample-simple spring-boot:run 29 | $ mvn -pl spring-boot-mybatis-sample-xml spring-boot:run 30 | $ mvn -pl spring-boot-mybatis-sample-hybrid spring-boot:run 31 | $ mvn -pl spring-boot-mybatis-sample-groovy spring-boot:run 32 | ``` 33 | 34 | ## Maven Dependency 설정 35 | ```xml 36 | 37 | org.springframework.boot.mybatis 38 | spring-boot-mybatis 39 | 1.3.0.BUILD-SNAPSHOT 40 | 41 | ``` 42 | 43 | ## 예제설명 44 | - spring-boot-mybatis-sample-default 45 | - 기본적이 설정이 없을 때 작동하는 예제 46 | - 기본적인 설정 Annotation을 사용 47 | - spring-boot-mybatis-sample-simple 48 | - 가장 간단하게 DB에 접근할 수 있도록 하나의 Class에서 작동하는 예제 49 | - spring-boot-mybatis-sample-xml 50 | - XML mapper를 사용하여 작동하는 예제 51 | - 기본적인 XML 설정 필요 52 | - spring-boot-mybatis-sample-hybrid 53 | - 기본 Annotation 형태와 XML형태를 동시에 사용하도록 작동하는 예제 54 | - 단지 테스트형태이며 실무에서는 지양필요 55 | - spring-boot-mybatis-sample-groovy 56 | - 기본 Annotation 형태는 XML형태에서 Multiline이 되지 않기 때문에 Groovy의 '''문자열''' 기능을 사용하여 멀티라인 문자열을 구현 하여 쿼리를 편리하게 작성할 수 있도록 구성된 예제 57 | - 이 부분은 Spring Boot MyBatis와 관계없이 작동하는 예제 58 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.springframework.boot 5 | spring-boot-mybatis-build 6 | 1.3.0.BUILD-SNAPSHOT 7 | pom 8 | spring-boot-mybatis-build 9 | Spring Boot Mybatis Build 10 | http://sbcoba.tistory.com 11 | 12 | Lee su hong 13 | http://sbcoba.tistory.com 14 | 15 | 16 | 17 | Apache License, Version 2.0 18 | http://www.apache.org/licenses/LICENSE-2.0 19 | 20 | 21 | 22 | https://github.com/sbcoba/spring-boot-mybatis 23 | 24 | 25 | 26 | sbcoba 27 | Lee su hong 28 | sbcoba at gmail.com 29 | 30 | Project lead 31 | 32 | 33 | 34 | 35 | 3.2.1 36 | 37 | 38 | ${basedir} 39 | 40 | 41 | 42 | default 43 | 44 | true 45 | 46 | 47 | spring-boot-mybatis-parent 48 | spring-boot-mybatis 49 | spring-boot-mybatis-sample 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /spring-boot-mybatis-parent/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.springframework.boot 6 | spring-boot-starter-parent 7 | 1.3.0.M5 8 | 9 | 10 | 11 | org.springframework.boot.mybatis 12 | spring-boot-mybatis-parent 13 | 1.3.0.BUILD-SNAPSHOT 14 | pom 15 | spring-boot-mybatis-parent 16 | 17 | Spring Boot Mybatis 18 | http://github.io/sbcoba/spring-boot-mybatis/ 19 | 20 | sbcoba 21 | http://sbcoba.tistoty.com 22 | 23 | 24 | 25 | UTF-8 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot.mybatis 32 | spring-boot-mybatis 33 | ${project.version} 34 | 35 | 36 | org.mybatis 37 | mybatis 38 | 3.3.0 39 | 40 | 41 | org.mybatis 42 | mybatis-spring 43 | 1.2.3 44 | 45 | 46 | 47 | 48 | 49 | 50 | spring-snapshots 51 | Spring Snapshots 52 | https://repo.spring.io/snapshot 53 | 54 | true 55 | 56 | 57 | 58 | spring-milestones 59 | Spring Milestones 60 | https://repo.spring.io/milestone 61 | 62 | false 63 | 64 | 65 | 66 | 67 | 68 | spring-snapshots 69 | Spring Snapshots 70 | https://repo.spring.io/snapshot 71 | 72 | true 73 | 74 | 75 | 76 | spring-milestones 77 | Spring Milestones 78 | https://repo.spring.io/milestone 79 | 80 | false 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.springframework.boot.mybatis 6 | spring-boot-mybatis-parent 7 | 1.3.0.BUILD-SNAPSHOT 8 | ../spring-boot-mybatis-parent 9 | 10 | 11 | org.springframework.boot.mybatis 12 | spring-boot-mybatis-sample 13 | pom 14 | spring-boot-mybatis-sample 15 | 16 | Spring Boot Mybatis 17 | http://github.io/sbcoba/spring-boot-mybatis/ 18 | 19 | sbcoba 20 | http://sbcoba.tistoty.com 21 | 22 | 23 | 24 | spring-boot-mybatis-sample-default 25 | spring-boot-mybatis-sample-simple 26 | spring-boot-mybatis-sample-xml 27 | spring-boot-mybatis-sample-hybrid 28 | spring-boot-mybatis-sample-groovy 29 | 30 | 31 | 32 | UTF-8 33 | 34 | 35 | 36 | 37 | spring-snapshots 38 | Spring Snapshots 39 | https://repo.spring.io/snapshot 40 | 41 | true 42 | 43 | 44 | 45 | spring-milestones 46 | Spring Milestones 47 | https://repo.spring.io/milestone 48 | 49 | false 50 | 51 | 52 | 53 | 54 | 55 | spring-snapshots 56 | Spring Snapshots 57 | https://repo.spring.io/snapshot 58 | 59 | true 60 | 61 | 62 | 63 | spring-milestones 64 | Spring Milestones 65 | https://repo.spring.io/milestone 66 | 67 | false 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot.mybatis 7 | spring-boot-mybatis-sample 8 | 1.3.0.BUILD-SNAPSHOT 9 | ../ 10 | 11 | 12 | spring-boot-mybatis-sample-default 13 | war 14 | 15 | spring-boot-mybatis-sample-default 16 | Spring Boot MyBatis sample 17 | 18 | 19 | ${basedir}/../.. 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot.mybatis 30 | spring-boot-mybatis 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | runtime 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-surefire-plugin 55 | 56 | false 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/java/sample/springboot/mybatis/defaultconfig/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SampleApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(SampleApplication.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/java/sample/springboot/mybatis/defaultconfig/service/Member.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig.service; 2 | 3 | /** 4 | * Sample DTO 5 | * 6 | * @author sbcoba 7 | * 8 | */ 9 | public class Member { 10 | private Long id; 11 | private String name; 12 | private String phone; 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | public void setId(Long id) { 18 | this.id = id; 19 | } 20 | public String getName() { 21 | return name; 22 | } 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public String getPhone() { 27 | return phone; 28 | } 29 | public void setPhone(String phone) { 30 | this.phone = phone; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Member [id=" + id + ", name=" + name + ", phone=" + phone + "]"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/java/sample/springboot/mybatis/defaultconfig/service/MemberMapper.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig.service; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Select; 6 | import org.springframework.boot.mybatis.autoconfigure.Mapper; 7 | 8 | @Mapper 9 | public interface MemberMapper { 10 | @Select("SELECT ID, NAME, PHONE FROM MEMBER") 11 | List findAll(); 12 | 13 | @Select("SELECT ID, NAME, PHONE FROM MEMBER WHERE ID = #{id}") 14 | Member findById(Long id); 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/java/sample/springboot/mybatis/defaultconfig/web/MemberController.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import sample.springboot.mybatis.defaultconfig.service.Member; 11 | import sample.springboot.mybatis.defaultconfig.service.MemberMapper; 12 | 13 | @RestController 14 | public class MemberController { 15 | 16 | @Autowired 17 | MemberMapper memberMapper; 18 | 19 | @RequestMapping("members") 20 | public List members() { 21 | return memberMapper.findAll(); 22 | } 23 | 24 | @RequestMapping("member/{id}") 25 | public Member member(@PathVariable("id") Long id) { 26 | return memberMapper.findById(id); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | h2: 3 | console: 4 | path: /console 5 | enabled: true 6 | 7 | logging: 8 | level: 9 | org.springframework.boot: DEBUG 10 | sample.springboot.mybatis: DEBUG 11 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (1, 'xml', '010-9912-1234'); 2 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (2, 'annotation', '010-2333-4321'); 3 | 4 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (1, 'Team1', 'Test1 group'); 5 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (2, 'Team2', 'Test2 group'); 6 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE MEMBER ( 2 | ID INT PRIMARY KEY, 3 | NAME VARCHAR(50), 4 | PHONE VARCHAR(20)); 5 | 6 | CREATE TABLE MEMBER_GROUP ( 7 | ID INT PRIMARY KEY, 8 | NAME VARCHAR(50), 9 | REMARK VARCHAR(20)); -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-default/src/test/java/sample/springboot/mybatis/defaultconfig/SpringDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 8 | 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.boot.test.SpringApplicationConfiguration; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 20 | import org.springframework.test.context.web.WebAppConfiguration; 21 | import org.springframework.test.web.servlet.MockMvc; 22 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 23 | import org.springframework.web.context.WebApplicationContext; 24 | 25 | import sample.springboot.mybatis.defaultconfig.service.Member; 26 | import sample.springboot.mybatis.defaultconfig.service.MemberMapper; 27 | import sample.springboot.mybatis.defaultconfig.web.MemberController; 28 | 29 | @RunWith(SpringJUnit4ClassRunner.class) 30 | @SpringApplicationConfiguration(classes = SampleApplication.class) 31 | @WebAppConfiguration 32 | public class SpringDemoApplicationTests { 33 | private static final Logger log = LoggerFactory.getLogger(SpringDemoApplicationTests.class); 34 | 35 | @Autowired 36 | WebApplicationContext wac; 37 | 38 | MockMvc mockMvc; 39 | 40 | @Before 41 | public void init() { 42 | mockMvc = MockMvcBuilders.webAppContextSetup(wac) 43 | .alwaysDo(print()) 44 | .build(); 45 | } 46 | 47 | @Test 48 | public void controllerMemberTest() throws Exception { 49 | mockMvc.perform( 50 | get("/member/{id}", 1).accept(MediaType.APPLICATION_JSON)) 51 | .andExpect(handler().handlerType(MemberController.class)) 52 | .andExpect(handler().methodName("member")) 53 | .andExpect(status().isOk()) 54 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 55 | ); 56 | } 57 | 58 | @Autowired 59 | MemberMapper memberMapper; 60 | 61 | @Test 62 | public void findAllTest() { 63 | List members = memberMapper.findAll(); 64 | log.debug("members : {}", members); 65 | assertThat(members.size(), is(2)); 66 | } 67 | 68 | @Test 69 | public void findByIdTest() { 70 | Member member1 = memberMapper.findById(1L); 71 | log.debug("member1 : {}", member1); 72 | assertThat(member1.getId(), is(1L)); 73 | assertThat(member1.getName(), is("xml")); 74 | assertThat(member1.getPhone(), is("010-9912-1234")); 75 | 76 | Member member2 = memberMapper.findById(2L); 77 | log.debug("member2 : {}", member2); 78 | assertThat(member2.getId(), is(2L)); 79 | assertThat(member2.getName(), is("annotation")); 80 | assertThat(member2.getPhone(), is("010-2333-4321")); 81 | 82 | Member member3 = memberMapper.findById(3L); 83 | log.debug("member3 : {}", member3); 84 | assertThat(member3, is(nullValue())); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot.mybatis 7 | spring-boot-mybatis-sample 8 | 1.3.0.BUILD-SNAPSHOT 9 | ../ 10 | 11 | 12 | spring-boot-mybatis-sample-groovy 13 | war 14 | 15 | spring-boot-mybatis-sample-groovy 16 | Spring Boot MyBatis sample 17 | 18 | 19 | ${basedir}/../.. 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot.mybatis 30 | spring-boot-mybatis 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | runtime 37 | 38 | 39 | 40 | org.codehaus.groovy 41 | groovy-all 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | src/main/java 55 | 56 | **/*.java 57 | **/*.groovy 58 | 59 | 60 | 61 | src/main/resources 62 | 63 | 64 | 65 | 66 | 67 | org.springframework.boot 68 | spring-boot-maven-plugin 69 | 70 | 71 | org.apache.maven.plugins 72 | maven-surefire-plugin 73 | 74 | false 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-compiler-plugin 80 | 81 | groovy-eclipse-compiler 82 | true 83 | false 84 | false 85 | 86 | 87 | 88 | org.codehaus.groovy 89 | groovy-eclipse-compiler 90 | 2.9.1-01 91 | 92 | 93 | org.codehaus.groovy 94 | groovy-eclipse-batch 95 | 2.3.7-01 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/java/sample/springboot/mybatis/groovymapper/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.groovymapper; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | import sample.springboot.mybatis.groovymapper.SampleApplication; 7 | 8 | @SpringBootApplication 9 | public class SampleApplication { 10 | public static void main(String[] args) { 11 | SpringApplication.run(SampleApplication.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/java/sample/springboot/mybatis/groovymapper/service/Member.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.groovymapper.service; 2 | 3 | /** 4 | * Sample DTO 5 | * 6 | * @author sbcoba 7 | * 8 | */ 9 | public class Member { 10 | private Long id; 11 | private String name; 12 | private String phone; 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | public void setId(Long id) { 18 | this.id = id; 19 | } 20 | public String getName() { 21 | return name; 22 | } 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public String getPhone() { 27 | return phone; 28 | } 29 | public void setPhone(String phone) { 30 | this.phone = phone; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Member [id=" + id + ", name=" + name + ", phone=" + phone + "]"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/java/sample/springboot/mybatis/groovymapper/service/MemberMapper.groovy: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.groovymapper.service; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Select; 6 | import org.springframework.boot.mybatis.autoconfigure.Mapper; 7 | 8 | @Mapper 9 | public interface MemberMapper { 10 | 11 | @Select(""" 12 | SELECT ID, 13 | NAME, 14 | PHONE 15 | FROM MEMBER 16 | """) 17 | List findAll(); 18 | 19 | @Select(""" 20 | SELECT ID, 21 | NAME, 22 | PHONE 23 | FROM MEMBER 24 | WHERE ID = #{id} 25 | """) 26 | Member findById(Long id); 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/java/sample/springboot/mybatis/groovymapper/web/MemberController.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.groovymapper.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import sample.springboot.mybatis.groovymapper.service.Member; 11 | import sample.springboot.mybatis.groovymapper.service.MemberMapper; 12 | 13 | @RestController 14 | public class MemberController { 15 | 16 | @Autowired 17 | MemberMapper memberMapper; 18 | 19 | @RequestMapping("members") 20 | public List members() { 21 | return memberMapper.findAll(); 22 | } 23 | 24 | @RequestMapping("member/{id}") 25 | public Member member(@PathVariable("id") Long id) { 26 | return memberMapper.findById(id); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | h2: 3 | console: 4 | path: /console 5 | enabled: true 6 | 7 | logging: 8 | level: 9 | org.springframework.boot: DEBUG 10 | sample.springboot.mybatis: DEBUG 11 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (1, 'xml', '010-9912-1234'); 2 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (2, 'annotation', '010-2333-4321'); 3 | 4 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (1, 'Team1', 'Test1 group'); 5 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (2, 'Team2', 'Test2 group'); 6 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE MEMBER ( 2 | ID INT PRIMARY KEY, 3 | NAME VARCHAR(50), 4 | PHONE VARCHAR(20)); 5 | 6 | CREATE TABLE MEMBER_GROUP ( 7 | ID INT PRIMARY KEY, 8 | NAME VARCHAR(50), 9 | REMARK VARCHAR(20)); -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-groovy/src/test/java/sample/springboot/mybatis/groovymapper/SpringDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.groovymapper; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 8 | 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.boot.test.SpringApplicationConfiguration; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 20 | import org.springframework.test.context.web.WebAppConfiguration; 21 | import org.springframework.test.web.servlet.MockMvc; 22 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 23 | import org.springframework.web.context.WebApplicationContext; 24 | 25 | import sample.springboot.mybatis.groovymapper.SampleApplication; 26 | import sample.springboot.mybatis.groovymapper.SpringDemoApplicationTests; 27 | import sample.springboot.mybatis.groovymapper.service.Member; 28 | import sample.springboot.mybatis.groovymapper.service.MemberMapper; 29 | import sample.springboot.mybatis.groovymapper.web.MemberController; 30 | 31 | @RunWith(SpringJUnit4ClassRunner.class) 32 | @SpringApplicationConfiguration(classes = SampleApplication.class) 33 | @WebAppConfiguration 34 | public class SpringDemoApplicationTests { 35 | private static final Logger log = LoggerFactory.getLogger(SpringDemoApplicationTests.class); 36 | 37 | @Autowired 38 | WebApplicationContext wac; 39 | 40 | MockMvc mockMvc; 41 | 42 | @Before 43 | public void init() { 44 | mockMvc = MockMvcBuilders.webAppContextSetup(wac) 45 | .alwaysDo(print()) 46 | .build(); 47 | } 48 | 49 | @Test 50 | public void controllerMemberTest() throws Exception { 51 | mockMvc.perform( 52 | get("/member/{id}", 1).accept(MediaType.APPLICATION_JSON)) 53 | .andExpect(handler().handlerType(MemberController.class)) 54 | .andExpect(handler().methodName("member")) 55 | .andExpect(status().isOk()) 56 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 57 | ); 58 | } 59 | 60 | @Autowired 61 | MemberMapper memberMapper; 62 | 63 | @Test 64 | public void findAllTest() { 65 | List members = memberMapper.findAll(); 66 | log.debug("members : {}", members); 67 | assertThat(members.size(), is(2)); 68 | } 69 | 70 | @Test 71 | public void findByIdTest() { 72 | Member member1 = memberMapper.findById(1L); 73 | log.debug("member1 : {}", member1); 74 | assertThat(member1.getId(), is(1L)); 75 | assertThat(member1.getName(), is("xml")); 76 | assertThat(member1.getPhone(), is("010-9912-1234")); 77 | 78 | Member member2 = memberMapper.findById(2L); 79 | log.debug("member2 : {}", member2); 80 | assertThat(member2.getId(), is(2L)); 81 | assertThat(member2.getName(), is("annotation")); 82 | assertThat(member2.getPhone(), is("010-2333-4321")); 83 | 84 | Member member3 = memberMapper.findById(3L); 85 | log.debug("member3 : {}", member3); 86 | assertThat(member3, is(nullValue())); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot.mybatis 7 | spring-boot-mybatis-sample 8 | 1.3.0.BUILD-SNAPSHOT 9 | ../ 10 | 11 | 12 | spring-boot-mybatis-sample-hybrid 13 | jar 14 | 15 | spring-boot-mybatis-sample-hybrid 16 | Spring Boot MyBatis sample 17 | 18 | 19 | ${basedir}/../.. 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot.mybatis 30 | spring-boot-mybatis 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | runtime 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | src/main/java 50 | 51 | **/*.java 52 | **/*.xml 53 | 54 | 55 | 56 | src/main/resources 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot-maven-plugin 63 | 64 | 65 | org.apache.maven.plugins 66 | maven-surefire-plugin 67 | 68 | false 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/java/sample/springboot/mybatis/hybrid/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.hybrid; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SampleApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(SampleApplication.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/java/sample/springboot/mybatis/hybrid/service/Member.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.hybrid.service; 2 | 3 | /** 4 | * Sample DTO 5 | * 6 | * @author sbcoba 7 | * 8 | */ 9 | public class Member { 10 | private Long id; 11 | private String name; 12 | private String phone; 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | public void setId(Long id) { 18 | this.id = id; 19 | } 20 | public String getName() { 21 | return name; 22 | } 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public String getPhone() { 27 | return phone; 28 | } 29 | public void setPhone(String phone) { 30 | this.phone = phone; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Member [id=" + id + ", name=" + name + ", phone=" + phone + "]"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/java/sample/springboot/mybatis/hybrid/service/MemberAnnotationMapper.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.hybrid.service; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Select; 6 | import org.springframework.boot.mybatis.autoconfigure.Mapper; 7 | 8 | @Mapper 9 | public interface MemberAnnotationMapper { 10 | @Select("SELECT id, name, phone FROM MEMBER where name = 'annotation'") 11 | List findAll(); 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/java/sample/springboot/mybatis/hybrid/service/MemberXmlMapper.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.hybrid.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.boot.mybatis.autoconfigure.Mapper; 6 | 7 | /** 8 | * mapper.xml check 9 | * @author sbcoba 10 | * 11 | */ 12 | @Mapper 13 | public interface MemberXmlMapper { 14 | List findAll(); 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/java/sample/springboot/mybatis/hybrid/service/mapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/java/sample/springboot/mybatis/hybrid/web/MemberController.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.hybrid.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import sample.springboot.mybatis.hybrid.service.Member; 10 | import sample.springboot.mybatis.hybrid.service.MemberAnnotationMapper; 11 | import sample.springboot.mybatis.hybrid.service.MemberXmlMapper; 12 | 13 | @RestController 14 | public class MemberController { 15 | 16 | @Autowired 17 | MemberAnnotationMapper memberAnnotationMapper; 18 | 19 | @Autowired 20 | MemberXmlMapper memberXmlMapper; 21 | 22 | @RequestMapping(value="members") 23 | public List members() { 24 | return memberAnnotationMapper.findAll(); 25 | } 26 | 27 | @RequestMapping(value="members", params="mapper=anno") 28 | public List annoMembers() { 29 | return memberAnnotationMapper.findAll(); 30 | } 31 | 32 | @RequestMapping(value="members", params="mapper=xml") 33 | public List xmlMembers() { 34 | return memberXmlMapper.findAll(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | mybatis: 3 | # default value 4 | # annotationClass: org.springframework.boot.mybatis.autoconfigure.Mapper 5 | # configLocation: "mybatis-configuration.xml" 6 | mapper-locations: "classpath*:**/mapper.xml" 7 | 8 | # If the 'spring.mybatis.configLocation' property is used when the 'spring.mybatis.configurationProperties' property is ignored. 9 | configuration-properties: 10 | aggressiveLazyLoading: false 11 | lazyLoadingEnabled: true 12 | lazyLoadTriggerMethods: "" 13 | mapUnderscoreToCamelCase: true 14 | 15 | h2: 16 | console: 17 | path: /console 18 | enabled: true 19 | 20 | logging: 21 | level: 22 | org.springframework.boot: DEBUG 23 | org.hibernate: ERROR -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (1, 'xml', '010-9912-1234'); 2 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (2, 'annotation', '010-2333-4321'); 3 | 4 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (1, 'Team1', 'Test1 group'); 5 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (2, 'Team2', 'Test2 group'); 6 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/resources/mybatis-configuration.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE MEMBER ( 2 | ID INT PRIMARY KEY, 3 | NAME VARCHAR(50), 4 | PHONE VARCHAR(20)); 5 | 6 | CREATE TABLE MEMBER_GROUP ( 7 | ID INT PRIMARY KEY, 8 | NAME VARCHAR(50), 9 | REMARK VARCHAR(20)); -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-hybrid/src/test/java/sample/springboot/mybatis/hybrid/SpringDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.hybrid; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 8 | 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.boot.test.SpringApplicationConfiguration; 19 | import org.springframework.http.MediaType; 20 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 21 | import org.springframework.test.context.web.WebAppConfiguration; 22 | import org.springframework.test.web.servlet.MockMvc; 23 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 24 | import org.springframework.web.context.WebApplicationContext; 25 | 26 | import sample.springboot.mybatis.hybrid.service.Member; 27 | import sample.springboot.mybatis.hybrid.service.MemberAnnotationMapper; 28 | import sample.springboot.mybatis.hybrid.service.MemberXmlMapper; 29 | import sample.springboot.mybatis.hybrid.web.MemberController; 30 | 31 | @RunWith(SpringJUnit4ClassRunner.class) 32 | @SpringApplicationConfiguration(classes = SampleApplication.class) 33 | @WebAppConfiguration 34 | public class SpringDemoApplicationTests { 35 | private static final Logger log = LoggerFactory.getLogger(SpringDemoApplicationTests.class); 36 | 37 | @Autowired 38 | WebApplicationContext wac; 39 | 40 | MockMvc mockMvc; 41 | 42 | @Before 43 | public void init() { 44 | mockMvc = MockMvcBuilders.webAppContextSetup(wac) 45 | .alwaysDo(print()) 46 | .build(); 47 | } 48 | 49 | @Test 50 | public void controllerMembersAnnoTest() throws Exception { 51 | mockMvc.perform( 52 | get("/members").param("mapper", "anno").accept(MediaType.APPLICATION_JSON)) 53 | .andExpect(handler().handlerType(MemberController.class)) 54 | .andExpect(handler().methodName("annoMembers")) 55 | .andExpect(status().isOk()) 56 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 57 | ); 58 | } 59 | 60 | @Test 61 | public void controllerMemberXmlTest() throws Exception { 62 | mockMvc.perform( 63 | get("/members").param("mapper", "xml").accept(MediaType.APPLICATION_JSON)) 64 | .andExpect(handler().handlerType(MemberController.class)) 65 | .andExpect(handler().methodName("xmlMembers")) 66 | .andExpect(status().isOk()) 67 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 68 | ); 69 | } 70 | 71 | @Autowired 72 | MemberXmlMapper memberXmlMapper; 73 | 74 | @Test 75 | public void xmlFindAllTest() { 76 | List members = memberXmlMapper.findAll(); 77 | log.debug("Members by xml mapper : {}", members); 78 | assertThat(members.size(), is(1)); 79 | assertThat(members.get(0).getId(), is(1L)); 80 | assertThat(members.get(0).getName(), is("xml")); 81 | assertThat(members.get(0).getPhone(), is("010-9912-1234")); 82 | } 83 | 84 | @Autowired 85 | MemberAnnotationMapper memberAnnotationMapper; 86 | 87 | @Test 88 | public void annotationFindAllTest() { 89 | List members = memberAnnotationMapper.findAll(); 90 | log.debug("members by annotation mapper : {}", members); 91 | assertThat(members.size(), is(1)); 92 | assertThat(members.get(0).getId(), is(2L)); 93 | assertThat(members.get(0).getName(), is("annotation")); 94 | assertThat(members.get(0).getPhone(), is("010-2333-4321")); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot.mybatis 7 | spring-boot-mybatis-sample 8 | 1.3.0.BUILD-SNAPSHOT 9 | ../ 10 | 11 | 12 | spring-boot-mybatis-sample-simple 13 | war 14 | 15 | spring-boot-mybatis-sample-simple 16 | Spring Boot MyBatis sample 17 | 18 | 19 | ${basedir}/../.. 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot.mybatis 30 | spring-boot-mybatis 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | runtime 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-maven-plugin 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-surefire-plugin 55 | 56 | false 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/src/main/java/sample/springboot/mybatis/defaultconfig/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.apache.ibatis.annotations.Select; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.SpringApplication; 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; 10 | import org.springframework.boot.mybatis.autoconfigure.Mapper; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | @SpringBootApplication 16 | public class SampleApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(SampleApplication.class, args); 20 | } 21 | 22 | @RestController 23 | public static class MemberController { 24 | 25 | @Autowired 26 | MemberMapper memberMapper; 27 | 28 | @RequestMapping("members") 29 | public List> members() { 30 | return memberMapper.findAll(); 31 | } 32 | 33 | @RequestMapping("member/{id}") 34 | public Map member(@PathVariable("id") Long id) { 35 | return memberMapper.findById(id); 36 | } 37 | } 38 | 39 | @Mapper 40 | interface MemberMapper { 41 | @Select("SELECT ID, NAME, PHONE FROM MEMBER") 42 | List> findAll(); 43 | 44 | @Select("SELECT ID, NAME, PHONE FROM MEMBER WHERE ID = #{id}") 45 | Map findById(Long id); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | h2: 3 | console: 4 | path: /console 5 | enabled: true 6 | 7 | logging: 8 | level: 9 | org.springframework.boot: DEBUG 10 | sample.springboot.mybatis: DEBUG 11 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (1, 'xml', '010-9912-1234'); 2 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (2, 'annotation', '010-2333-4321'); 3 | 4 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (1, 'Team1', 'Test1 group'); 5 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (2, 'Team2', 'Test2 group'); 6 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE MEMBER ( 2 | ID INT PRIMARY KEY, 3 | NAME VARCHAR(50), 4 | PHONE VARCHAR(20)); 5 | 6 | CREATE TABLE MEMBER_GROUP ( 7 | ID INT PRIMARY KEY, 8 | NAME VARCHAR(50), 9 | REMARK VARCHAR(20)); -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-simple/src/test/java/sample/springboot/mybatis/defaultconfig/SpringDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.defaultconfig; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 8 | 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.boot.test.SpringApplicationConfiguration; 19 | import org.springframework.http.MediaType; 20 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 21 | import org.springframework.test.context.web.WebAppConfiguration; 22 | import org.springframework.test.web.servlet.MockMvc; 23 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 24 | import org.springframework.web.context.WebApplicationContext; 25 | 26 | import sample.springboot.mybatis.defaultconfig.SampleApplication.MemberController; 27 | import sample.springboot.mybatis.defaultconfig.SampleApplication.MemberMapper; 28 | 29 | @RunWith(SpringJUnit4ClassRunner.class) 30 | @SpringApplicationConfiguration(classes = SampleApplication.class) 31 | @WebAppConfiguration 32 | public class SpringDemoApplicationTests { 33 | private static final Logger log = LoggerFactory.getLogger(SpringDemoApplicationTests.class); 34 | 35 | @Autowired 36 | WebApplicationContext wac; 37 | 38 | MockMvc mockMvc; 39 | 40 | @Before 41 | public void init() { 42 | mockMvc = MockMvcBuilders.webAppContextSetup(wac) 43 | .alwaysDo(print()) 44 | .build(); 45 | } 46 | 47 | @Test 48 | public void controllerMemberTest() throws Exception { 49 | mockMvc.perform( 50 | get("/member/{id}", 1).accept(MediaType.APPLICATION_JSON)) 51 | .andExpect(handler().handlerType(MemberController.class)) 52 | .andExpect(handler().methodName("member")) 53 | .andExpect(status().isOk()) 54 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 55 | ); 56 | } 57 | 58 | @Test 59 | public void controllerMembersTest() throws Exception { 60 | mockMvc.perform( 61 | get("/members").accept(MediaType.APPLICATION_JSON)) 62 | .andExpect(handler().handlerType(MemberController.class)) 63 | .andExpect(handler().methodName("members")) 64 | .andExpect(status().isOk()) 65 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 66 | ); 67 | } 68 | 69 | @Autowired 70 | MemberMapper memberMapper; 71 | 72 | @Test 73 | public void findAllTest() { 74 | List> members = memberMapper.findAll(); 75 | log.debug("members : {}", members); 76 | assertThat(members.size(), is(2)); 77 | } 78 | 79 | @Test 80 | public void findByIdTest() { 81 | Map member1 = memberMapper.findById(1L); 82 | log.debug("member1 : {}", member1); 83 | assertThat((Integer) member1.get("ID"), is(1)); 84 | assertThat((String) member1.get("NAME"), is("xml")); 85 | assertThat((String) member1.get("PHONE"), is("010-9912-1234")); 86 | 87 | Map member2 = memberMapper.findById(2L); 88 | log.debug("member2 : {}", member2); 89 | assertThat((Integer) member2.get("ID"), is(2)); 90 | assertThat((String) member2.get("NAME"), is("annotation")); 91 | assertThat((String) member2.get("PHONE"), is("010-2333-4321")); 92 | 93 | Map member3 = memberMapper.findById(3L); 94 | log.debug("member3 : {}", member3); 95 | assertThat(member3, is(nullValue())); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot.mybatis 7 | spring-boot-mybatis-sample 8 | 1.3.0.BUILD-SNAPSHOT 9 | ../ 10 | 11 | 12 | spring-boot-mybatis-sample-xml 13 | war 14 | 15 | spring-boot-mybatis-sample-xml 16 | Spring Boot MyBatis xml sample 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.springframework.boot.mybatis 26 | spring-boot-mybatis 27 | 28 | 29 | 30 | com.h2database 31 | h2 32 | runtime 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | src/main/java 46 | 47 | **/*.java 48 | **/*.xml 49 | 50 | 51 | 52 | src/main/resources 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-surefire-plugin 63 | 64 | false 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/java/sample/springboot/mybatis/xmlconfig/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.xmlconfig; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SampleApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(SampleApplication.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/java/sample/springboot/mybatis/xmlconfig/service/Member.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.xmlconfig.service; 2 | 3 | /** 4 | * Sample DTO 5 | * 6 | * @author sbcoba 7 | * 8 | */ 9 | public class Member { 10 | private Long id; 11 | private String name; 12 | private String phone; 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | public void setId(Long id) { 18 | this.id = id; 19 | } 20 | public String getName() { 21 | return name; 22 | } 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public String getPhone() { 27 | return phone; 28 | } 29 | public void setPhone(String phone) { 30 | this.phone = phone; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Member [id=" + id + ", name=" + name + ", phone=" + phone + "]"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/java/sample/springboot/mybatis/xmlconfig/service/MemberMapper.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.xmlconfig.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.boot.mybatis.autoconfigure.Mapper; 6 | 7 | @Mapper 8 | public interface MemberMapper { 9 | List findAll(); 10 | Member findById(Long id); 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/java/sample/springboot/mybatis/xmlconfig/service/mapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/java/sample/springboot/mybatis/xmlconfig/web/MemberController.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.xmlconfig.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import sample.springboot.mybatis.xmlconfig.service.Member; 11 | import sample.springboot.mybatis.xmlconfig.service.MemberMapper; 12 | 13 | @RestController 14 | public class MemberController { 15 | 16 | @Autowired 17 | MemberMapper memberMapper; 18 | 19 | @RequestMapping("members") 20 | public List members() { 21 | return memberMapper.findAll(); 22 | } 23 | 24 | @RequestMapping("member/{id}") 25 | public Member member(@PathVariable("id") Long id) { 26 | return memberMapper.findById(id); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | mybatis: 3 | mapper-locations: "classpath*:**/mapper.xml" 4 | configuration-properties: 5 | aggressiveLazyLoading: false 6 | lazyLoadingEnabled: true 7 | lazyLoadTriggerMethods: "" 8 | mapUnderscoreToCamelCase: true 9 | h2: 10 | console: 11 | path: /console 12 | enabled: true 13 | 14 | logging: 15 | level: 16 | org.springframework.boot: DEBUG 17 | sample.springboot.mybatis: DEBUG 18 | 19 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (1, 'xml', '010-9912-1234'); 2 | INSERT INTO MEMBER (ID, NAME, PHONE) VALUES (2, 'annotation', '010-2333-4321'); 3 | 4 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (1, 'Team1', 'Test1 group'); 5 | INSERT INTO MEMBER_GROUP (ID, NAME, REMARK) VALUES (2, 'Team2', 'Test2 group'); 6 | -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE MEMBER ( 2 | ID INT PRIMARY KEY, 3 | NAME VARCHAR(50), 4 | PHONE VARCHAR(20)); 5 | 6 | CREATE TABLE MEMBER_GROUP ( 7 | ID INT PRIMARY KEY, 8 | NAME VARCHAR(50), 9 | REMARK VARCHAR(20)); -------------------------------------------------------------------------------- /spring-boot-mybatis-sample/spring-boot-mybatis-sample-xml/src/test/java/sample/springboot/mybatis/xmlconfig/SpringDemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package sample.springboot.mybatis.xmlconfig; 2 | 3 | import static org.hamcrest.Matchers.*; 4 | import static org.junit.Assert.*; 5 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 6 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 7 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 8 | 9 | import java.util.List; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.boot.test.SpringApplicationConfiguration; 18 | import org.springframework.http.MediaType; 19 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 20 | import org.springframework.test.context.web.WebAppConfiguration; 21 | import org.springframework.test.web.servlet.MockMvc; 22 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 23 | import org.springframework.web.context.WebApplicationContext; 24 | 25 | import sample.springboot.mybatis.xmlconfig.SampleApplication; 26 | import sample.springboot.mybatis.xmlconfig.service.Member; 27 | import sample.springboot.mybatis.xmlconfig.service.MemberMapper; 28 | import sample.springboot.mybatis.xmlconfig.web.MemberController; 29 | 30 | @RunWith(SpringJUnit4ClassRunner.class) 31 | @SpringApplicationConfiguration(classes = SampleApplication.class) 32 | @WebAppConfiguration 33 | public class SpringDemoApplicationTests { 34 | private static final Logger log = LoggerFactory.getLogger(SpringDemoApplicationTests.class); 35 | 36 | @Autowired 37 | WebApplicationContext wac; 38 | 39 | MockMvc mockMvc; 40 | 41 | @Before 42 | public void init() { 43 | mockMvc = MockMvcBuilders.webAppContextSetup(wac) 44 | .alwaysDo(print()) 45 | .build(); 46 | } 47 | 48 | @Test 49 | public void controllerMemberTest() throws Exception { 50 | mockMvc.perform( 51 | get("/member/{id}", 1).accept(MediaType.APPLICATION_JSON)) 52 | .andExpect(handler().handlerType(MemberController.class)) 53 | .andExpect(handler().methodName("member")) 54 | .andExpect(status().isOk()) 55 | .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON) 56 | ); 57 | } 58 | 59 | @Autowired 60 | MemberMapper memberMapper; 61 | 62 | @Test 63 | public void findAllTest() { 64 | List members = memberMapper.findAll(); 65 | log.debug("members : {}", members); 66 | assertThat(members.size(), is(2)); 67 | } 68 | 69 | @Test 70 | public void findByIdTest() { 71 | Member member1 = memberMapper.findById(1L); 72 | log.debug("member1 : {}", member1); 73 | assertThat(member1.getId(), is(1L)); 74 | assertThat(member1.getName(), is("xml")); 75 | assertThat(member1.getPhone(), is("010-9912-1234")); 76 | 77 | Member member2 = memberMapper.findById(2L); 78 | log.debug("member2 : {}", member2); 79 | assertThat(member2.getId(), is(2L)); 80 | assertThat(member2.getName(), is("annotation")); 81 | assertThat(member2.getPhone(), is("010-2333-4321")); 82 | 83 | Member member3 = memberMapper.findById(3L); 84 | log.debug("member3 : {}", member3); 85 | assertThat(member3, is(nullValue())); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /spring-boot-mybatis/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Application Initializers 2 | org.springframework.context.ApplicationContextInitializer=\ 3 | org.springframework.boot.devtools.restart.RestartScopeInitializer 4 | 5 | # Application Listeners 6 | org.springframework.context.ApplicationListener=\ 7 | org.springframework.boot.devtools.restart.RestartApplicationListener 8 | 9 | # Auto Configure 10 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 11 | org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration,\ 12 | org.springframework.boot.devtools.autoconfigure.RemoteDevToolsAutoConfiguration 13 | -------------------------------------------------------------------------------- /spring-boot-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot.mybatis 7 | spring-boot-mybatis-parent 8 | 1.3.0.BUILD-SNAPSHOT 9 | ../spring-boot-mybatis-parent 10 | 11 | 12 | spring-boot-mybatis 13 | jar 14 | 15 | spring-boot-mybatis 16 | http://maven.apache.org 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-jdbc 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-configuration-processor 37 | true 38 | 39 | 40 | 41 | org.mybatis 42 | mybatis 43 | 44 | 45 | 46 | org.mybatis 47 | mybatis-spring 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-test 53 | test 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/Mapper.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Default SqlMapper marker annotation 11 | * @author sbcoba 12 | * 13 | */ 14 | @Target(ElementType.TYPE) 15 | @Retention(RetentionPolicy.RUNTIME) 16 | @Documented 17 | public @interface Mapper { 18 | } -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/MyBatisAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.IOException; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import javax.sql.DataSource; 9 | 10 | import org.apache.ibatis.plugin.Interceptor; 11 | import org.apache.ibatis.session.SqlSessionFactory; 12 | import org.apache.ibatis.type.TypeHandler; 13 | import org.mybatis.spring.SqlSessionFactoryBean; 14 | import org.mybatis.spring.SqlSessionTemplate; 15 | import org.mybatis.spring.annotation.MapperScannerRegistrar; 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 20 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 22 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | import org.springframework.context.annotation.Import; 26 | import org.springframework.core.io.Resource; 27 | import org.springframework.util.StringUtils; 28 | 29 | @EnableConfigurationProperties 30 | @ConditionalOnBean({DataSource.class, MapperScannerRegistrar.class}) 31 | @ConditionalOnProperty(prefix = "spring.mybatis", name = "enabled", havingValue = "true", matchIfMissing = true) 32 | @Import(MyBatisMapperScannerRegistrar.class) 33 | @Configuration 34 | public class MyBatisAutoConfiguration { 35 | private static final Logger log = LoggerFactory.getLogger(MyBatisAutoConfiguration.class); 36 | 37 | @Autowired(required = false) 38 | private List myBatisConfigurers; 39 | 40 | @Autowired 41 | private MyBatisProperties myBatisProperties; 42 | 43 | @Bean 44 | @ConditionalOnMissingBean 45 | public MyBatisProperties myBatisProperties() { 46 | return new MyBatisProperties(); 47 | } 48 | 49 | @Bean 50 | @ConditionalOnMissingBean 51 | public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException { 52 | SqlSessionFactoryBean factoryBean = new SqlSessionFactoryUpdateConfigurationBean(); 53 | factoryBean.setDataSource(dataSource); 54 | { 55 | Resource configLocation = myBatisProperties.getConfigLocation(); 56 | try { 57 | configLocation.getInputStream(); 58 | } catch (FileNotFoundException e) { 59 | log.info(e.getMessage()); 60 | configLocation = null; 61 | } 62 | if (configLocation != null) { 63 | factoryBean.setConfigLocation(configLocation); 64 | } 65 | } 66 | { 67 | Resource[] mapperLocations = myBatisProperties.getMapperLocations(); 68 | if (mapperLocations != null && mapperLocations.length > 0) { 69 | factoryBean.setMapperLocations(mapperLocations); 70 | } 71 | } 72 | { 73 | String typeAliasesPackage = myBatisProperties.getTypeAliasesPackage(); 74 | if (StringUtils.hasText(typeAliasesPackage)) { 75 | factoryBean.setTypeAliasesPackage(typeAliasesPackage); 76 | } 77 | } 78 | 79 | if (myBatisConfigurers != null) { 80 | List> typeHandlers = new ArrayList>(); 81 | List interceptors = new ArrayList(); 82 | for (MyBatisConfigurer myBatisConfigurer : myBatisConfigurers) { 83 | myBatisConfigurer.addTypeHandlers(typeHandlers); 84 | myBatisConfigurer.addPlugins(interceptors); 85 | } 86 | factoryBean.setTypeHandlers(typeHandlers.toArray(new TypeHandler[]{})); 87 | factoryBean.setPlugins(interceptors.toArray(new Interceptor[]{})); 88 | } 89 | 90 | factoryBean.setConfigurationProperties(myBatisProperties.getConfigurationProperties()); 91 | return factoryBean; 92 | } 93 | 94 | @ConditionalOnMissingBean 95 | @Bean(destroyMethod="clearCache") 96 | public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception { 97 | return new SqlSessionTemplate(sqlSessionFactory); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/MyBatisConfigurer.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.plugin.Interceptor; 6 | import org.apache.ibatis.type.TypeHandler; 7 | 8 | public interface MyBatisConfigurer { 9 | void addPlugins(List interceptors); 10 | 11 | void addTypeHandlers(List> typeHandlers); 12 | } -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/MyBatisConfigurerAdapter.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.plugin.Interceptor; 6 | import org.apache.ibatis.type.TypeHandler; 7 | 8 | public class MyBatisConfigurerAdapter implements MyBatisConfigurer { 9 | 10 | @Override 11 | public void addPlugins(List interceptors) { 12 | 13 | } 14 | 15 | @Override 16 | public void addTypeHandlers(List> typeHandlers) { 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/MyBatisMapperScannerRegistrar.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import org.mybatis.spring.mapper.ClassPathMapperScanner; 8 | import org.mybatis.spring.mapper.MapperFactoryBean; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.beans.BeanUtils; 12 | import org.springframework.beans.BeansException; 13 | import org.springframework.beans.factory.BeanCreationException; 14 | import org.springframework.beans.factory.BeanFactory; 15 | import org.springframework.beans.factory.BeanFactoryAware; 16 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; 17 | import org.springframework.beans.factory.support.BeanNameGenerator; 18 | import org.springframework.boot.autoconfigure.AutoConfigurationPackages; 19 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 20 | import org.springframework.boot.bind.PropertiesConfigurationFactory; 21 | import org.springframework.boot.context.properties.ConfigurationProperties; 22 | import org.springframework.context.EnvironmentAware; 23 | import org.springframework.context.ResourceLoaderAware; 24 | import org.springframework.context.annotation.Configuration; 25 | import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; 26 | import org.springframework.core.annotation.AnnotationUtils; 27 | import org.springframework.core.convert.support.DefaultConversionService; 28 | import org.springframework.core.env.ConfigurableEnvironment; 29 | import org.springframework.core.env.Environment; 30 | import org.springframework.core.env.MutablePropertySources; 31 | import org.springframework.core.io.ResourceLoader; 32 | import org.springframework.core.type.AnnotationMetadata; 33 | import org.springframework.util.StringUtils; 34 | import org.springframework.validation.BindException; 35 | 36 | @ConditionalOnMissingBean 37 | @Configuration 38 | public class MyBatisMapperScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware, BeanFactoryAware{ 39 | private static final Logger log = LoggerFactory.getLogger(MyBatisMapperScannerRegistrar.class); 40 | 41 | private ResourceLoader resourceLoader; 42 | 43 | private Environment environment; 44 | 45 | private BeanFactory beanFactory; 46 | 47 | @Override 48 | public void setResourceLoader(ResourceLoader resourceLoader) { 49 | this.resourceLoader = resourceLoader; 50 | } 51 | 52 | @Override 53 | public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { 54 | ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); 55 | if (resourceLoader != null) { 56 | scanner.setResourceLoader(resourceLoader); 57 | } 58 | MyBatisProperties myBatisProperties = getMyBatisProperties(); 59 | log.debug("myBatisProperties : {}", myBatisProperties); 60 | 61 | Class annotationClass = myBatisProperties.getAnnotationClass(); 62 | if (annotationClass != null && !Annotation.class.equals(annotationClass)) { 63 | scanner.setAnnotationClass(annotationClass); 64 | } 65 | 66 | Class markerInterface = myBatisProperties.getMarkerInterface(); 67 | if (markerInterface != null && !Class.class.equals(markerInterface)) { 68 | scanner.setMarkerInterface(markerInterface); 69 | } 70 | 71 | Class generatorClass = myBatisProperties.getNameGenerator(); 72 | if (generatorClass != null && !BeanNameGenerator.class.equals(generatorClass)) { 73 | scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass)); 74 | } 75 | 76 | Class> mapperFactoryBeanClass = myBatisProperties.getFactoryBean(); 77 | if (mapperFactoryBeanClass != null && !MapperFactoryBean.class.equals(mapperFactoryBeanClass)) { 78 | scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass)); 79 | } 80 | 81 | scanner.setSqlSessionTemplateBeanName(myBatisProperties.getSqlSessionTemplateRef()); 82 | scanner.setSqlSessionFactoryBeanName(myBatisProperties.getSqlSessionFactoryRef()); 83 | 84 | List basePackages = new ArrayList(); 85 | String[] propertiesBasePackages = myBatisProperties.getBasePackage(); 86 | if (propertiesBasePackages != null && propertiesBasePackages.length > 0) { 87 | for (String basePackage : propertiesBasePackages) { 88 | if (StringUtils.hasText(basePackage)) { 89 | basePackages.addAll(StringUtils.commaDelimitedListToSet(basePackage)); 90 | } 91 | } 92 | } else { 93 | basePackages.addAll(AutoConfigurationPackages.get(this.beanFactory)); 94 | } 95 | 96 | scanner.registerFilters(); 97 | scanner.doScan(StringUtils.toStringArray(basePackages)); 98 | } 99 | 100 | private MyBatisProperties getMyBatisProperties() { 101 | MyBatisProperties myBatisProperties = new MyBatisProperties(); 102 | PropertiesConfigurationFactory factory = 103 | new PropertiesConfigurationFactory(myBatisProperties); 104 | factory.setConversionService(new DefaultConversionService()); 105 | ConfigurationProperties annotation = 106 | AnnotationUtils.findAnnotation(MyBatisProperties.class, ConfigurationProperties.class); 107 | String prefix = StringUtils.hasText(annotation.value()) ? annotation.value() : annotation.prefix(); 108 | factory.setTargetName(prefix); 109 | 110 | if (environment instanceof ConfigurableEnvironment) { 111 | ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; 112 | MutablePropertySources propertySources = configurableEnvironment.getPropertySources(); 113 | factory.setPropertySources(propertySources); 114 | } 115 | 116 | try { 117 | factory.bindPropertiesToTarget(); 118 | } catch (BindException e) { 119 | throw new BeanCreationException("myBatisProperties", "Could not bind properties to " 120 | + MyBatisProperties.class + " (" + annotation + ")", e); 121 | } 122 | return myBatisProperties; 123 | } 124 | 125 | @Override 126 | public void setEnvironment(Environment environment) { 127 | this.environment = environment; 128 | } 129 | 130 | @Override 131 | public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 132 | this.beanFactory = beanFactory; 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/MyBatisProperties.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.util.Arrays; 5 | import java.util.Properties; 6 | 7 | import org.mybatis.spring.mapper.MapperFactoryBean; 8 | import org.springframework.beans.factory.support.BeanNameGenerator; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.core.io.ClassPathResource; 11 | import org.springframework.core.io.Resource; 12 | 13 | @ConfigurationProperties("spring.mybatis") 14 | public class MyBatisProperties { 15 | private String[] basePackage; 16 | private String sqlSessionFactoryRef; 17 | private String sqlSessionTemplateRef; 18 | private Class> factoryBean; 19 | private Class nameGenerator; 20 | private Class markerInterface; 21 | private Class annotationClass = Mapper.class; 22 | 23 | private Resource configLocation = new ClassPathResource("mybatis-configuration.xml"); 24 | private String typeAliasesPackage; 25 | private Resource[] mapperLocations; 26 | 27 | private Properties configurationProperties = new Properties(); 28 | 29 | public String[] getBasePackage() { 30 | return basePackage; 31 | } 32 | 33 | public void setBasePackage(String[] basePackage) { 34 | this.basePackage = basePackage; 35 | } 36 | 37 | public String getSqlSessionFactoryRef() { 38 | return sqlSessionFactoryRef; 39 | } 40 | 41 | public void setSqlSessionFactoryRef(String sqlSessionFactoryRef) { 42 | this.sqlSessionFactoryRef = sqlSessionFactoryRef; 43 | } 44 | 45 | public String getSqlSessionTemplateRef() { 46 | return sqlSessionTemplateRef; 47 | } 48 | 49 | public void setSqlSessionTemplateRef(String sqlSessionTemplateRef) { 50 | this.sqlSessionTemplateRef = sqlSessionTemplateRef; 51 | } 52 | 53 | public Class> getFactoryBean() { 54 | return factoryBean; 55 | } 56 | 57 | public void setFactoryBean(Class> factoryBean) { 58 | this.factoryBean = factoryBean; 59 | } 60 | 61 | public Class getNameGenerator() { 62 | return nameGenerator; 63 | } 64 | 65 | public void setNameGenerator(Class nameGenerator) { 66 | this.nameGenerator = nameGenerator; 67 | } 68 | 69 | public Class getMarkerInterface() { 70 | return markerInterface; 71 | } 72 | 73 | public void setMarkerInterface(Class markerInterface) { 74 | this.markerInterface = markerInterface; 75 | } 76 | 77 | public Class getAnnotationClass() { 78 | return annotationClass; 79 | } 80 | 81 | public void setAnnotationClass(Class annotationClass) { 82 | this.annotationClass = annotationClass; 83 | } 84 | 85 | public void setConfigLocation(Resource configLocation) { 86 | this.configLocation = configLocation; 87 | } 88 | 89 | public Resource getConfigLocation() { 90 | return this.configLocation; 91 | } 92 | 93 | public String getTypeAliasesPackage() { 94 | return this.typeAliasesPackage; 95 | } 96 | 97 | public void setTypeAliasesPackage(String typeAliasesPackage) { 98 | this.typeAliasesPackage = typeAliasesPackage; 99 | } 100 | 101 | public Resource[] getMapperLocations() { 102 | return mapperLocations; 103 | } 104 | 105 | public void setMapperLocations(Resource[] mapperLocations) { 106 | this.mapperLocations = mapperLocations; 107 | } 108 | 109 | public Properties getConfigurationProperties() { 110 | return configurationProperties; 111 | } 112 | 113 | public void setConfigurationProperties(Properties configurationProperties) { 114 | this.configurationProperties = configurationProperties; 115 | } 116 | 117 | @Override 118 | public String toString() { 119 | return String.format( 120 | "MyBatisProperties [basePackage=%s, sqlSessionFactoryRef=%s, sqlSessionTemplateRef=%s, factoryBean=%s, nameGenerator=%s, markerInterface=%s, annotationClass=%s, configLocation=%s, typeAliasesPackage=%s, mapperLocations=%s, configurationProperties=%s]", 121 | Arrays.toString(basePackage), sqlSessionFactoryRef, sqlSessionTemplateRef, factoryBean, nameGenerator, 122 | markerInterface, annotationClass, configLocation, typeAliasesPackage, Arrays.toString(mapperLocations), 123 | configurationProperties); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/java/org/springframework/boot/mybatis/autoconfigure/SqlSessionFactoryUpdateConfigurationBean.java: -------------------------------------------------------------------------------- 1 | package org.springframework.boot.mybatis.autoconfigure; 2 | 3 | import org.apache.ibatis.logging.Log; 4 | import org.apache.ibatis.logging.LogFactory; 5 | import org.apache.ibatis.session.Configuration; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.mybatis.spring.SqlSessionFactoryBean; 8 | import org.springframework.beans.BeanWrapper; 9 | import org.springframework.beans.BeanWrapperImpl; 10 | 11 | import java.util.Map; 12 | import java.util.Properties; 13 | import java.util.Set; 14 | 15 | /** 16 | * SqlSessionFactory's configuration update 17 | * @author sbcoba 18 | */ 19 | public class SqlSessionFactoryUpdateConfigurationBean extends SqlSessionFactoryBean { 20 | private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryUpdateConfigurationBean.class); 21 | 22 | private Properties configurationProperties; 23 | 24 | @Override 25 | public void setConfigurationProperties(Properties configurationProperties) { 26 | this.configurationProperties = configurationProperties; 27 | } 28 | 29 | @Override 30 | public SqlSessionFactory getObject() throws Exception { 31 | SqlSessionFactory sqlSessionFactory = super.getObject(); 32 | updateConfiguration(sqlSessionFactory.getConfiguration()); 33 | return sqlSessionFactory; 34 | } 35 | 36 | private void updateConfiguration(Configuration configuration) { 37 | if (configurationProperties == null) { 38 | return; 39 | } 40 | 41 | BeanWrapper bw = new BeanWrapperImpl(configuration); 42 | Set> entries = configurationProperties.entrySet(); 43 | for (Map.Entry entry : entries) { 44 | String propertyName = entry.getKey().toString(); 45 | Object propertyValue = entry.getValue(); 46 | try { 47 | bw.setPropertyValue(propertyName, propertyValue); 48 | } catch (Exception e) { 49 | LOGGER.warn(propertyValue + " values are not set in " + propertyName + " property!" + 50 | " (" + e.getMessage() + ")"); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /spring-boot-mybatis/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | org.springframework.boot.mybatis.autoconfigure.MyBatisAutoConfiguration -------------------------------------------------------------------------------- /spring-boot-mybatis/src/test/java/org/sbcoba/mymodule/AppTest.java: -------------------------------------------------------------------------------- 1 | package org.sbcoba.mymodule; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-boot-mybatis/src/test/java/org/sbcoba/mymodule/TestClass.java: -------------------------------------------------------------------------------- 1 | package org.sbcoba.mymodule; 2 | 3 | public class TestClass { 4 | public static void main(String[] args) { 5 | Test test = Test.newInstance(); 6 | Test test1 = Test.newInstance(); 7 | Test test2 = Test.newInstance(); 8 | System.out.println(test1 == test2); 9 | System.out.println(test.cal(10, 20)); 10 | } 11 | static class Test { 12 | private static Test test; 13 | static synchronized Test newInstance() { 14 | if (test == null) { 15 | test = new Test(); 16 | } 17 | return test; 18 | } 19 | private Test() {} 20 | int cal(int a, int b) { 21 | return a + b; 22 | } 23 | } 24 | } 25 | --------------------------------------------------------------------------------