Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1d49ae

Browse files
committed
initial commit
0 parents  commit d1d49ae

File tree

10 files changed

+399
-0
lines changed

10 files changed

+399
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/bin
2+
/.settings
3+
/.classpath
4+
/.project
5+
/.gradle

build.gradle

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
compile files('lib/sqljdbc4.jar')
27+
28+
compile("org.springframework.boot:spring-boot-starter-web:1.0.0.RC3")
29+
compile("org.springframework.boot:spring-boot-starter-actuator:1.0.0.RC3")
30+
31+
compile("org.springframework:spring-jdbc:4.0.0.RELEASE")
32+
33+
compile("javax.inject:javax.inject:1")
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+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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> T createMapper(Class<T> type) throws Exception {
36+
// MapperFactoryBean<T> factory = new MapperFactoryBean<T>();
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+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package at.ebssoftware.ebs.timesheet.configuration;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
@ConfigurationProperties(name = "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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tomcat.datasource.username:sa
2+
tomcat.datasource.password:
3+
tomcat.datasource.driverClassName:org.hsqldb.jdbcDriver
4+
tomcat.datasource.url:jdbc:hsqldb:mem:mydb
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper namespace="at.ebssoftware.ebs.timesheet.mybatis.mapper.UserMapper">
4+
5+
<resultMap id="BaseResultMap" type="at.ebssoftware.ebs.timesheet.model.User">
6+
<!--
7+
WARNING - @mbggenerated
8+
This element is automatically generated by MyBatis Generator, do not modify.
9+
This element was generated on Tue Feb 11 09:02:26 CET 2014.
10+
-->
11+
<id column="UsrID" jdbcType="CHAR" property="ID" />
12+
<result column="FirstNameUsr" jdbcType="VARCHAR" property="firstName" />
13+
<result column="LastNameUsr" jdbcType="VARCHAR" property="lastName" />
14+
<result column="AddressUsr" jdbcType="VARCHAR" property="address" />
15+
<result column="CityUsr" jdbcType="VARCHAR" property="city" />
16+
<result column="DepartmentUsr" jdbcType="VARCHAR" property="department" />
17+
</resultMap>
18+
19+
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
20+
<!--
21+
WARNING - @mbggenerated
22+
This element is automatically generated by MyBatis Generator, do not modify.
23+
This element was generated on Tue Feb 11 09:02:26 CET 2014.
24+
-->
25+
select * from std.tbUser
26+
where UsrID = #{ID,jdbcType=CHAR}
27+
</select>
28+
29+
</mapper>

src/main/resources/logback.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<!-- encoders are assigned the type
5+
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6+
<encoder>
7+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
8+
</encoder>
9+
</appender>
10+
11+
<root level="info">
12+
<appender-ref ref="STDOUT" />
13+
</root>
14+
</configuration>

0 commit comments

Comments
 (0)