├── .gitignore ├── README.md ├── build.gradle └── src ├── main ├── java │ └── at │ │ └── ebssoftware │ │ └── ebs │ │ └── timesheet │ │ ├── Application.java │ │ ├── configuration │ │ └── TomcatPoolDataSourceProperties.java │ │ ├── model │ │ └── User.java │ │ └── mybatis │ │ └── mapper │ │ └── UserMapper.java └── resources │ ├── application.properties │ ├── at │ └── ebssoftware │ │ └── ebs │ │ └── timesheet │ │ └── mybatis │ │ └── mapper │ │ └── UserMapper.xml │ └── logback.xml └── test └── java └── at └── ebssoftware └── ebs └── timesheet └── ConfigurationTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | /.settings 3 | /.classpath 4 | /.project 5 | /.gradle 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spring-boot-mybatis 2 | =================== 3 | 4 | Test project to show that mybatis @MapperScan causes Spring Boot's property mechanism to fail 5 | 6 | -> Fixed with upgrade to version 1.1.6.BUILD-SNAPSHOT of spring boot. 7 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | maven { url "http://repo.spring.io/libs-snapshot" } 4 | mavenLocal() 5 | } 6 | dependencies { 7 | classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC3") 8 | } 9 | } 10 | 11 | apply plugin: 'java' 12 | apply plugin: 'eclipse' 13 | apply plugin: 'spring-boot' 14 | 15 | jar { 16 | baseName = 'spring-boot-mybatis' 17 | version = '0.1.0' 18 | } 19 | 20 | repositories { 21 | mavenCentral() 22 | maven { url "http://repo.spring.io/libs-snapshot" } 23 | } 24 | 25 | dependencies { 26 | 27 | compile("org.springframework.boot:spring-boot-starter-web:1.1.6.BUILD-SNAPSHOT") 28 | compile("org.springframework.boot:spring-boot-starter-actuator:1.1.6.BUILD-SNAPSHOT") 29 | 30 | compile("org.springframework:spring-jdbc:4.0.0.RELEASE") 31 | 32 | compile("javax.inject:javax.inject:1") 33 | compile("org.hsqldb:hsqldb:2.2.9") 34 | // 35 | compile("org.mybatis:mybatis:3.2.4") 36 | compile("org.mybatis:mybatis-spring:1.2.2") 37 | 38 | compile("org.apache.tomcat:tomcat-jdbc:7.0.50") 39 | 40 | testCompile("junit:junit:4.11") 41 | testCompile("org.springframework:spring-test:4.0.1.RELEASE") 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/at/ebssoftware/ebs/timesheet/Application.java: -------------------------------------------------------------------------------- 1 | package at.ebssoftware.ebs.timesheet; 2 | 3 | import javax.annotation.PreDestroy; 4 | import javax.inject.Inject; 5 | 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.tomcat.jdbc.pool.DataSource; 8 | import org.mybatis.spring.SqlSessionFactoryBean; 9 | import org.mybatis.spring.annotation.MapperScan; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 13 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 14 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 15 | import org.springframework.context.ConfigurableApplicationContext; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Configuration; 18 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 19 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 20 | import org.springframework.transaction.PlatformTransactionManager; 21 | 22 | import at.ebssoftware.ebs.timesheet.configuration.TomcatPoolDataSourceProperties; 23 | 24 | @Configuration 25 | @EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class ) 26 | @EnableConfigurationProperties(TomcatPoolDataSourceProperties.class) 27 | @MapperScan("at.ebssoftware.ebs.timesheet.mybatis.mapper") 28 | public class Application { 29 | 30 | // @Bean 31 | // public UserMapper userMapper() throws Exception { 32 | // return createMapper(UserMapper.class); 33 | // } 34 | // 35 | // private T createMapper(Class type) throws Exception { 36 | // MapperFactoryBean factory = new MapperFactoryBean(); 37 | // factory.setMapperInterface(type); 38 | // factory.setSqlSessionFactory(sqlSessionFactoryBean()); 39 | // return factory.getObject(); 40 | // } 41 | 42 | 43 | @Autowired 44 | private TomcatPoolDataSourceProperties tomcatPoolDataSourceProperties; 45 | 46 | private org.apache.tomcat.jdbc.pool.DataSource pool; 47 | 48 | @Bean(destroyMethod = "close") 49 | public DataSource dataSource() { 50 | 51 | TomcatPoolDataSourceProperties config = tomcatPoolDataSourceProperties; 52 | 53 | this.pool = new org.apache.tomcat.jdbc.pool.DataSource(); 54 | 55 | this.pool.setDriverClassName(config.getDriverClassName()); 56 | this.pool.setUrl(config.getUrl()); 57 | if (config.getUsername() != null) { 58 | this.pool.setUsername(config.getUsername()); 59 | } 60 | if (config.getPassword() != null) { 61 | this.pool.setPassword(config.getPassword()); 62 | } 63 | this.pool.setInitialSize(config.getInitialSize()); 64 | this.pool.setMaxActive(config.getMaxActive()); 65 | this.pool.setMaxIdle(config.getMaxIdle()); 66 | this.pool.setMinIdle(config.getMinIdle()); 67 | this.pool.setTestOnBorrow(config.isTestOnBorrow()); 68 | this.pool.setTestOnReturn(config.isTestOnReturn()); 69 | this.pool.setValidationQuery(config.getValidationQuery()); 70 | return this.pool; 71 | } 72 | 73 | @PreDestroy 74 | public void close() { 75 | if (this.pool != null) { 76 | this.pool.close(); 77 | } 78 | } 79 | 80 | @Bean 81 | public SqlSessionFactory sqlSessionFactoryBean() throws Exception { 82 | 83 | SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); 84 | sqlSessionFactoryBean.setDataSource(dataSource()); 85 | 86 | PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 87 | sqlSessionFactoryBean 88 | .setMapperLocations(resolver.getResources("classpath:at/ebssoftware/ebs/timesheet/mybatis/mapper/*.xml")); 89 | 90 | return sqlSessionFactoryBean.getObject(); 91 | } 92 | 93 | @Bean 94 | public PlatformTransactionManager transactionManager() { 95 | return new DataSourceTransactionManager(dataSource()); 96 | } 97 | 98 | 99 | public static void main(String[] args) { 100 | ConfigurableApplicationContext ctx = SpringApplication.run(new Object[] { Application.class }, args); 101 | 102 | } 103 | 104 | } -------------------------------------------------------------------------------- /src/main/java/at/ebssoftware/ebs/timesheet/configuration/TomcatPoolDataSourceProperties.java: -------------------------------------------------------------------------------- 1 | package at.ebssoftware.ebs.timesheet.configuration; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | @ConfigurationProperties(prefix = "tomcat.datasource", ignoreUnknownFields = false) 6 | public class TomcatPoolDataSourceProperties { 7 | 8 | private String driverClassName; 9 | private String url; 10 | private String username; 11 | private String password; 12 | private int maxActive = 100; 13 | private int maxIdle = 8; 14 | private int minIdle = 8; 15 | private int initialSize = 10; 16 | private String validationQuery; 17 | 18 | public String getDriverClassName() { 19 | return driverClassName; 20 | } 21 | 22 | public void setDriverClassName(String driverClassName) { 23 | this.driverClassName = driverClassName; 24 | } 25 | 26 | public String getUrl() { 27 | return url; 28 | } 29 | 30 | public void setUrl(String url) { 31 | this.url = url; 32 | } 33 | 34 | public String getUsername() { 35 | return username; 36 | } 37 | 38 | public void setUsername(String username) { 39 | this.username = username; 40 | } 41 | 42 | public String getPassword() { 43 | return password; 44 | } 45 | 46 | public void setPassword(String password) { 47 | this.password = password; 48 | } 49 | 50 | public int getMaxActive() { 51 | return maxActive; 52 | } 53 | 54 | public void setMaxActive(int maxActive) { 55 | this.maxActive = maxActive; 56 | } 57 | 58 | public int getMaxIdle() { 59 | return maxIdle; 60 | } 61 | 62 | public void setMaxIdle(int maxIdle) { 63 | this.maxIdle = maxIdle; 64 | } 65 | 66 | public int getMinIdle() { 67 | return minIdle; 68 | } 69 | 70 | public void setMinIdle(int minIdle) { 71 | this.minIdle = minIdle; 72 | } 73 | 74 | public int getInitialSize() { 75 | return initialSize; 76 | } 77 | 78 | public void setInitialSize(int initialSize) { 79 | this.initialSize = initialSize; 80 | } 81 | 82 | public String getValidationQuery() { 83 | return validationQuery; 84 | } 85 | 86 | public void setValidationQuery(String validationQuery) { 87 | this.validationQuery = validationQuery; 88 | } 89 | 90 | public boolean isTestOnBorrow() { 91 | return testOnBorrow; 92 | } 93 | 94 | public void setTestOnBorrow(boolean testOnBorrow) { 95 | this.testOnBorrow = testOnBorrow; 96 | } 97 | 98 | public boolean isTestOnReturn() { 99 | return testOnReturn; 100 | } 101 | 102 | public void setTestOnReturn(boolean testOnReturn) { 103 | this.testOnReturn = testOnReturn; 104 | } 105 | 106 | private boolean testOnBorrow = false; 107 | 108 | private boolean testOnReturn = false; 109 | 110 | } -------------------------------------------------------------------------------- /src/main/java/at/ebssoftware/ebs/timesheet/model/User.java: -------------------------------------------------------------------------------- 1 | package at.ebssoftware.ebs.timesheet.model; 2 | 3 | 4 | public class User { 5 | 6 | private String ID; 7 | private String firstName; 8 | private String lastName; 9 | private String address; 10 | private String city; 11 | private String department; 12 | 13 | public String getID() { 14 | return ID; 15 | } 16 | public void setID(String iD) { 17 | ID = iD; 18 | } 19 | public String getFirstName() { 20 | return firstName; 21 | } 22 | public void setFirstName(String firstName) { 23 | this.firstName = firstName; 24 | } 25 | public String getLastName() { 26 | return lastName; 27 | } 28 | public void setLastName(String lastName) { 29 | this.lastName = lastName; 30 | } 31 | public String getAddress() { 32 | return address; 33 | } 34 | public void setAddress(String address) { 35 | this.address = address; 36 | } 37 | public String getCity() { 38 | return city; 39 | } 40 | public void setCity(String city) { 41 | this.city = city; 42 | } 43 | public String getDepartment() { 44 | return department; 45 | } 46 | public void setDepartment(String department) { 47 | this.department = department; 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/at/ebssoftware/ebs/timesheet/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package at.ebssoftware.ebs.timesheet.mybatis.mapper; 2 | 3 | import at.ebssoftware.ebs.timesheet.model.User; 4 | 5 | public interface UserMapper { 6 | 7 | User selectByPrimaryKey(String ID); 8 | } -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | tomcat.datasource.username:sa 2 | tomcat.datasource.password: 3 | tomcat.datasource.driverClassName:org.hsqldb.jdbcDriver 4 | tomcat.datasource.url:jdbc:hsqldb:mem:mydb -------------------------------------------------------------------------------- /src/main/resources/at/ebssoftware/ebs/timesheet/mybatis/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/test/java/at/ebssoftware/ebs/timesheet/ConfigurationTest.java: -------------------------------------------------------------------------------- 1 | package at.ebssoftware.ebs.timesheet; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.SpringApplicationConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | import at.ebssoftware.ebs.timesheet.configuration.TomcatPoolDataSourceProperties; 11 | import at.ebssoftware.ebs.timesheet.model.User; 12 | import at.ebssoftware.ebs.timesheet.mybatis.mapper.UserMapper; 13 | 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @SpringApplicationConfiguration(classes = { Application.class }) 16 | public class ConfigurationTest { 17 | 18 | @Autowired 19 | TomcatPoolDataSourceProperties dataSourceProperties; 20 | 21 | @Autowired 22 | UserMapper userMapper; 23 | 24 | @Test 25 | public void testMapperScanWithSpringBoot() { 26 | 27 | Assert.assertNotNull(dataSourceProperties); 28 | Assert.assertEquals("org.hsqldb.jdbcDriver", dataSourceProperties.getDriverClassName()); 29 | 30 | Assert.assertNotNull(userMapper); 31 | } 32 | 33 | } 34 | --------------------------------------------------------------------------------