Skip to content

Commit 6ef6384

Browse files
committed
首次导入代码
0 parents  commit 6ef6384

File tree

15 files changed

+972
-0
lines changed

15 files changed

+972
-0
lines changed

SpringBootDemo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

SpringBootDemo/pom.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.tony</groupId>
8+
<artifactId>com.tony.spring.boot</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<!-- 继承父包,此包会添加依赖,spring用到的核心包 -->
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.5.2.RELEASE</version>
15+
</parent>
16+
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
22+
</dependency>
23+
<!-- 热部署 -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-devtools</artifactId>
27+
<optional>true</optional>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-configuration-processor</artifactId>
33+
<optional>true</optional>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-redis</artifactId>
39+
<version>1.3.8.RELEASE</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.mybatis.spring.boot</groupId>
44+
<artifactId>mybatis-spring-boot-starter</artifactId>
45+
<version>1.3.0</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>com.alibaba</groupId>
50+
<artifactId>druid</artifactId>
51+
<version>1.0.19</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>mysql</groupId>
56+
<artifactId>mysql-connector-java</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>com.github.pagehelper</groupId>
61+
<artifactId>pagehelper</artifactId>
62+
<version>3.7.5</version>
63+
</dependency>
64+
65+
<!-- javax -->
66+
<dependency>
67+
<groupId>javax.inject</groupId>
68+
<artifactId>javax.inject</artifactId>
69+
<version>1</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>javax</groupId>
73+
<artifactId>javaee-api</artifactId>
74+
<version>7.0</version>
75+
<scope>provided</scope>
76+
</dependency>
77+
78+
</dependencies>
79+
80+
<properties>
81+
<java.version>1.8</java.version>
82+
</properties>
83+
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-maven-plugin</artifactId>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.tony.spring.boot;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
10+
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
11+
import org.springframework.boot.web.servlet.ServletComponentScan;
12+
import org.springframework.boot.web.support.SpringBootServletInitializer;
13+
14+
/**
15+
* Created by Administrator on 2017/4/19.
16+
*/
17+
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
18+
@ServletComponentScan
19+
@EnableAutoConfiguration
20+
@MapperScan("com.tony.spring.boot.mapper")
21+
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {
22+
23+
@Value("${server.port}")
24+
private int port;//应用的端口
25+
/**
26+
* 启动入口
27+
* @param args
28+
*/
29+
public static void main(String ... args){
30+
SpringApplication.run(Application.class, args);
31+
}
32+
33+
/**
34+
* 自定义端口
35+
*/
36+
@Override
37+
public void customize(ConfigurableEmbeddedServletContainer container) {
38+
container.setPort(port);
39+
}
40+
41+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.tony.spring.boot.config;
2+
3+
import java.sql.SQLException;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.boot.bind.RelaxedPropertyResolver;
10+
import org.springframework.context.EnvironmentAware;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.core.env.Environment;
14+
import org.springframework.transaction.annotation.EnableTransactionManagement;
15+
16+
import com.alibaba.druid.pool.DruidDataSource;
17+
18+
@Configuration
19+
@EnableTransactionManagement
20+
public class DataBaseConfiguration implements EnvironmentAware {
21+
22+
private RelaxedPropertyResolver propertyResolver;
23+
24+
private static Logger log = LoggerFactory.getLogger(DataBaseConfiguration.class);
25+
26+
private Environment env;
27+
28+
@Override
29+
public void setEnvironment(Environment env) {
30+
this.env = env;
31+
this.propertyResolver = new RelaxedPropertyResolver(env, "jdbc.datasource.");
32+
}
33+
34+
/**
35+
* 配置数据源
36+
* @Description TODO
37+
* @return
38+
*/
39+
@Bean(name = "dataSource",destroyMethod = "close")
40+
public DataSource dataSource() {
41+
log.debug(env.getActiveProfiles().toString());
42+
43+
DruidDataSource dataSource = new DruidDataSource();
44+
dataSource.setUrl(propertyResolver.getProperty("url"));
45+
dataSource.setUsername(propertyResolver.getProperty("username"));//用户名
46+
dataSource.setPassword(propertyResolver.getProperty("password"));//密码
47+
dataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
48+
dataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")));
49+
dataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")));
50+
dataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")));
51+
dataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")));
52+
dataSource.setTimeBetweenEvictionRunsMillis(Integer.parseInt(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));
53+
dataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(propertyResolver.getProperty("minEvictableIdleTimeMillis")));
54+
dataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"));
55+
dataSource.setTestOnBorrow(Boolean.getBoolean(propertyResolver.getProperty("testOnBorrow")));
56+
dataSource.setTestWhileIdle(Boolean.getBoolean(propertyResolver.getProperty("testWhileIdle")));
57+
dataSource.setTestOnReturn(Boolean.getBoolean(propertyResolver.getProperty("testOnReturn")));
58+
dataSource.setPoolPreparedStatements(Boolean.getBoolean(propertyResolver.getProperty("poolPreparedStatements")));
59+
dataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxOpenPreparedStatements")));
60+
try {
61+
dataSource.init();
62+
} catch (SQLException e) {
63+
64+
}
65+
return dataSource;
66+
}
67+
68+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.tony.spring.boot.config;
2+
3+
import javax.annotation.Resource;
4+
import javax.persistence.EntityManager;
5+
import javax.sql.DataSource;
6+
7+
import org.apache.commons.logging.Log;
8+
import org.apache.commons.logging.LogFactory;
9+
import org.apache.ibatis.session.SqlSessionFactory;
10+
import org.mybatis.spring.SqlSessionFactoryBean;
11+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
12+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
14+
import org.springframework.boot.bind.RelaxedPropertyResolver;
15+
import org.springframework.context.EnvironmentAware;
16+
import org.springframework.context.annotation.Bean;
17+
import org.springframework.context.annotation.Configuration;
18+
import org.springframework.core.env.Environment;
19+
import org.springframework.core.io.DefaultResourceLoader;
20+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
21+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
22+
import org.springframework.transaction.annotation.EnableTransactionManagement;
23+
24+
/**
25+
* 初始化SqlSessionFactory
26+
* Java Lib For OIS, Powered By BeiJing FanTongTianXia.
27+
* Copyright (c) 2014-2017 FanTongTianXia Co.,Ltd
28+
* http://www.chifaner.com/
29+
*
30+
* @ClassName MybatisConfiguration
31+
* @author Zhang.Tao
32+
* @Date 2017年4月24日 下午5:24:56
33+
* @version V2.0.0
34+
*/
35+
@Configuration
36+
@ConditionalOnClass({ EnableTransactionManagement.class, EntityManager.class })
37+
@AutoConfigureAfter({ DataBaseConfiguration.class })
38+
public class MybatisConfiguration implements EnvironmentAware {
39+
40+
private static Log logger = LogFactory.getLog(MybatisConfiguration.class);
41+
42+
private RelaxedPropertyResolver propertyResolver;
43+
44+
@Resource(name = "dataSource")
45+
DataSource dataSource;
46+
47+
@Override
48+
public void setEnvironment(Environment environment) {
49+
this.propertyResolver = new RelaxedPropertyResolver(environment,"mybatis.");
50+
}
51+
52+
/**
53+
* 初始化SessionFactory
54+
* @Description TODO
55+
* @return
56+
*/
57+
@Bean
58+
@ConditionalOnMissingBean
59+
public SqlSessionFactory sqlSessionFactory() {
60+
try {
61+
62+
System.err.println(propertyResolver.getProperty("mapperLocations"));
63+
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
64+
sessionFactory.setDataSource(dataSource);
65+
sessionFactory.setTypeAliasesPackage(propertyResolver
66+
.getProperty("typeAliasesPackage"));
67+
sessionFactory
68+
.setMapperLocations(new PathMatchingResourcePatternResolver()
69+
.getResources(propertyResolver
70+
.getProperty("mapperLocations")));
71+
sessionFactory
72+
.setConfigLocation(new DefaultResourceLoader()
73+
.getResource(propertyResolver
74+
.getProperty("configLocation")));
75+
76+
return sessionFactory.getObject();
77+
} catch (Exception e) {
78+
e.printStackTrace();
79+
logger.warn("Could not confiure mybatis session factory");
80+
return null;
81+
}
82+
}
83+
84+
@Bean
85+
@ConditionalOnMissingBean
86+
public DataSourceTransactionManager transactionManager() {
87+
return new DataSourceTransactionManager(dataSource);
88+
}
89+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.tony.spring.boot.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import redis.clients.jedis.JedisPool;
10+
import redis.clients.jedis.JedisPoolConfig;
11+
12+
/**
13+
* 配置redis数据源
14+
* Java Lib For OIS, Powered By BeiJing FanTongTianXia.
15+
* Copyright (c) 2014-2017 FanTongTianXia Co.,Ltd
16+
* http://www.chifaner.com/
17+
*
18+
* @ClassName RedisConfig
19+
* @author Zhang.Tao
20+
* @Date 2017年4月24日 下午5:25:30
21+
* @version V2.0.0
22+
*/
23+
@Configuration
24+
public class RedisConfig {
25+
26+
27+
@Bean(name= "jedis.pool")
28+
@Autowired
29+
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
30+
@Value("${jedis.pool.host}")String host,
31+
@Value("${jedis.pool.port}")int port) {
32+
return new JedisPool(config, host, port);
33+
}
34+
35+
@Bean(name= "jedis.pool.config")
36+
public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
37+
@Value("${jedis.pool.config.maxIdle}")int maxIdle,
38+
@Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
39+
JedisPoolConfig config = new JedisPoolConfig();
40+
config.setMaxTotal(maxTotal);
41+
config.setMaxIdle(maxIdle);
42+
config.setMaxWaitMillis(maxWaitMillis);
43+
return config;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)