diff --git a/mybatis-spring-boot/README.md b/mybatis-spring-boot/README.md deleted file mode 100644 index e66dae1..0000000 --- a/mybatis-spring-boot/README.md +++ /dev/null @@ -1,14 +0,0 @@ -spring-boot-mybatis -=========== - -对Spring boot组合mybatis的学习,一个是注解版,一个是配置版 - - -mybatis和spring结合大大的减少了使用mybatis的配置,主要是使用[mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter) - - -- [spring-boot-mybaits-annotation](https://github.com/ityouknow/spring-boot-starter/tree/master/mybatis-spring-boot/spring-boot-mybatis-annotation):注解版本 - -- [spring-boot-mybaits-xml](https://github.com/ityouknow/spring-boot-starter/tree/master/mybatis-spring-boot/spring-boot-mybatis-xml):xml配置版本 - -- users.sql :示例中的表结构 \ No newline at end of file diff --git a/spring-boot-helloWorld/mvnw b/spring-boot-helloWorld/mvnw new file mode 100644 index 0000000..a1ba1bf --- /dev/null +++ b/spring-boot-helloWorld/mvnw @@ -0,0 +1,233 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # + # Look for the Apple JDKs first to preserve the existing behaviour, and then look + # for the new JDKs provided by Oracle. + # + if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then + # + # Apple JDKs + # + export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home + fi + + if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then + # + # Apple JDKs + # + export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home + fi + + if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then + # + # Oracle JDKs + # + export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home + fi + + if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then + # + # Apple JDKs + # + export JAVA_HOME=`/usr/libexec/java_home` + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Migwn, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` +fi + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + local basedir=$(pwd) + local wdir=$(pwd) + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + wdir=$(cd "$wdir/.."; pwd) + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} "$@" diff --git a/spring-boot-helloWorld/mvnw.cmd b/spring-boot-helloWorld/mvnw.cmd new file mode 100644 index 0000000..2b934e8 --- /dev/null +++ b/spring-boot-helloWorld/mvnw.cmd @@ -0,0 +1,145 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +set MAVEN_CMD_LINE_ARGS=%* + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" + +set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% \ No newline at end of file diff --git a/spring-boot-helloWorld/pom.xml b/spring-boot-helloWorld/pom.xml new file mode 100644 index 0000000..790eddb --- /dev/null +++ b/spring-boot-helloWorld/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.neo + springBoot + 0.0.1-SNAPSHOT + jar + + springBoot + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.3.6.RELEASE + + + + + UTF-8 + 1.7 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/spring-boot-helloWorld/src/main/java/com/neo/Application.java b/spring-boot-helloWorld/src/main/java/com/neo/Application.java new file mode 100644 index 0000000..6cda50c --- /dev/null +++ b/spring-boot-helloWorld/src/main/java/com/neo/Application.java @@ -0,0 +1,12 @@ +package com.neo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-helloWorld/src/main/java/com/neo/controller/HelloWorldController.java b/spring-boot-helloWorld/src/main/java/com/neo/controller/HelloWorldController.java new file mode 100644 index 0000000..c231fae --- /dev/null +++ b/spring-boot-helloWorld/src/main/java/com/neo/controller/HelloWorldController.java @@ -0,0 +1,13 @@ +package com.neo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloWorldController { + + @RequestMapping("/hello") + public String index() { + return "Hello World xx"; + } +} \ No newline at end of file diff --git a/spring-boot-helloWorld/src/main/resources/application.properties b/spring-boot-helloWorld/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot-helloWorld/src/test/java/com/neo/ApplicationTests.java b/spring-boot-helloWorld/src/test/java/com/neo/ApplicationTests.java new file mode 100644 index 0000000..f35d76c --- /dev/null +++ b/spring-boot-helloWorld/src/test/java/com/neo/ApplicationTests.java @@ -0,0 +1,16 @@ +package com.neo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloTests.java b/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloTests.java new file mode 100644 index 0000000..02133d7 --- /dev/null +++ b/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloTests.java @@ -0,0 +1,39 @@ +package com.neo.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class HelloTests { + + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +} \ No newline at end of file diff --git a/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloWorldControlerTests.java b/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloWorldControlerTests.java new file mode 100644 index 0000000..423d57c --- /dev/null +++ b/spring-boot-helloWorld/src/test/java/com/neo/controller/HelloWorldControlerTests.java @@ -0,0 +1,37 @@ +package com.neo.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class HelloWorldControlerTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + } + +} \ No newline at end of file diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/pom.xml b/spring-boot-mybatis-annotation/pom.xml similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/pom.xml rename to spring-boot-mybatis-annotation/pom.xml diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/Application.java b/spring-boot-mybatis-annotation/src/main/java/com/neo/Application.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/Application.java rename to spring-boot-mybatis-annotation/src/main/java/com/neo/Application.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/entity/UserEntity.java b/spring-boot-mybatis-annotation/src/main/java/com/neo/entity/UserEntity.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/entity/UserEntity.java rename to spring-boot-mybatis-annotation/src/main/java/com/neo/entity/UserEntity.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/enums/UserSexEnum.java b/spring-boot-mybatis-annotation/src/main/java/com/neo/enums/UserSexEnum.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/enums/UserSexEnum.java rename to spring-boot-mybatis-annotation/src/main/java/com/neo/enums/UserSexEnum.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/mapper/UserMapper.java b/spring-boot-mybatis-annotation/src/main/java/com/neo/mapper/UserMapper.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/mapper/UserMapper.java rename to spring-boot-mybatis-annotation/src/main/java/com/neo/mapper/UserMapper.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/web/UserController.java b/spring-boot-mybatis-annotation/src/main/java/com/neo/web/UserController.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/java/com/neo/web/UserController.java rename to spring-boot-mybatis-annotation/src/main/java/com/neo/web/UserController.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/resources/application.properties b/spring-boot-mybatis-annotation/src/main/resources/application.properties similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/main/resources/application.properties rename to spring-boot-mybatis-annotation/src/main/resources/application.properties diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/test/java/com/neo/ApplicationTests.java b/spring-boot-mybatis-annotation/src/test/java/com/neo/ApplicationTests.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/test/java/com/neo/ApplicationTests.java rename to spring-boot-mybatis-annotation/src/test/java/com/neo/ApplicationTests.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-annotation/src/test/java/com/neo/mapper/UserMapperTest.java b/spring-boot-mybatis-annotation/src/test/java/com/neo/mapper/UserMapperTest.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-annotation/src/test/java/com/neo/mapper/UserMapperTest.java rename to spring-boot-mybatis-annotation/src/test/java/com/neo/mapper/UserMapperTest.java diff --git a/mybatis-spring-boot/users.sql b/spring-boot-mybatis-annotation/users.sql similarity index 100% rename from mybatis-spring-boot/users.sql rename to spring-boot-mybatis-annotation/users.sql diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/pom.xml b/spring-boot-mybatis-xml/pom.xml similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/pom.xml rename to spring-boot-mybatis-xml/pom.xml diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/Application.java b/spring-boot-mybatis-xml/src/main/java/com/neo/Application.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/Application.java rename to spring-boot-mybatis-xml/src/main/java/com/neo/Application.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/entity/UserEntity.java b/spring-boot-mybatis-xml/src/main/java/com/neo/entity/UserEntity.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/entity/UserEntity.java rename to spring-boot-mybatis-xml/src/main/java/com/neo/entity/UserEntity.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/enums/UserSexEnum.java b/spring-boot-mybatis-xml/src/main/java/com/neo/enums/UserSexEnum.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/enums/UserSexEnum.java rename to spring-boot-mybatis-xml/src/main/java/com/neo/enums/UserSexEnum.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/mapper/UserMapper.java b/spring-boot-mybatis-xml/src/main/java/com/neo/mapper/UserMapper.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/mapper/UserMapper.java rename to spring-boot-mybatis-xml/src/main/java/com/neo/mapper/UserMapper.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/web/UserController.java b/spring-boot-mybatis-xml/src/main/java/com/neo/web/UserController.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/java/com/neo/web/UserController.java rename to spring-boot-mybatis-xml/src/main/java/com/neo/web/UserController.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/resources/application.properties b/spring-boot-mybatis-xml/src/main/resources/application.properties similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/resources/application.properties rename to spring-boot-mybatis-xml/src/main/resources/application.properties diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/resources/mybatis/mapper/UserMapper.xml b/spring-boot-mybatis-xml/src/main/resources/mybatis/mapper/UserMapper.xml similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/resources/mybatis/mapper/UserMapper.xml rename to spring-boot-mybatis-xml/src/main/resources/mybatis/mapper/UserMapper.xml diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/main/resources/mybatis/mybatis-config.xml b/spring-boot-mybatis-xml/src/main/resources/mybatis/mybatis-config.xml similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/main/resources/mybatis/mybatis-config.xml rename to spring-boot-mybatis-xml/src/main/resources/mybatis/mybatis-config.xml diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/test/java/com/neo/ApplicationTests.java b/spring-boot-mybatis-xml/src/test/java/com/neo/ApplicationTests.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/test/java/com/neo/ApplicationTests.java rename to spring-boot-mybatis-xml/src/test/java/com/neo/ApplicationTests.java diff --git a/mybatis-spring-boot/spring-boot-mybatis-xml/src/test/java/com/neo/mapper/UserMapperTest.java b/spring-boot-mybatis-xml/src/test/java/com/neo/mapper/UserMapperTest.java similarity index 100% rename from mybatis-spring-boot/spring-boot-mybatis-xml/src/test/java/com/neo/mapper/UserMapperTest.java rename to spring-boot-mybatis-xml/src/test/java/com/neo/mapper/UserMapperTest.java diff --git a/spring-boot-mybatis-xml/users.sql b/spring-boot-mybatis-xml/users.sql new file mode 100644 index 0000000..c8a2769 --- /dev/null +++ b/spring-boot-mybatis-xml/users.sql @@ -0,0 +1,30 @@ +/* +Navicat MySQL Data Transfer + +Source Server : 本地 +Source Server Version : 50505 +Source Host : localhost:3306 +Source Database : test1 + +Target Server Type : MYSQL +Target Server Version : 50505 +File Encoding : 65001 + +Date: 2016-11-05 21:17:33 +*/ + +SET FOREIGN_KEY_CHECKS=0; + +-- ---------------------------- +-- Table structure for `users` +-- ---------------------------- +DROP TABLE IF EXISTS `users`; +CREATE TABLE `users` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', + `userName` varchar(32) DEFAULT NULL COMMENT '用户名', + `passWord` varchar(32) DEFAULT NULL COMMENT '密码', + `user_sex` varchar(32) DEFAULT NULL, + `nick_name` varchar(32) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8; + diff --git a/spring-boot-web/pom.xml b/spring-boot-web/pom.xml new file mode 100644 index 0000000..8051f69 --- /dev/null +++ b/spring-boot-web/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + com.neo + spring-boot-web + 0.0.1-SNAPSHOT + jar + + spring-boot-web + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.3.6.RELEASE + + + + + UTF-8 + 1.7 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.springframework.boot + spring-boot-devtools + true + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.webjars.bower + jquery + 2.0.3 + + + org.webjars.bower + bootstrap + 3.0.3 + + + org.springframework.boot + spring-boot-starter-redis + + + org.springframework.session + spring-session-data-redis + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/spring-boot-web/src/main/java/com/neo/Application.java b/spring-boot-web/src/main/java/com/neo/Application.java new file mode 100644 index 0000000..6cda50c --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/Application.java @@ -0,0 +1,12 @@ +package com.neo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-web/src/main/java/com/neo/WebConfiguration.java b/spring-boot-web/src/main/java/com/neo/WebConfiguration.java new file mode 100644 index 0000000..0627d74 --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/WebConfiguration.java @@ -0,0 +1,60 @@ +package com.neo; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + +import org.apache.catalina.filters.RemoteIpFilter; +import org.springframework.boot.context.embedded.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class WebConfiguration { + @Bean + public RemoteIpFilter remoteIpFilter() { + return new RemoteIpFilter(); + } + + @Bean + public FilterRegistrationBean testFilterRegistration() { + + FilterRegistrationBean registration = new FilterRegistrationBean(); + registration.setFilter(new MyFilter()); + registration.addUrlPatterns("/*"); + registration.addInitParameter("paramName", "paramValue"); + registration.setName("MyFilter"); + registration.setOrder(1); + return registration; + } + + public class MyFilter implements Filter { + @Override + public void destroy() { + // TODO Auto-generated method stub + } + + @Override + public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) + throws IOException, ServletException { + // TODO Auto-generated method stub + HttpServletRequest request = (HttpServletRequest) srequest; + System.out.println("this is MyFilter,url :"+request.getRequestURI()); + filterChain.doFilter(srequest, sresponse); + } + + @Override + public void init(FilterConfig arg0) throws ServletException { + // TODO Auto-generated method stub + } + } +} + + + diff --git a/spring-boot-web/src/main/java/com/neo/config/RedisConfig.java b/spring-boot-web/src/main/java/com/neo/config/RedisConfig.java new file mode 100644 index 0000000..2925ad9 --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/config/RedisConfig.java @@ -0,0 +1,63 @@ +package com.neo.config; + +import java.lang.reflect.Method; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.interceptor.KeyGenerator; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Configuration +@EnableCaching +public class RedisConfig extends CachingConfigurerSupport{ + + @Bean + public KeyGenerator keyGenerator() { + return new KeyGenerator() { + @Override + public Object generate(Object target, Method method, Object... params) { + StringBuilder sb = new StringBuilder(); + sb.append(target.getClass().getName()); + sb.append(method.getName()); + for (Object obj : params) { + sb.append(obj.toString()); + } + return sb.toString(); + } + }; + } + + @SuppressWarnings("rawtypes") + @Bean + public CacheManager cacheManager(RedisTemplate redisTemplate) { + RedisCacheManager rcm = new RedisCacheManager(redisTemplate); + //设置缓存过期时间 + //rcm.setDefaultExpiration(60);//秒 + return rcm; + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + StringRedisTemplate template = new StringRedisTemplate(factory); + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + ObjectMapper om = new ObjectMapper(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(om); + template.setValueSerializer(jackson2JsonRedisSerializer); + template.afterPropertiesSet(); + return template; + } + +} \ No newline at end of file diff --git a/spring-boot-web/src/main/java/com/neo/config/SessionConfig.java b/spring-boot-web/src/main/java/com/neo/config/SessionConfig.java new file mode 100644 index 0000000..d4fedfc --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/config/SessionConfig.java @@ -0,0 +1,10 @@ +package com.neo.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; + +@Configuration +@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30) +public class SessionConfig { + +} \ No newline at end of file diff --git a/spring-boot-web/src/main/java/com/neo/domain/User.java b/spring-boot-web/src/main/java/com/neo/domain/User.java new file mode 100644 index 0000000..74afa23 --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/domain/User.java @@ -0,0 +1,76 @@ +package com.neo.domain; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class User implements Serializable { + + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue + private Long id; + @Column(nullable = false, unique = true) + private String userName; + @Column(nullable = false) + private String passWord; + @Column(nullable = false, unique = true) + private String email; + @Column(nullable = true, unique = true) + private String nickName; + @Column(nullable = false) + private String regTime; + + public User() { + super(); + } + public User(String email, String nickName, String passWord, String userName, String regTime) { + super(); + this.email = email; + this.nickName = nickName; + this.passWord = passWord; + this.userName = userName; + this.regTime = regTime; + } + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + public String getUserName() { + return userName; + } + public void setUserName(String userName) { + this.userName = userName; + } + public String getPassWord() { + return passWord; + } + public void setPassWord(String passWord) { + this.passWord = passWord; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getNickName() { + return nickName; + } + public void setNickName(String nickName) { + this.nickName = nickName; + } + public String getRegTime() { + return regTime; + } + public void setRegTime(String regTime) { + this.regTime = regTime; + } + +} \ No newline at end of file diff --git a/spring-boot-web/src/main/java/com/neo/domain/UserRepository.java b/spring-boot-web/src/main/java/com/neo/domain/UserRepository.java new file mode 100644 index 0000000..5455dbd --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/domain/UserRepository.java @@ -0,0 +1,11 @@ +package com.neo.domain; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + + User findByUserName(String userName); + + User findByUserNameOrEmail(String username, String email); + +} \ No newline at end of file diff --git a/spring-boot-web/src/main/java/com/neo/util/NeoProperties.java b/spring-boot-web/src/main/java/com/neo/util/NeoProperties.java new file mode 100644 index 0000000..ab337bf --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/util/NeoProperties.java @@ -0,0 +1,26 @@ +package com.neo.util; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class NeoProperties { + + @Value("${com.neo.title}") + private String title; + @Value("${com.neo.description}") + private String description; + public String getTitle() { + return title; + } + public void setTitle(String title) { + this.title = title; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/spring-boot-web/src/main/java/com/neo/web/HelloController.java b/spring-boot-web/src/main/java/com/neo/web/HelloController.java new file mode 100644 index 0000000..5fd95a0 --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/web/HelloController.java @@ -0,0 +1,30 @@ +package com.neo.web; + +import java.util.Locale; +import java.util.UUID; + +import javax.servlet.http.HttpSession; + +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + @RequestMapping("/hello") + public String hello(Locale locale, Model model) { + return "hello world"; + } + + @RequestMapping("/uid") + String uid(HttpSession session) { + UUID uid = (UUID) session.getAttribute("uid"); + if (uid == null) { + uid = UUID.randomUUID(); + } + session.setAttribute("uid", uid); + return session.getId(); + } + +} \ No newline at end of file diff --git a/spring-boot-web/src/main/java/com/neo/web/ThymeleafController.java b/spring-boot-web/src/main/java/com/neo/web/ThymeleafController.java new file mode 100644 index 0000000..c384bd1 --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/web/ThymeleafController.java @@ -0,0 +1,26 @@ +package com.neo.web; + +import java.text.DateFormat; +import java.util.Date; +import java.util.Locale; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class ThymeleafController { + + @RequestMapping("/hi") + public String hello(Locale locale, Model model) { + model.addAttribute("greeting", "Hello!"); + + Date date = new Date(); + DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); + String formattedDate = dateFormat.format(date); + model.addAttribute("currentTime", formattedDate); + + return "hello"; + } + +} \ No newline at end of file diff --git a/spring-boot-web/src/main/java/com/neo/web/UserController.java b/spring-boot-web/src/main/java/com/neo/web/UserController.java new file mode 100644 index 0000000..983617c --- /dev/null +++ b/spring-boot-web/src/main/java/com/neo/web/UserController.java @@ -0,0 +1,34 @@ +package com.neo.web; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.neo.domain.User; +import com.neo.domain.UserRepository; + +@RestController +public class UserController { + + @Autowired + private UserRepository userRepository; + + @RequestMapping("/getUser") + @Cacheable(value="user-key") + public User getUser() { + User user=userRepository.findByUserName("aa"); + System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); + return user; + } + + @RequestMapping("/getUsers") + @Cacheable(value="key-Users") + public List getUsers() { + List users=userRepository.findAll(); + System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); + return users; + } +} \ No newline at end of file diff --git a/spring-boot-web/src/main/resources/application.properties b/spring-boot-web/src/main/resources/application.properties new file mode 100644 index 0000000..19063f0 --- /dev/null +++ b/spring-boot-web/src/main/resources/application.properties @@ -0,0 +1,32 @@ +spring.datasource.url=jdbc:mysql://localhost:3306/test +spring.datasource.username=root +spring.datasource.password=root +spring.datasource.driver-class-name=com.mysql.jdbc.Driver + +spring.jpa.properties.hibernate.hbm2ddl.auto=update +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect +spring.jpa.show-sql= true + +com.neo.title=\u7eaf\u6d01\u7684\u5fae\u7b11 +com.neo.description=\u5206\u4eab\u751f\u6d3b\u548c\u6280\u672f + +# REDIS (RedisProperties) +# Redis\u6570\u636e\u5e93\u7d22\u5f15\uff08\u9ed8\u8ba4\u4e3a0\uff09 +spring.redis.database=0 +# Redis\u670d\u52a1\u5668\u5730\u5740 +spring.redis.host=192.168.0.58 +# Redis\u670d\u52a1\u5668\u8fde\u63a5\u7aef\u53e3 +spring.redis.port=6379 +# Redis\u670d\u52a1\u5668\u8fde\u63a5\u5bc6\u7801\uff08\u9ed8\u8ba4\u4e3a\u7a7a\uff09 +spring.redis.password= +# \u8fde\u63a5\u6c60\u6700\u5927\u8fde\u63a5\u6570\uff08\u4f7f\u7528\u8d1f\u503c\u8868\u793a\u6ca1\u6709\u9650\u5236\uff09 +spring.redis.pool.max-active=8 +# \u8fde\u63a5\u6c60\u6700\u5927\u963b\u585e\u7b49\u5f85\u65f6\u95f4\uff08\u4f7f\u7528\u8d1f\u503c\u8868\u793a\u6ca1\u6709\u9650\u5236\uff09 +spring.redis.pool.max-wait=-1 +# \u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5927\u7a7a\u95f2\u8fde\u63a5 +spring.redis.pool.max-idle=8 +# \u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5 +spring.redis.pool.min-idle=0 +# \u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u6beb\u79d2\uff09 +spring.redis.timeout=0 + diff --git a/spring-boot-web/src/main/resources/static/css/starter.css b/spring-boot-web/src/main/resources/static/css/starter.css new file mode 100644 index 0000000..e2fc60c --- /dev/null +++ b/spring-boot-web/src/main/resources/static/css/starter.css @@ -0,0 +1,8 @@ +body { + padding-top: 50px; +} + +.starter-template { + padding: 40px 15px; + text-align: center; +} \ No newline at end of file diff --git a/spring-boot-web/src/main/resources/static/images/favicon.png b/spring-boot-web/src/main/resources/static/images/favicon.png new file mode 100644 index 0000000..890ef06 Binary files /dev/null and b/spring-boot-web/src/main/resources/static/images/favicon.png differ diff --git a/spring-boot-web/src/main/resources/templates/hello.html b/spring-boot-web/src/main/resources/templates/hello.html new file mode 100644 index 0000000..c417054 --- /dev/null +++ b/spring-boot-web/src/main/resources/templates/hello.html @@ -0,0 +1,18 @@ + + + + +
(navbar)
+ +
+
+

Spring MVC / Thymeleaf / Bootstrap

+

(greeting)

+

The current time is (time)

+
+
+ + + + + diff --git a/spring-boot-web/src/main/resources/templates/layout.html b/spring-boot-web/src/main/resources/templates/layout.html new file mode 100644 index 0000000..422fbb7 --- /dev/null +++ b/spring-boot-web/src/main/resources/templates/layout.html @@ -0,0 +1,54 @@ + + + + + + + + + + (title) + + + + + + + + +
+
+

Spring MVC/Thymeleaf/Bootstrap

+

(greeting)

+
+
+ + + + + diff --git a/spring-boot-web/src/test/java/com/neo/ApplicationTests.java b/spring-boot-web/src/test/java/com/neo/ApplicationTests.java new file mode 100644 index 0000000..f35d76c --- /dev/null +++ b/spring-boot-web/src/test/java/com/neo/ApplicationTests.java @@ -0,0 +1,16 @@ +package com.neo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-web/src/test/java/com/neo/domain/UserRepositoryTests.java b/spring-boot-web/src/test/java/com/neo/domain/UserRepositoryTests.java new file mode 100644 index 0000000..2a0a7e5 --- /dev/null +++ b/spring-boot-web/src/test/java/com/neo/domain/UserRepositoryTests.java @@ -0,0 +1,38 @@ +package com.neo.domain; + +import java.text.DateFormat; +import java.util.Date; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.neo.Application; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(Application.class) +public class UserRepositoryTests { + + @Autowired + private UserRepository userRepository; + + @Test + public void test() throws Exception { + Date date = new Date(); + DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); + String formattedDate = dateFormat.format(date); + + userRepository.save(new User("aa1", "aa@126.com", "aa", "aa123456",formattedDate)); + userRepository.save(new User("bb2", "bb@126.com", "bb", "bb123456",formattedDate)); + userRepository.save(new User("cc3", "cc@126.com", "cc", "cc123456",formattedDate)); + + Assert.assertEquals(9, userRepository.findAll().size()); + Assert.assertEquals("bb", userRepository.findByUserNameOrEmail("bb", "cc@126.com").getNickName()); + userRepository.delete(userRepository.findByUserName("aa1")); + } + +} \ No newline at end of file diff --git a/spring-boot-web/src/test/java/com/neo/util/TestRedis.java b/spring-boot-web/src/test/java/com/neo/util/TestRedis.java new file mode 100644 index 0000000..a8f2eb0 --- /dev/null +++ b/spring-boot-web/src/test/java/com/neo/util/TestRedis.java @@ -0,0 +1,52 @@ +package com.neo.util; + +import java.util.concurrent.TimeUnit; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.neo.Application; +import com.neo.domain.User; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(Application.class) +public class TestRedis { + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Autowired + private RedisTemplate redisTemplate; + + @Test + public void test() throws Exception { + stringRedisTemplate.opsForValue().set("aaa", "111"); + Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa")); + } + + @Test + public void testObj() throws Exception { + User user=new User("aa@126.com", "aa", "aa123456", "aa","123"); + ValueOperations operations=redisTemplate.opsForValue(); + operations.set("com.neox", user); + operations.set("com.neo.f", user,1,TimeUnit.SECONDS); + Thread.sleep(1000); + //redisTemplate.delete("com.neo.f"); + boolean exists=redisTemplate.hasKey("com.neo.f"); + if(exists){ + System.out.println("exists is true"); + }else{ + System.out.println("exists is false"); + } + // Assert.assertEquals("aa", operations.get("com.neo.f").getUserName()); + } + + +} \ No newline at end of file diff --git a/spring-boot-web/src/test/java/com/neo/web/HelloControlerTests.java b/spring-boot-web/src/test/java/com/neo/web/HelloControlerTests.java new file mode 100644 index 0000000..33869db --- /dev/null +++ b/spring-boot-web/src/test/java/com/neo/web/HelloControlerTests.java @@ -0,0 +1,57 @@ +package com.neo.web; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.neo.web.HelloController; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class HelloControlerTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + } + + + + @Test + public void testHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + + +} \ No newline at end of file diff --git a/spring-boot-web/src/test/java/com/neo/web/ProPertiesTest.java b/spring-boot-web/src/test/java/com/neo/web/ProPertiesTest.java new file mode 100644 index 0000000..6f684bb --- /dev/null +++ b/spring-boot-web/src/test/java/com/neo/web/ProPertiesTest.java @@ -0,0 +1,39 @@ +package com.neo.web; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.neo.Application; +import com.neo.util.NeoProperties; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(Application.class) +public class ProPertiesTest { + + + @Autowired + private NeoProperties neoProperties; + + + @Test + public void getHello() throws Exception { + System.out.println(neoProperties.getTitle()); + Assert.assertEquals(neoProperties.getTitle(), "纯洁的微笑"); + Assert.assertEquals(neoProperties.getDescription(), "分享生活和技术"); + } + + + @Test + public void testMap() throws Exception { + Map orderMinTime=new HashMap(); + long xx=orderMinTime.get("123"); + } + +} \ No newline at end of file