Commit 59a3fc51f270317704ecccf9c57c6021b71acf6e
0 parents
Exists in
master
1.较为完整的第一版
Showing
18 changed files
with
813 additions
and
0 deletions
Show diff stats
1 | +++ a/build.gradle | |
... | ... | @@ -0,0 +1,38 @@ |
1 | +/* | |
2 | + * This file was generated by the Gradle 'init' task. | |
3 | + * | |
4 | + * This generated file contains a sample Java Library project to get you started. | |
5 | + * For more details take a look at the Java Libraries chapter in the Gradle | |
6 | + * User Manual available at https://docs.gradle.org/5.3-rc-3/userguide/java_library_plugin.html | |
7 | + */ | |
8 | + | |
9 | +plugins { | |
10 | + // Apply the java-library plugin to add support for Java Library | |
11 | + id 'java' | |
12 | + id 'org.springframework.boot' version '2.1.3.RELEASE' | |
13 | +} | |
14 | + | |
15 | +apply plugin: 'java' | |
16 | +apply plugin: 'io.spring.dependency-management' | |
17 | + | |
18 | +repositories { | |
19 | + // Use jcenter for resolving your dependencies. | |
20 | + // You can declare any Maven/Ivy/file repository here. | |
21 | + jcenter() | |
22 | +} | |
23 | + | |
24 | +dependencies { | |
25 | + implementation('org.springframework.boot:spring-boot-starter') | |
26 | + implementation('org.springframework.boot:spring-boot-starter-web'){ | |
27 | + exclude group:'org.springframework.boot',module:'spring-boot-starter-tomcat' | |
28 | + } | |
29 | + implementation('org.springframework.boot:spring-boot-starter-jetty') | |
30 | + implementation('org.springframework.boot:spring-boot-starter-data-jdbc') | |
31 | + implementation('mysql:mysql-connector-java') | |
32 | + implementation('org.springframework.boot:spring-boot-starter-data-jpa') | |
33 | + implementation('org.springframework.boot:spring-boot-starter-data-redis'){ | |
34 | + exclude module:'lettuce' | |
35 | + } | |
36 | + implementation('redis.clients:jedis') | |
37 | + implementation('com.alibaba:druid-spring-boot-starter:1.1.10') | |
38 | +} | ... | ... |
No preview for this file type
1 | +++ a/gradle/wrapper/gradle-wrapper.properties | |
... | ... | @@ -0,0 +1,5 @@ |
1 | +distributionBase=GRADLE_USER_HOME | |
2 | +distributionPath=wrapper/dists | |
3 | +distributionUrl=https\://services.gradle.org/distributions/gradle-5.3-rc-3-bin.zip | |
4 | +zipStoreBase=GRADLE_USER_HOME | |
5 | +zipStorePath=wrapper/dists | ... | ... |
1 | +++ a/gradlew | |
... | ... | @@ -0,0 +1,188 @@ |
1 | +#!/usr/bin/env sh | |
2 | + | |
3 | +# | |
4 | +# Copyright 2015 the original author or authors. | |
5 | +# | |
6 | +# Licensed under the Apache License, Version 2.0 (the "License"); | |
7 | +# you may not use this file except in compliance with the License. | |
8 | +# You may obtain a copy of the License at | |
9 | +# | |
10 | +# http://www.apache.org/licenses/LICENSE-2.0 | |
11 | +# | |
12 | +# Unless required by applicable law or agreed to in writing, software | |
13 | +# distributed under the License is distributed on an "AS IS" BASIS, | |
14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 | +# See the License for the specific language governing permissions and | |
16 | +# limitations under the License. | |
17 | +# | |
18 | + | |
19 | +############################################################################## | |
20 | +## | |
21 | +## Gradle start up script for UN*X | |
22 | +## | |
23 | +############################################################################## | |
24 | + | |
25 | +# Attempt to set APP_HOME | |
26 | +# Resolve links: $0 may be a link | |
27 | +PRG="$0" | |
28 | +# Need this for relative symlinks. | |
29 | +while [ -h "$PRG" ] ; do | |
30 | + ls=`ls -ld "$PRG"` | |
31 | + link=`expr "$ls" : '.*-> \(.*\)$'` | |
32 | + if expr "$link" : '/.*' > /dev/null; then | |
33 | + PRG="$link" | |
34 | + else | |
35 | + PRG=`dirname "$PRG"`"/$link" | |
36 | + fi | |
37 | +done | |
38 | +SAVED="`pwd`" | |
39 | +cd "`dirname \"$PRG\"`/" >/dev/null | |
40 | +APP_HOME="`pwd -P`" | |
41 | +cd "$SAVED" >/dev/null | |
42 | + | |
43 | +APP_NAME="Gradle" | |
44 | +APP_BASE_NAME=`basename "$0"` | |
45 | + | |
46 | +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. | |
47 | +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' | |
48 | + | |
49 | +# Use the maximum available, or set MAX_FD != -1 to use that value. | |
50 | +MAX_FD="maximum" | |
51 | + | |
52 | +warn () { | |
53 | + echo "$*" | |
54 | +} | |
55 | + | |
56 | +die () { | |
57 | + echo | |
58 | + echo "$*" | |
59 | + echo | |
60 | + exit 1 | |
61 | +} | |
62 | + | |
63 | +# OS specific support (must be 'true' or 'false'). | |
64 | +cygwin=false | |
65 | +msys=false | |
66 | +darwin=false | |
67 | +nonstop=false | |
68 | +case "`uname`" in | |
69 | + CYGWIN* ) | |
70 | + cygwin=true | |
71 | + ;; | |
72 | + Darwin* ) | |
73 | + darwin=true | |
74 | + ;; | |
75 | + MINGW* ) | |
76 | + msys=true | |
77 | + ;; | |
78 | + NONSTOP* ) | |
79 | + nonstop=true | |
80 | + ;; | |
81 | +esac | |
82 | + | |
83 | +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar | |
84 | + | |
85 | +# Determine the Java command to use to start the JVM. | |
86 | +if [ -n "$JAVA_HOME" ] ; then | |
87 | + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then | |
88 | + # IBM's JDK on AIX uses strange locations for the executables | |
89 | + JAVACMD="$JAVA_HOME/jre/sh/java" | |
90 | + else | |
91 | + JAVACMD="$JAVA_HOME/bin/java" | |
92 | + fi | |
93 | + if [ ! -x "$JAVACMD" ] ; then | |
94 | + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME | |
95 | + | |
96 | +Please set the JAVA_HOME variable in your environment to match the | |
97 | +location of your Java installation." | |
98 | + fi | |
99 | +else | |
100 | + JAVACMD="java" | |
101 | + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. | |
102 | + | |
103 | +Please set the JAVA_HOME variable in your environment to match the | |
104 | +location of your Java installation." | |
105 | +fi | |
106 | + | |
107 | +# Increase the maximum file descriptors if we can. | |
108 | +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then | |
109 | + MAX_FD_LIMIT=`ulimit -H -n` | |
110 | + if [ $? -eq 0 ] ; then | |
111 | + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then | |
112 | + MAX_FD="$MAX_FD_LIMIT" | |
113 | + fi | |
114 | + ulimit -n $MAX_FD | |
115 | + if [ $? -ne 0 ] ; then | |
116 | + warn "Could not set maximum file descriptor limit: $MAX_FD" | |
117 | + fi | |
118 | + else | |
119 | + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" | |
120 | + fi | |
121 | +fi | |
122 | + | |
123 | +# For Darwin, add options to specify how the application appears in the dock | |
124 | +if $darwin; then | |
125 | + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" | |
126 | +fi | |
127 | + | |
128 | +# For Cygwin, switch paths to Windows format before running java | |
129 | +if $cygwin ; then | |
130 | + APP_HOME=`cygpath --path --mixed "$APP_HOME"` | |
131 | + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` | |
132 | + JAVACMD=`cygpath --unix "$JAVACMD"` | |
133 | + | |
134 | + # We build the pattern for arguments to be converted via cygpath | |
135 | + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` | |
136 | + SEP="" | |
137 | + for dir in $ROOTDIRSRAW ; do | |
138 | + ROOTDIRS="$ROOTDIRS$SEP$dir" | |
139 | + SEP="|" | |
140 | + done | |
141 | + OURCYGPATTERN="(^($ROOTDIRS))" | |
142 | + # Add a user-defined pattern to the cygpath arguments | |
143 | + if [ "$GRADLE_CYGPATTERN" != "" ] ; then | |
144 | + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" | |
145 | + fi | |
146 | + # Now convert the arguments - kludge to limit ourselves to /bin/sh | |
147 | + i=0 | |
148 | + for arg in "$@" ; do | |
149 | + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` | |
150 | + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option | |
151 | + | |
152 | + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition | |
153 | + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` | |
154 | + else | |
155 | + eval `echo args$i`="\"$arg\"" | |
156 | + fi | |
157 | + i=$((i+1)) | |
158 | + done | |
159 | + case $i in | |
160 | + (0) set -- ;; | |
161 | + (1) set -- "$args0" ;; | |
162 | + (2) set -- "$args0" "$args1" ;; | |
163 | + (3) set -- "$args0" "$args1" "$args2" ;; | |
164 | + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; | |
165 | + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; | |
166 | + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; | |
167 | + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; | |
168 | + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; | |
169 | + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; | |
170 | + esac | |
171 | +fi | |
172 | + | |
173 | +# Escape application args | |
174 | +save () { | |
175 | + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done | |
176 | + echo " " | |
177 | +} | |
178 | +APP_ARGS=$(save "$@") | |
179 | + | |
180 | +# Collect all arguments for the java command, following the shell quoting and substitution rules | |
181 | +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" | |
182 | + | |
183 | +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong | |
184 | +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then | |
185 | + cd "$(dirname "$0")" | |
186 | +fi | |
187 | + | |
188 | +exec "$JAVACMD" "$@" | ... | ... |
1 | +++ a/gradlew.bat | |
... | ... | @@ -0,0 +1,100 @@ |
1 | +@rem | |
2 | +@rem Copyright 2015 the original author or authors. | |
3 | +@rem | |
4 | +@rem Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | +@rem you may not use this file except in compliance with the License. | |
6 | +@rem You may obtain a copy of the License at | |
7 | +@rem | |
8 | +@rem http://www.apache.org/licenses/LICENSE-2.0 | |
9 | +@rem | |
10 | +@rem Unless required by applicable law or agreed to in writing, software | |
11 | +@rem distributed under the License is distributed on an "AS IS" BASIS, | |
12 | +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | +@rem See the License for the specific language governing permissions and | |
14 | +@rem limitations under the License. | |
15 | +@rem | |
16 | + | |
17 | +@if "%DEBUG%" == "" @echo off | |
18 | +@rem ########################################################################## | |
19 | +@rem | |
20 | +@rem Gradle startup script for Windows | |
21 | +@rem | |
22 | +@rem ########################################################################## | |
23 | + | |
24 | +@rem Set local scope for the variables with windows NT shell | |
25 | +if "%OS%"=="Windows_NT" setlocal | |
26 | + | |
27 | +set DIRNAME=%~dp0 | |
28 | +if "%DIRNAME%" == "" set DIRNAME=. | |
29 | +set APP_BASE_NAME=%~n0 | |
30 | +set APP_HOME=%DIRNAME% | |
31 | + | |
32 | +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. | |
33 | +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" | |
34 | + | |
35 | +@rem Find java.exe | |
36 | +if defined JAVA_HOME goto findJavaFromJavaHome | |
37 | + | |
38 | +set JAVA_EXE=java.exe | |
39 | +%JAVA_EXE% -version >NUL 2>&1 | |
40 | +if "%ERRORLEVEL%" == "0" goto init | |
41 | + | |
42 | +echo. | |
43 | +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. | |
44 | +echo. | |
45 | +echo Please set the JAVA_HOME variable in your environment to match the | |
46 | +echo location of your Java installation. | |
47 | + | |
48 | +goto fail | |
49 | + | |
50 | +:findJavaFromJavaHome | |
51 | +set JAVA_HOME=%JAVA_HOME:"=% | |
52 | +set JAVA_EXE=%JAVA_HOME%/bin/java.exe | |
53 | + | |
54 | +if exist "%JAVA_EXE%" goto init | |
55 | + | |
56 | +echo. | |
57 | +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% | |
58 | +echo. | |
59 | +echo Please set the JAVA_HOME variable in your environment to match the | |
60 | +echo location of your Java installation. | |
61 | + | |
62 | +goto fail | |
63 | + | |
64 | +:init | |
65 | +@rem Get command-line arguments, handling Windows variants | |
66 | + | |
67 | +if not "%OS%" == "Windows_NT" goto win9xME_args | |
68 | + | |
69 | +:win9xME_args | |
70 | +@rem Slurp the command line arguments. | |
71 | +set CMD_LINE_ARGS= | |
72 | +set _SKIP=2 | |
73 | + | |
74 | +:win9xME_args_slurp | |
75 | +if "x%~1" == "x" goto execute | |
76 | + | |
77 | +set CMD_LINE_ARGS=%* | |
78 | + | |
79 | +:execute | |
80 | +@rem Setup the command line | |
81 | + | |
82 | +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar | |
83 | + | |
84 | +@rem Execute Gradle | |
85 | +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% | |
86 | + | |
87 | +:end | |
88 | +@rem End local scope for the variables with windows NT shell | |
89 | +if "%ERRORLEVEL%"=="0" goto mainEnd | |
90 | + | |
91 | +:fail | |
92 | +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of | |
93 | +rem the _cmd.exe /c_ return code! | |
94 | +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 | |
95 | +exit /b 1 | |
96 | + | |
97 | +:mainEnd | |
98 | +if "%OS%"=="Windows_NT" endlocal | |
99 | + | |
100 | +:omega | ... | ... |
1 | +++ a/settings.gradle | |
... | ... | @@ -0,0 +1,10 @@ |
1 | +/* | |
2 | + * This file was generated by the Gradle 'init' task. | |
3 | + * | |
4 | + * The settings file is used to specify which projects to include in your build. | |
5 | + * | |
6 | + * Detailed information about configuring a multi-project build in Gradle can be found | |
7 | + * in the user manual at https://docs.gradle.org/5.3-rc-3/userguide/multi_project_builds.html | |
8 | + */ | |
9 | + | |
10 | +rootProject.name = 'spring-boot-template' | ... | ... |
1 | +++ a/src/main/java/com/springboot/template/Bootstrap.java | |
... | ... | @@ -0,0 +1,19 @@ |
1 | +package com.springboot.template; | |
2 | + | |
3 | +import org.springframework.boot.SpringApplication; | |
4 | +import org.springframework.boot.autoconfigure.SpringBootApplication; | |
5 | + | |
6 | +/** | |
7 | + * 启动类 | |
8 | + * @author wangbin | |
9 | + * | |
10 | + */ | |
11 | + | |
12 | +@SpringBootApplication | |
13 | +public class Bootstrap { | |
14 | + | |
15 | + public static void main(String[] args) { | |
16 | + SpringApplication.run(Bootstrap.class, args); | |
17 | + } | |
18 | + | |
19 | +} | ... | ... |
src/main/java/com/springboot/template/BootstrapContextListener.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/BootstrapContextListener.java | |
... | ... | @@ -0,0 +1,24 @@ |
1 | +package com.springboot.template; | |
2 | + | |
3 | +import javax.servlet.ServletContextEvent; | |
4 | + | |
5 | +import org.springframework.stereotype.Component; | |
6 | + | |
7 | +/** | |
8 | + * 上下文监听说明: | |
9 | + * 1.必须放在启动类平级目录 | |
10 | + * @author wangbin | |
11 | + * | |
12 | + */ | |
13 | +@Component | |
14 | +public class BootstrapContextListener implements javax.servlet.ServletContextListener{ | |
15 | + @Override | |
16 | + public void contextInitialized(ServletContextEvent sce) { | |
17 | + System.out.println("ServletContextListener:contextInitialized()"); | |
18 | + } | |
19 | + | |
20 | + @Override | |
21 | + public void contextDestroyed(ServletContextEvent sce) { | |
22 | + System.out.println("ServletContextListener:contextDestroyed()"); | |
23 | + } | |
24 | +} | ... | ... |
src/main/java/com/springboot/template/beanconfig/RedisConfig.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/beanconfig/RedisConfig.java | |
... | ... | @@ -0,0 +1,85 @@ |
1 | +package com.springboot.template.beanconfig; | |
2 | + | |
3 | +import org.springframework.beans.factory.annotation.Value; | |
4 | +import org.springframework.context.annotation.Bean; | |
5 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | |
6 | +import org.springframework.data.redis.core.RedisTemplate; | |
7 | +import org.springframework.data.redis.serializer.StringRedisSerializer; | |
8 | +import org.springframework.stereotype.Component; | |
9 | + | |
10 | +@Component | |
11 | +public class RedisConfig{ | |
12 | + @Value("${spring.redis.host}") | |
13 | + private String redisHost; | |
14 | + @Value("${spring.redis.port}") | |
15 | + private int redisPort; | |
16 | + @Value("${spring.redis.timeout}") | |
17 | + private int redisTimeout; | |
18 | + @Value("${spring.redis.password}") | |
19 | + private String redisAuth; | |
20 | + @Value("${spring.redis.database}") | |
21 | + private int redisDb; | |
22 | + @Value("${spring.redis.jedis.pool.max-active}") | |
23 | + private int maxActive; | |
24 | + @Value("${spring.redis.jedis.pool.max-wait}") | |
25 | + private int maxWait; | |
26 | + @Value("${spring.redis.jedis.pool.max-idle}") | |
27 | + private int maxIdle; | |
28 | + @Value("${spring.redis.jedis.pool.min-idle}") | |
29 | + private int minIdle; | |
30 | + | |
31 | + /** | |
32 | + * 1.注解bean标注的方法参数,会被扫描是否存在已经自动注入的bean | |
33 | + * 2.注解bean的返回对象会被自动注入到容器中 | |
34 | + * @param redisConnectionFactory | |
35 | + * @return | |
36 | + */ | |
37 | + @Bean | |
38 | + public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | |
39 | + /*//连接池配置 | |
40 | + JedisPoolConfig poolConfig = new JedisPoolConfig(); | |
41 | + poolConfig.setMaxTotal(maxActive); | |
42 | + poolConfig.setMaxIdle(maxIdle); | |
43 | + poolConfig.setMaxWaitMillis(maxWait); | |
44 | + poolConfig.setMinIdle(minIdle); | |
45 | + poolConfig.setTestOnBorrow(true); | |
46 | + poolConfig.setTestOnReturn(false); | |
47 | + poolConfig.setTestWhileIdle(true); | |
48 | + JedisClientConfiguration clientConfig = JedisClientConfiguration.builder() | |
49 | + .usePooling().poolConfig(poolConfig).and().readTimeout(Duration.ofMillis(redisTimeout)).build(); | |
50 | + | |
51 | + // 单点redis | |
52 | + RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(); | |
53 | + // 哨兵redis | |
54 | + // RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration(); | |
55 | + // 集群redis | |
56 | + // RedisClusterConfiguration redisConfig = new RedisClusterConfiguration(); | |
57 | + redisConfig.setHostName(redisHost); | |
58 | + redisConfig.setPassword(RedisPassword.of(redisAuth)); | |
59 | + redisConfig.setPort(redisPort); | |
60 | + redisConfig.setDatabase(redisDb); | |
61 | + | |
62 | + RedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisConfig,clientConfig); | |
63 | + */ | |
64 | + | |
65 | + /* | |
66 | + * JSON数据序列化处理 | |
67 | + //ObjectMapper | |
68 | + ObjectMapper objectMapper = new ObjectMapper(); | |
69 | + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | |
70 | + objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | |
71 | + | |
72 | + //jackjson | |
73 | + Jackson2JsonRedisSerializer jackson = new Jackson2JsonRedisSerializer(Object.class); | |
74 | + jackson.setObjectMapper(objectMapper); | |
75 | + */ | |
76 | + | |
77 | + //RESTTemplate | |
78 | + RedisTemplate<String, String> restTemplate = new RedisTemplate<String, String>(); | |
79 | + restTemplate.setValueSerializer(new StringRedisSerializer()); | |
80 | + restTemplate.setKeySerializer(new StringRedisSerializer()); | |
81 | + restTemplate.setConnectionFactory(redisConnectionFactory); | |
82 | + restTemplate.afterPropertiesSet(); | |
83 | + return restTemplate; | |
84 | + } | |
85 | +} | ... | ... |
src/main/java/com/springboot/template/controller/DemoController.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/controller/DemoController.java | |
... | ... | @@ -0,0 +1,53 @@ |
1 | +package com.springboot.template.controller; | |
2 | + | |
3 | +import org.slf4j.Logger; | |
4 | +import org.slf4j.LoggerFactory; | |
5 | +import org.springframework.beans.factory.annotation.Autowired; | |
6 | +import org.springframework.stereotype.Controller; | |
7 | +import org.springframework.web.bind.annotation.GetMapping; | |
8 | +import org.springframework.web.bind.annotation.RequestMapping; | |
9 | +import org.springframework.web.bind.annotation.ResponseBody; | |
10 | + | |
11 | +import com.springboot.template.redis.HelloRedis; | |
12 | +import com.springboot.template.service.JdbcService; | |
13 | + | |
14 | +@Controller | |
15 | +@RequestMapping("/demo") | |
16 | +public class DemoController { | |
17 | + @Autowired | |
18 | + private JdbcService jdbcService; | |
19 | + @Autowired | |
20 | + private HelloRedis helloRedis; | |
21 | + | |
22 | + @RequestMapping("/sayHello") | |
23 | + @ResponseBody | |
24 | + public String sayHello() { | |
25 | + return "hello"; | |
26 | + } | |
27 | + | |
28 | + @GetMapping("sayFromJdbc") | |
29 | + @ResponseBody | |
30 | + public String sayFromJdbc() { | |
31 | + return this.jdbcService.queryHello(); | |
32 | + } | |
33 | + | |
34 | + @GetMapping("sayFromJpa") | |
35 | + @ResponseBody | |
36 | + public String sayFromJpa() { | |
37 | + return this.jdbcService.queryHelloFromJpa(); | |
38 | + } | |
39 | + | |
40 | + @GetMapping("sayFromRedis") | |
41 | + @ResponseBody | |
42 | + public String sayFromRedis() { | |
43 | + return this.helloRedis.getName(); | |
44 | + } | |
45 | + | |
46 | + @GetMapping("sayFromLogback") | |
47 | + @ResponseBody | |
48 | + public String sayFromLogback() { | |
49 | + Logger log = LoggerFactory.getLogger(this.getClass()); | |
50 | + log.debug("HERE WE ARE!"); | |
51 | + return "wang bin logback"; | |
52 | + } | |
53 | +} | ... | ... |
src/main/java/com/springboot/template/entity/HelloEntity.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/entity/HelloEntity.java | |
... | ... | @@ -0,0 +1,28 @@ |
1 | +package com.springboot.template.entity; | |
2 | + | |
3 | +import javax.persistence.Entity; | |
4 | +import javax.persistence.Id; | |
5 | +import javax.persistence.Table; | |
6 | + | |
7 | + | |
8 | +@Entity | |
9 | +@Table(name="hello") | |
10 | +public class HelloEntity { | |
11 | + @Id | |
12 | + private Long id; | |
13 | + private String name; | |
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 | + | |
27 | + | |
28 | +} | ... | ... |
src/main/java/com/springboot/template/redis/HelloRedis.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/redis/HelloRedis.java | |
... | ... | @@ -0,0 +1,20 @@ |
1 | +package com.springboot.template.redis; | |
2 | + | |
3 | +import org.springframework.beans.factory.annotation.Autowired; | |
4 | +import org.springframework.data.redis.core.RedisTemplate; | |
5 | +import org.springframework.stereotype.Component; | |
6 | + | |
7 | +@Component | |
8 | +public class HelloRedis { | |
9 | + @Autowired | |
10 | + private RedisTemplate redisTemplate; | |
11 | + | |
12 | + public String getName() { | |
13 | + Object name = this.redisTemplate.opsForValue().get("name"); | |
14 | + if(name == null) { | |
15 | + return "没有在Redis中找到name"; | |
16 | + }else { | |
17 | + return name.toString(); | |
18 | + } | |
19 | + } | |
20 | +} | ... | ... |
src/main/java/com/springboot/template/reposity/HelloRepository.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/reposity/HelloRepository.java | |
... | ... | @@ -0,0 +1,9 @@ |
1 | +package com.springboot.template.reposity; | |
2 | + | |
3 | +import org.springframework.data.repository.CrudRepository; | |
4 | + | |
5 | +import com.springboot.template.entity.HelloEntity; | |
6 | + | |
7 | +public interface HelloRepository extends CrudRepository<HelloEntity, Long> { | |
8 | + | |
9 | +} | ... | ... |
src/main/java/com/springboot/template/service/JdbcService.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/service/JdbcService.java | |
... | ... | @@ -0,0 +1,9 @@ |
1 | +package com.springboot.template.service; | |
2 | + | |
3 | +import org.springframework.stereotype.Component; | |
4 | + | |
5 | +public interface JdbcService { | |
6 | + public String queryHello(); | |
7 | + | |
8 | + public String queryHelloFromJpa(); | |
9 | +} | ... | ... |
src/main/java/com/springboot/template/service/impl/JdbcServerImpl.java
0 → 100644
1 | +++ a/src/main/java/com/springboot/template/service/impl/JdbcServerImpl.java | |
... | ... | @@ -0,0 +1,46 @@ |
1 | +package com.springboot.template.service.impl; | |
2 | + | |
3 | +import java.util.Map; | |
4 | +import java.util.Optional; | |
5 | + | |
6 | +import org.springframework.beans.factory.annotation.Autowired; | |
7 | +import org.springframework.jdbc.core.JdbcTemplate; | |
8 | +import org.springframework.stereotype.Component; | |
9 | + | |
10 | +import com.springboot.template.entity.HelloEntity; | |
11 | +import com.springboot.template.reposity.HelloRepository; | |
12 | +import com.springboot.template.service.JdbcService; | |
13 | + | |
14 | +@Component | |
15 | +public class JdbcServerImpl implements JdbcService{ | |
16 | + @Autowired | |
17 | + private JdbcTemplate JdbcTemplte; | |
18 | + @Autowired | |
19 | + private HelloRepository helloRepository; | |
20 | + | |
21 | + @Override | |
22 | + public String queryHello() { | |
23 | + try { | |
24 | + Map<String, Object> data = this.JdbcTemplte.queryForMap("select * from demo.hello limit 1"); | |
25 | + if(data.isEmpty()) { | |
26 | + return "没有发现任何数据"; | |
27 | + }else { | |
28 | + return data.get("name").toString(); | |
29 | + } | |
30 | + }catch(Exception e) { | |
31 | + e.printStackTrace(); | |
32 | + return "数据库不存在记录"; | |
33 | + } | |
34 | + } | |
35 | + | |
36 | + @Override | |
37 | + public String queryHelloFromJpa() { | |
38 | + Optional<HelloEntity> entity = this.helloRepository.findById(1L); | |
39 | + if(entity == null) { | |
40 | + return "没有找到ID=1对象"; | |
41 | + }else { | |
42 | + return entity.get().getName(); | |
43 | + } | |
44 | + } | |
45 | + | |
46 | +} | ... | ... |
1 | +++ a/src/main/resources/application.properties | |
... | ... | @@ -0,0 +1,29 @@ |
1 | +server.port=8080 | |
2 | + | |
3 | +spring.datasource.url=jdbc:mysql://localhost:3306/demo | |
4 | +spring.datasource.username=wangbin | |
5 | +spring.datasource.password=wangbin | |
6 | +spring.datasource.driver-class-name=com.mysql.jdbc.Driver | |
7 | +spring.datasource.type=com.alibaba.druid.pool.DruidDataSource | |
8 | + | |
9 | +spring.h2.console.enabled=true | |
10 | +spring.jpa.open-in-view=true | |
11 | +spring.data.jpa.repositories.bootstrap-mode=default | |
12 | +logging.level.org.hibernate.SQL=debug | |
13 | + | |
14 | +#允许覆盖bean注入 | |
15 | +spring.main.allow-bean-definition-overriding=true | |
16 | + | |
17 | +#redis配置 | |
18 | +spring.redis.host=localhost | |
19 | +spring.redis.port=6379 | |
20 | +spring.redis.timeout=30 | |
21 | +spring.redis.password= | |
22 | +spring.redis.database=0 | |
23 | +spring.redis.jedis.pool.max-idle=8 | |
24 | +spring.redis.jedis.pool.min-idle=0 | |
25 | +spring.redis.jedis.pool.max-active=8 | |
26 | +spring.redis.jedis.pool.max-wait=-1 | |
27 | + | |
28 | +logging.config=classpath:logback-spring.xml | |
29 | +logging.path=/home/wangbin/logs/ | ... | ... |
1 | +++ a/src/main/resources/logback-spring.xml | |
... | ... | @@ -0,0 +1,140 @@ |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<configuration> | |
3 | + <!-- | |
4 | + 说明: | |
5 | + 1、日志级别及文件 | |
6 | + 日志记录采用分级记录,级别与日志文件名相对应,不同级别的日志信息记录到不同的日志文件中 | |
7 | + 例如:error级别记录到log_error_xxx.log或log_error.log(该文件为当前记录的日志文件),而log_error_xxx.log为归档日志, | |
8 | + 日志文件按日期记录,同一天内,若日志文件大小等于或大于2M,则按0、1、2...顺序分别命名 | |
9 | + 例如log-level-2013-12-21.0.log | |
10 | + 其它级别的日志也是如此。 | |
11 | + 2、文件路径 | |
12 | + 若开发、测试用,在Eclipse中运行项目,则到Eclipse的安装路径查找logs文件夹,以相对路径../logs。 | |
13 | + 若部署到Tomcat下,则在Tomcat下的logs文件中 | |
14 | + 3、Appender | |
15 | + FILEERROR对应error级别,文件名以log-error-xxx.log形式命名 | |
16 | + FILEWARN对应warn级别,文件名以log-warn-xxx.log形式命名 | |
17 | + FILEINFO对应info级别,文件名以log-info-xxx.log形式命名 | |
18 | + FILEDEBUG对应debug级别,文件名以log-debug-xxx.log形式命名 | |
19 | + stdout将日志信息输出到控制上,为方便开发测试使用 | |
20 | + --> | |
21 | + <contextName>SpringBootDemo</contextName> | |
22 | + <property name="LOG_PATH" value="/home/wangbin/logs/" /> | |
23 | + <!--设置系统日志目录--> | |
24 | + <property name="APPDIR" value="SpringBootDemo" /> | |
25 | + | |
26 | + <!-- 日志记录器,日期滚动记录 --> | |
27 | + <appender name="FILEERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
28 | + <!-- 正在记录的日志文件的路径及文件名 --> | |
29 | + <file>${LOG_PATH}/${APPDIR}/log_error.log</file> | |
30 | + <!-- 日志记录器的滚动策略,按日期,按大小记录 --> | |
31 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
32 | + <!-- 归档的日志文件的路径,例如今天是2013-12-21日志,当前写的日志文件路径为file节点指定,可以将此文件与file指定文件路径设置为不同路径,从而将当前日志文件或归档日志文件置不同的目录。 | |
33 | + 而2013-12-21的日志文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 --> | |
34 | + <fileNamePattern>${LOG_PATH}/${APPDIR}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | |
35 | + <!-- 除按日志记录之外,还配置了日志文件不能超过2M,若超过2M,日志文件会以索引0开始, | |
36 | + 命名日志文件,例如log-error-2013-12-21.0.log --> | |
37 | + <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |
38 | + <maxFileSize>2MB</maxFileSize> | |
39 | + </timeBasedFileNamingAndTriggeringPolicy> | |
40 | + </rollingPolicy> | |
41 | + <!-- 追加方式记录日志 --> | |
42 | + <append>true</append> | |
43 | + <!-- 日志文件的格式 --> | |
44 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | |
45 | + <pattern>===%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> | |
46 | + <charset>utf-8</charset> | |
47 | + </encoder> | |
48 | + <!-- 此日志文件只记录info级别的 --> | |
49 | + <filter class="ch.qos.logback.classic.filter.LevelFilter"> | |
50 | + <level>error</level> | |
51 | + <onMatch>ACCEPT</onMatch> | |
52 | + <onMismatch>DENY</onMismatch> | |
53 | + </filter> | |
54 | + </appender> | |
55 | + | |
56 | + <!-- 日志记录器,日期滚动记录 --> | |
57 | + <appender name="FILEWARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
58 | + <!-- 正在记录的日志文件的路径及文件名 --> | |
59 | + <file>${LOG_PATH}/${APPDIR}/log_warn.log</file> | |
60 | + <!-- 日志记录器的滚动策略,按日期,按大小记录 --> | |
61 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
62 | + <!-- 归档的日志文件的路径,例如今天是2013-12-21日志,当前写的日志文件路径为file节点指定,可以将此文件与file指定文件路径设置为不同路径,从而将当前日志文件或归档日志文件置不同的目录。 | |
63 | + 而2013-12-21的日志文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 --> | |
64 | + <fileNamePattern>${LOG_PATH}/${APPDIR}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | |
65 | + <!-- 除按日志记录之外,还配置了日志文件不能超过2M,若超过2M,日志文件会以索引0开始, | |
66 | + 命名日志文件,例如log-error-2013-12-21.0.log --> | |
67 | + <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |
68 | + <maxFileSize>2MB</maxFileSize> | |
69 | + </timeBasedFileNamingAndTriggeringPolicy> | |
70 | + </rollingPolicy> | |
71 | + <!-- 追加方式记录日志 --> | |
72 | + <append>true</append> | |
73 | + <!-- 日志文件的格式 --> | |
74 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | |
75 | + <pattern>===%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> | |
76 | + <charset>utf-8</charset> | |
77 | + </encoder> | |
78 | + <!-- 此日志文件只记录info级别的 --> | |
79 | + <filter class="ch.qos.logback.classic.filter.LevelFilter"> | |
80 | + <level>warn</level> | |
81 | + <onMatch>ACCEPT</onMatch> | |
82 | + <onMismatch>DENY</onMismatch> | |
83 | + </filter> | |
84 | + </appender> | |
85 | + | |
86 | + <!-- 日志记录器,日期滚动记录 --> | |
87 | + <appender name="FILEINFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
88 | + <!-- 正在记录的日志文件的路径及文件名 --> | |
89 | + <file>${LOG_PATH}/${APPDIR}/log_info.log</file> | |
90 | + <!-- 日志记录器的滚动策略,按日期,按大小记录 --> | |
91 | + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
92 | + <!-- 归档的日志文件的路径,例如今天是2013-12-21日志,当前写的日志文件路径为file节点指定,可以将此文件与file指定文件路径设置为不同路径,从而将当前日志文件或归档日志文件置不同的目录。 | |
93 | + 而2013-12-21的日志文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 --> | |
94 | + <fileNamePattern>${LOG_PATH}/${APPDIR}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern> | |
95 | + <!-- 除按日志记录之外,还配置了日志文件不能超过2M,若超过2M,日志文件会以索引0开始, | |
96 | + 命名日志文件,例如log-error-2013-12-21.0.log --> | |
97 | + <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |
98 | + <maxFileSize>2MB</maxFileSize> | |
99 | + </timeBasedFileNamingAndTriggeringPolicy> | |
100 | + </rollingPolicy> | |
101 | + <!-- 追加方式记录日志 --> | |
102 | + <append>true</append> | |
103 | + <!-- 日志文件的格式 --> | |
104 | + <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | |
105 | + <pattern>===%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> | |
106 | + <charset>utf-8</charset> | |
107 | + </encoder> | |
108 | + <!-- 此日志文件只记录info级别的 --> | |
109 | + <filter class="ch.qos.logback.classic.filter.LevelFilter"> | |
110 | + <level>info</level> | |
111 | + <onMatch>ACCEPT</onMatch> | |
112 | + <onMismatch>DENY</onMismatch> | |
113 | + </filter> | |
114 | + </appender> | |
115 | + | |
116 | + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | |
117 | + <!--encoder 默认配置为PatternLayoutEncoder--> | |
118 | + <encoder> | |
119 | + <pattern>===%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern> | |
120 | + <charset>utf-8</charset> | |
121 | + </encoder> | |
122 | + <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息--> | |
123 | + <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | |
124 | + <level>debug</level> | |
125 | + </filter> | |
126 | + </appender> | |
127 | + | |
128 | + <logger name="org.springframework" level="WARN" /> | |
129 | + <logger name="org.hibernate" level="WARN" /> | |
130 | + | |
131 | + <!-- 生产环境下,将此级别配置为适合的级别,以免日志文件太多或影响程序性能 --> | |
132 | + <root level="INFO"> | |
133 | + <appender-ref ref="FILEERROR" /> | |
134 | + <appender-ref ref="FILEWARN" /> | |
135 | + <appender-ref ref="FILEINFO" /> | |
136 | + | |
137 | + <!-- 生产环境将请stdout,testfile去掉 --> | |
138 | + <appender-ref ref="STDOUT" /> | |
139 | + </root> | |
140 | +</configuration> | |
0 | 141 | \ No newline at end of file | ... | ... |