├── .gitignore ├── README.md ├── springboot-admin ├── springboot-admin-client │ └── src │ │ └── main │ │ └── java │ │ └── net │ │ └── codingme │ │ └── boot │ │ ├── config │ │ └── SecuritySecureConfig.java │ │ └── service │ │ ├── UserService.java │ │ └── impl │ │ └── UserServiceImpl.java └── springboot-admin-server │ └── src │ └── main │ └── java │ └── net │ └── codingme │ └── admin │ └── server │ └── config │ └── CustomNotifier.java ├── springboot-banner ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── banner │ │ │ ├── BannerApplication.java │ │ │ └── util │ │ │ └── GeneratorTextImage.java │ └── resources │ │ ├── application.properties │ │ ├── banner.jpg │ │ └── banner.txt │ └── test │ └── java │ └── net │ └── codingme │ └── banner │ └── BannerApplicationTests.java ├── springboot-config ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ └── ServiceConfig.java │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ ├── domain │ │ │ ├── Dog.java │ │ │ ├── Person.java │ │ │ ├── PersonSource.java │ │ │ └── PersonValue.java │ │ │ └── service │ │ │ └── HelloService.java │ └── resources │ │ ├── application-prod.properties │ │ ├── application.properties │ │ ├── application.yml │ │ ├── domain-person.properties │ │ └── spring-service.xml │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ └── HelloApplicationTests.java ├── springboot-data-jdbc ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ └── DruidConfig.java │ │ │ └── controller │ │ │ └── JdbcController.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ └── SpringbootDataJdbcApplicationTests.java ├── springboot-data-jpa ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ ├── ConverterDate.java │ │ │ ├── DruidConfig.java │ │ │ ├── LogAspect.java │ │ │ ├── LogHandlerInterceptor.java │ │ │ └── WebMvcConfig.java │ │ │ ├── controller │ │ │ ├── HelloController.java │ │ │ └── UserController.java │ │ │ ├── domain │ │ │ ├── Response.java │ │ │ ├── User.java │ │ │ └── repository │ │ │ │ └── UserRepository.java │ │ │ ├── enums │ │ │ └── ResponseEnum.java │ │ │ ├── exception │ │ │ ├── BaseException.java │ │ │ ├── ErrorAttributesCustom.java │ │ │ └── ExceptionHandle.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ └── utils │ │ │ └── ResponseUtill.java │ └── resources │ │ ├── application.properties │ │ ├── static │ │ └── css │ │ │ └── floating-labels.css │ │ └── templates │ │ ├── error │ │ └── 4xx.html │ │ ├── login.html │ │ └── user.html │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ └── SpringbootDataJpaApplicationTests.java ├── springboot-data-mybatis-multiple-datasource ├── pom.xml └── src │ ├── .DS_Store │ └── main │ ├── .DS_Store │ ├── java │ └── com │ │ └── wdbyte │ │ ├── BootApplication.java │ │ ├── config │ │ ├── PrimaryDataSourceConfig.java │ │ └── SecondDataSourceConfig.java │ │ ├── controller │ │ ├── BookController.java │ │ ├── HelloController.java │ │ └── UserController.java │ │ ├── domain │ │ ├── Book.java │ │ ├── Response.java │ │ └── User.java │ │ ├── enums │ │ └── ResponseEnum.java │ │ ├── mapper │ │ ├── datasource2 │ │ │ └── UserMapper.java │ │ └── primary │ │ │ └── BookMapper.java │ │ ├── service │ │ ├── BookService.java │ │ ├── UserService.java │ │ └── impl │ │ │ ├── BookServiceImpl.java │ │ │ └── UserServiceImpl.java │ │ └── utils │ │ ├── MybatisGenerator.java │ │ └── ResponseUtill.java │ └── resources │ ├── .DS_Store │ ├── application.properties │ └── mapper │ ├── BookMapper.xml │ └── UserMapper.xml ├── springboot-data-mybatis-page ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── MybatisPageApplication.java │ │ │ ├── config │ │ │ └── DruidConfig.java │ │ │ ├── domain │ │ │ ├── Book.java │ │ │ └── mapper │ │ │ │ └── BookMapper.java │ │ │ └── util │ │ │ ├── MybatisGenerator.java │ │ │ └── MybatisMapper.java │ └── resources │ │ ├── application.properties │ │ ├── generator │ │ └── generatorConfig.xml │ │ └── mapper │ │ └── BookMapper.xml │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── SpringbootDataMybatisPageApplicationTests.java │ └── domain │ └── mapper │ └── BookMapperTest.java ├── springboot-data-mybatis ├── .gitignore ├── generatorConfig.xml ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ ├── ConverterDate.java │ │ │ ├── DruidConfig.java │ │ │ ├── LogAspect.java │ │ │ ├── LogHandlerInterceptor.java │ │ │ ├── SwaggerConfig.java │ │ │ └── WebMvcConfig.java │ │ │ ├── controller │ │ │ ├── BookController.java │ │ │ ├── HelloController.java │ │ │ └── UserController.java │ │ │ ├── domain │ │ │ ├── Book.java │ │ │ ├── Response.java │ │ │ ├── User.java │ │ │ ├── mapper │ │ │ │ └── BookMapper.java │ │ │ └── repository │ │ │ │ └── UserRepository.java │ │ │ ├── enums │ │ │ └── ResponseEnum.java │ │ │ ├── exception │ │ │ ├── BaseException.java │ │ │ ├── ErrorAttributesCustom.java │ │ │ └── ExceptionHandle.java │ │ │ ├── service │ │ │ ├── BookService.java │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ ├── BookServiceImpl.java │ │ │ │ └── UserServiceImpl.java │ │ │ └── utils │ │ │ ├── MybatisGenerator.java │ │ │ └── ResponseUtill.java │ └── resources │ │ ├── application.properties │ │ ├── css │ │ └── floating-labels.css │ │ ├── mapper │ │ └── BookMapper.xml │ │ └── templates │ │ ├── error │ │ └── 4xx.html │ │ ├── login.html │ │ └── user.html │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── SpringbootDataMybatisApplicationTests.java │ └── domain │ └── mapper │ └── BookMapperTest.java ├── springboot-hello ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── coding │ │ │ └── boot │ │ │ ├── HelloApplication.java │ │ │ └── controller │ │ │ └── HelloController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── net │ └── coding │ └── boot │ ├── HelloApplicationTestBySpringBoot.java │ └── HelloApplicationTests.java ├── springboot-logback ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ └── ServiceConfig.java │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ ├── domain │ │ │ ├── Dog.java │ │ │ ├── Person.java │ │ │ ├── PersonSource.java │ │ │ └── PersonValue.java │ │ │ └── service │ │ │ └── HelloService.java │ └── resources │ │ ├── config │ │ ├── application-prod.properties │ │ ├── application.properties │ │ ├── application.yml │ │ ├── domain-person.properties │ │ └── spring-service.xml │ │ └── logback-spring.xml │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── HelloApplicationTests.java │ └── LogbackTest.java ├── springboot-mail ├── .gitignore ├── apple.png ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── MailApplication.java │ │ │ └── service │ │ │ └── MailService.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── RegisterSuccess.html │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── SpringbootMailApplicationTests.java │ └── service │ └── MailServiceTest.java ├── springboot-module-demo ├── .gitignore ├── pom.xml ├── product-common │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── wdbyte │ │ └── entity │ │ └── Product.java └── product-web │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── wdbyte │ │ ├── Application.java │ │ └── controller │ │ └── ProductController.java │ └── resources │ └── application.properties ├── springboot-properties ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ └── domain │ │ │ └── proper │ │ │ ├── Dog.java │ │ │ ├── Person.java │ │ │ ├── PersonSource.java │ │ │ └── PersonValue.java │ └── resources │ │ ├── application-prod.properties │ │ ├── application.properties │ │ ├── application.yml │ │ └── domain-person.properties │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ └── HelloApplicationTests.java ├── springboot-starter ├── myapp-spring-boot-autoconfigure │ ├── .gitignore │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── starter │ │ │ ├── HelloProperties.java │ │ │ ├── HelloService.java │ │ │ └── HelloServiceAutoConfiguration.java │ │ └── resources │ │ └── META-INF │ │ └── spring.factories ├── myapp-spring-boot-starter-test │ ├── .gitignore │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── net │ │ │ │ └── codingme │ │ │ │ └── starter │ │ │ │ ├── MyappSpringBootStarterTestApplication.java │ │ │ │ └── controller │ │ │ │ └── HelloController.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── net │ │ └── codingme │ │ └── starter │ │ └── MyappSpringBootStarterTestApplicationTests.java └── myapp-spring-boot-starter │ └── pom.xml ├── springboot-web-error ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ ├── LogHandlerInterceptor.java │ │ │ └── WebMvcConfig.java │ │ │ ├── controller │ │ │ ├── HelloController.java │ │ │ └── UserController.java │ │ │ ├── domain │ │ │ ├── Response.java │ │ │ └── User.java │ │ │ ├── enums │ │ │ └── ResponseEnum.java │ │ │ ├── exception │ │ │ ├── BaseException.java │ │ │ ├── ErrorAttributesCustom.java │ │ │ └── ExceptionHandle.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ └── utils │ │ │ └── ResponseUtill.java │ └── resources │ │ ├── config │ │ ├── application-prod.properties │ │ └── application.properties │ │ ├── public │ │ └── blog.html │ │ ├── static │ │ ├── css │ │ │ ├── blog.css │ │ │ ├── dashboard.css │ │ │ └── floating-labels.css │ │ ├── favicon.ico │ │ └── js │ │ │ └── holder.min.js │ │ └── templates │ │ ├── error │ │ └── 4xx.html │ │ ├── login.html │ │ ├── main.html │ │ └── user.html │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── HelloApplicationTests.java │ ├── LogbackTest.java │ └── SpringbootWebTemplateApplicationTests.java ├── springboot-web-https ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── net │ │ └── codingme │ │ └── https │ │ ├── SpringbootHttpsApplication.java │ │ ├── config │ │ └── Http2Https.java │ │ └── controller │ │ └── HttpsController.java │ └── resources │ ├── application.yml │ └── tomcat_https.keystore ├── springboot-web-interceptor ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ ├── LogAspect.java │ │ │ ├── LogHandlerInterceptor.java │ │ │ └── WebMvcConfig.java │ │ │ ├── controller │ │ │ ├── HelloController.java │ │ │ └── UserController.java │ │ │ ├── domain │ │ │ ├── Response.java │ │ │ └── User.java │ │ │ ├── enums │ │ │ └── ResponseEnum.java │ │ │ ├── exception │ │ │ ├── BaseException.java │ │ │ ├── ErrorAttributesCustom.java │ │ │ └── ExceptionHandle.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ └── utils │ │ │ └── ResponseUtill.java │ └── resources │ │ ├── config │ │ ├── application-prod.properties │ │ └── application.properties │ │ ├── public │ │ └── blog.html │ │ ├── static │ │ ├── css │ │ │ ├── blog.css │ │ │ ├── dashboard.css │ │ │ └── floating-labels.css │ │ ├── favicon.ico │ │ └── js │ │ │ └── holder.min.js │ │ └── templates │ │ ├── error │ │ └── 4xx.html │ │ ├── login.html │ │ ├── main.html │ │ └── user.html │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── HelloApplicationTests.java │ ├── LogbackTest.java │ └── SpringbootWebTemplateApplicationTests.java ├── springboot-web-servlet-filter-listener ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ └── WebCoreConfig.java │ │ │ ├── filter │ │ │ └── MyFilter.java │ │ │ ├── listener │ │ │ └── MyListener.java │ │ │ └── servlet │ │ │ └── MyServlet.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ └── SpringbootWebServletFilterListenerApplicationTests.java ├── springboot-web-staticfile ├── .gitignore ├── pom.xml └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── codingme │ │ │ └── boot │ │ │ ├── BootApplication.java │ │ │ ├── config │ │ │ └── WebMvcConfig.java │ │ │ ├── controller │ │ │ ├── HelloController.java │ │ │ └── PersonController.java │ │ │ ├── domain │ │ │ ├── Person.java │ │ │ └── Response.java │ │ │ ├── service │ │ │ └── PersonService.java │ │ │ └── utils │ │ │ └── ResponseUtill.java │ └── resources │ │ ├── config │ │ ├── application-prod.properties │ │ └── application.properties │ │ ├── public │ │ ├── blog.html │ │ └── login.html │ │ └── static │ │ ├── css │ │ ├── blog.css │ │ └── floating-labels.css │ │ ├── favicon.ico │ │ └── js │ │ └── holder.min.js │ └── test │ └── java │ └── net │ └── codingme │ └── boot │ ├── HelloApplicationTests.java │ └── LogbackTest.java ├── springboot-web-swagger ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── net │ │ └── codingme │ │ └── boot │ │ ├── BootApplication.java │ │ ├── config │ │ ├── ConverterDate.java │ │ ├── SwaggerConfig.java │ │ └── WebMvcConfig.java │ │ ├── controller │ │ └── UserController.java │ │ ├── domain │ │ ├── Response.java │ │ └── User.java │ │ ├── enums │ │ └── ResponseEnum.java │ │ └── utils │ │ └── ResponseUtill.java │ └── resources │ ├── application.properties │ ├── css │ └── floating-labels.css │ └── templates │ ├── error │ └── 4xx.html │ ├── login.html │ └── user.html └── springboot-web-template ├── .gitignore ├── pom.xml └── src ├── main ├── java │ └── net │ │ └── codingme │ │ └── boot │ │ ├── BootApplication.java │ │ ├── config │ │ ├── LogHandlerInterceptor.java │ │ └── WebMvcConfig.java │ │ ├── controller │ │ ├── HelloController.java │ │ └── UserController.java │ │ ├── domain │ │ ├── Response.java │ │ └── User.java │ │ ├── service │ │ ├── UserService.java │ │ └── impl │ │ │ └── UserServiceImpl.java │ │ └── utils │ │ └── ResponseUtill.java └── resources │ ├── config │ ├── application-prod.properties │ └── application.properties │ ├── public │ └── blog.html │ ├── static │ ├── css │ │ ├── blog.css │ │ ├── dashboard.css │ │ └── floating-labels.css │ ├── favicon.ico │ └── js │ │ └── holder.min.js │ └── templates │ ├── login.html │ ├── main.html │ └── user.html └── test └── java └── net └── codingme └── boot ├── HelloApplicationTests.java ├── LogbackTest.java └── SpringbootWebTemplateApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | */systemlog/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-admin/springboot-admin-client/src/main/java/net/codingme/boot/config/SecuritySecureConfig.java: -------------------------------------------------------------------------------- 1 | //package net.codingme.boot.config; 2 | // 3 | //import org.springframework.context.annotation.Configuration; 4 | //import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 5 | //import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | //import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 7 | //import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 8 | // 9 | //@Configuration(proxyBeanMethods = false) 10 | //public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { 11 | // 12 | // /** 13 | // * 配置用户名和密码 14 | // * 15 | // * @param auth 16 | // * @throws Exception 17 | // */ 18 | // @Override 19 | // protected void configure(AuthenticationManagerBuilder auth) throws Exception { 20 | // auth.inMemoryAuthentication().withUser("user").password("{noop}clientpassword").roles("USER"); 21 | // } 22 | // 23 | // 24 | //} -------------------------------------------------------------------------------- /springboot-admin/springboot-admin-client/src/main/java/net/codingme/boot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | public interface UserService {} 4 | -------------------------------------------------------------------------------- /springboot-admin/springboot-admin-client/src/main/java/net/codingme/boot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.service.UserService; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service 7 | public class UserServiceImpl implements UserService {} 8 | -------------------------------------------------------------------------------- /springboot-admin/springboot-admin-server/src/main/java/net/codingme/admin/server/config/CustomNotifier.java: -------------------------------------------------------------------------------- 1 | package net.codingme.admin.server.config; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | import de.codecentric.boot.admin.server.domain.entities.Instance; 8 | import de.codecentric.boot.admin.server.domain.entities.InstanceRepository; 9 | import de.codecentric.boot.admin.server.domain.events.InstanceEvent; 10 | import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent; 11 | import de.codecentric.boot.admin.server.notify.AbstractEventNotifier; 12 | import de.codecentric.boot.admin.server.notify.LoggingNotifier; 13 | import reactor.core.publisher.Mono; 14 | 15 | @Component 16 | public class CustomNotifier extends AbstractEventNotifier { 17 | 18 | private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class); 19 | 20 | public CustomNotifier(InstanceRepository repository) { 21 | super(repository); 22 | } 23 | 24 | @Override 25 | protected Mono doNotify(InstanceEvent event, Instance instance) { 26 | return Mono.fromRunnable(() -> { 27 | if (event instanceof InstanceStatusChangedEvent) { 28 | LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(), 29 | ((InstanceStatusChangedEvent)event).getStatusInfo().getStatus()); 30 | } else { 31 | LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(), 32 | event.getType()); 33 | } 34 | }); 35 | } 36 | } -------------------------------------------------------------------------------- /springboot-banner/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | -------------------------------------------------------------------------------- /springboot-banner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.3.RELEASE 9 | 10 | 11 | net.codingme 12 | banner 13 | 0.0.1-SNAPSHOT 14 | banner 15 | banner 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | true 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /springboot-banner/src/main/java/net/codingme/banner/BannerApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.banner; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class BannerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(BannerApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-banner/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-banner/src/main/resources/banner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-banner/src/main/resources/banner.jpg -------------------------------------------------------------------------------- /springboot-banner/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | (_) 2 | _ __ _ _ _ _ __ ___ ___ ___ 3 | | '_ \| | | | | '_ ` _ \ / _ \ / _ \ 4 | | | | | | |_| | | | | | | (_) | (_) | 5 | |_| |_|_|\__,_|_| |_| |_|\___/ \___/ 版本:${spring-boot.formatted-version} -------------------------------------------------------------------------------- /springboot-banner/src/test/java/net/codingme/banner/BannerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.banner; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class BannerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() {} 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-config/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.mvn/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.CommandLineRunner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ImportResource; 9 | import org.springframework.context.annotation.PropertySource; 10 | 11 | import java.util.Arrays; 12 | 13 | /** 14 | *

15 | * 16 | * @Author niujinpeng 17 | * @Date 2018/12/7 0:04 18 | */ 19 | 20 | /** 21 | * 用于引入传统的 XML 配置文件 22 | * @ImportResource(value = "classpath:spring-service.xml") 23 | */ 24 | @SpringBootApplication 25 | public class BootApplication { 26 | 27 | public static void main(String[] args) { 28 | SpringApplication.run(BootApplication.class, args); 29 | } 30 | 31 | @Bean 32 | public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 33 | return args -> { 34 | // 开始检查spring boot 提供的 beans 35 | System.out.println("Let's inspect the beans provided by Spring Boot:"); 36 | String[] beanNames = ctx.getBeanDefinitionNames(); 37 | Arrays.sort(beanNames); 38 | for (String beanName : beanNames) { 39 | System.out.println(beanName); 40 | } 41 | }; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/config/ServiceConfig.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import net.codingme.boot.service.HelloService; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | *

9 | * 配置类,相当于传统Spring 开发中的 xml-> bean的配置 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/12/7 0:04 13 | */ 14 | @Configuration 15 | public class ServiceConfig { 16 | 17 | /** 18 | * 默认添加到容器中的 ID 为方法名(helloService) 19 | * 20 | * @return 21 | */ 22 | @Bean 23 | public HelloService helloService() { 24 | return new HelloService(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | *

11 | * 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/4 14:41 15 | */ 16 | @Slf4j 17 | @RestController 18 | public class HelloController { 19 | 20 | @Value("${bootapp.description}") 21 | private String description; 22 | 23 | @RequestMapping("/") 24 | public String index() { 25 | log.info(description); 26 | return "Greetings from Spring Boot!"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/domain/Dog.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | *

11 | * 12 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 13 | * @Component 加到spring 容器中 14 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 15 | * @Author niujinpeng 16 | * @Date 2018/12/6 22:56 17 | */ 18 | 19 | @Data 20 | @Component 21 | @ConfigurationProperties(prefix = "person.dog") 22 | public class Dog { 23 | private String name; 24 | private Integer age; 25 | } 26 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/domain/Person.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.validation.annotation.Validated; 7 | 8 | import javax.validation.constraints.Email; 9 | import java.util.Date; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 17 | * @Component 加到spring 容器中 18 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 19 | * @Author niujinpeng 20 | * @Date 2018/12/6 22:54 21 | */ 22 | 23 | @Data 24 | @Component 25 | @ConfigurationProperties(prefix = "person") 26 | @Validated 27 | public class Person { 28 | 29 | private String lastName; 30 | private Integer age; 31 | private Date birth; 32 | private Map maps; 33 | private List lists; 34 | private Dog dog; 35 | 36 | /** 37 | * 支持数据校验 38 | */ 39 | @Email 40 | private String email; 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/domain/PersonSource.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.context.annotation.PropertySource; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.validation.annotation.Validated; 8 | 9 | import javax.validation.constraints.Email; 10 | import java.util.Date; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | *

16 | * 使用@PropertySource加载自定义的配置文件,需要注意的是,由于@PropertySource指定的文件会优先加载, 17 | * 所以如果在 applocation.properties中存在相同的属性配置,会覆盖前者的值。 18 | * 19 | * @@PropertySource 加载自定义的配置文件 20 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 21 | * @Component 加到spring 容器中 22 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 23 | * @Author niujinpeng 24 | * @Date 2018/12/6 22:54 25 | */ 26 | 27 | @Data 28 | @Component 29 | @Validated 30 | @PropertySource(value = "classpath:domain-person.properties") 31 | @ConfigurationProperties(value = "person") 32 | public class PersonSource { 33 | 34 | private String lastName; 35 | private Integer age; 36 | private Date birth; 37 | private Map maps; 38 | private List lists; 39 | private Dog dog; 40 | 41 | /** 42 | * 支持数据校验 43 | */ 44 | @Email 45 | private String email; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/domain/PersonValue.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.validation.annotation.Validated; 7 | 8 | import javax.validation.constraints.Email; 9 | import java.util.Date; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 17 | * @Component 加到spring 容器中 18 | * @Author niujinpeng 19 | * @Date 2018/12/6 23:21 20 | */ 21 | @Data 22 | @Component 23 | @Validated 24 | public class PersonValue { 25 | 26 | /** 27 | * 直接从配置文件读取一个值 28 | */ 29 | @Value("${person.last-name}") 30 | private String lastName; 31 | 32 | /** 33 | * 支持SpEL表达式 34 | */ 35 | @Value("#{11*4/2}") 36 | private Integer age; 37 | 38 | @Value("${person.birth}") 39 | private Date birth; 40 | 41 | /** 42 | * 不支持复杂类型 43 | */ 44 | private Map maps; 45 | private List lists; 46 | private Dog dog; 47 | 48 | /** 49 | * 不支持数据校验 50 | */ 51 | @Email 52 | @Value("xxx@@@@") 53 | private String email; 54 | } 55 | -------------------------------------------------------------------------------- /springboot-config/src/main/java/net/codingme/boot/service/HelloService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 测试server bean的加载方式 9 | * 1.使用@Service 注解声明 10 | * 2.使用XML配置通过@ImportResource 引入 11 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 12 | * 13 | * @Service 添加到容器中 14 | * @Author niujinpeng 15 | * @Date 2018/12/7 0:05 16 | */ 17 | //@Service 18 | public class HelloService { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ################################################################ 2 | # \u670D\u52A1\u542F\u52A8\u7AEF\u53E3\u53F7 3 | server.port=8080 4 | spring.profiles.active=prod 5 | ################################################################ 6 | # \u914D\u7F6E\u5C5E\u6027\u503C\uFF08\u4F7F\u7528IDE\u8FDB\u884C\u914D\u7F6E\u9700\u8981\u5904\u7406\u7F16\u7801\u95EE\u9898\uFF0C\u4E0D\u7136\u4E2D\u6587\u4F1A\u53D1\u9001\u4E71\u7801\u73B0\u8C61\uFF09 7 | person.last-name=\u5F20\u4E09 8 | person.age=18 9 | person.birth=2018/12/06 10 | person.email=gmail@gmail.com 11 | person.maps.key1=c 12 | person.maps.key2=java 13 | person.maps.key3=golang 14 | person.lists=a,b,c,d 15 | person.dog.name=\u65FA\u8D22 16 | person.dog.age=1 17 | ################################################################ 18 | # \u751F\u6210\u968F\u673A\u503C 19 | bootapp.secret=$ {random.value} 20 | bootapp.number=$ {random.int} 21 | bootapp.bignumber=$ {random.long} 22 | bootapp.uuid=$ {random.uuid} 23 | bootapp.number.less.than.ten=$ {random.int\uFF0810\uFF09} 24 | bootapp.number.in.range=$ {random.int [1024,65536]} 25 | # \u5C5E\u6027\u7684\u5360\u4F4D\u7B26 26 | bootapp.name=SpringBoot 27 | bootapp.description=${bootapp.name}\u662F\u4E00\u4E2Aspring\u5E94\u7528\u7A0B\u5E8F -------------------------------------------------------------------------------- /springboot-config/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | ### 服务启动端口号 2 | ##server: 3 | ## port: 8080 4 | ### 配置person属性值 5 | #person: 6 | # last-name: Darcy 7 | # age: 20 8 | # birth: 2018/01/01 9 | # email: gmail@gmail.com 10 | # maps: 11 | # key1:java 12 | # key2:golang 13 | # lists: 14 | # - a 15 | # - b 16 | # - c 17 | # dog: 18 | # name: 旺财 19 | # age: 2 20 | ## 属性的占位符 21 | #bootapp: 22 | # name: SpringBoot 23 | # description: ${bootapp.name}是一个spring应用程序 24 | #server: 25 | # port: 8083 26 | # profiles: 27 | # active: dev #指定环境为dev 28 | # # 使用三个---进行文档块区分 29 | # 30 | #--- 31 | #server: 32 | # port: 8084 33 | #spring: 34 | # profiles: dev 35 | #--- 36 | #server: 37 | # port: 8085 38 | #spring: 39 | # profiles: prod 40 | -------------------------------------------------------------------------------- /springboot-config/src/main/resources/domain-person.properties: -------------------------------------------------------------------------------- 1 | # \u914D\u7F6E\u5C5E\u6027\u503C\uFF08\u4F7F\u7528IDE\u8FDB\u884C\u914D\u7F6E\u9700\u8981\u5904\u7406\u7F16\u7801\u95EE\u9898\uFF0C\u4E0D\u7136\u4E2D\u6587\u4F1A\u53D1\u9001\u4E71\u7801\u73B0\u8C61\uFF09 2 | person.last-name=\u674E\u56DB 3 | person.age=20 4 | person.birth=2018/12/06 5 | person.email=niu@gmail.com 6 | person.maps.key1=c 7 | person.maps.key2=java 8 | person.maps.key3=golang 9 | person.lists=a,b,c,d 10 | person.dog.name=\u65FA\u8D22 11 | person.dog.age=1 -------------------------------------------------------------------------------- /springboot-config/src/main/resources/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /springboot-data-jdbc/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-data-jdbc/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | *

8 | * Spring Boot 启动类 9 | *

10 | */ 11 | 12 | @SpringBootApplication 13 | public class BootApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(BootApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /springboot-data-jdbc/src/main/java/net/codingme/boot/controller/JdbcController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jdbc.core.JdbcTemplate; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.sql.DataSource; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Author niujinpeng 17 | * @Date 2019/1/15 15:54 18 | */ 19 | @RestController 20 | public class JdbcController { 21 | 22 | 23 | @Autowired 24 | JdbcTemplate jdbcTemplate; 25 | 26 | @ResponseBody 27 | @GetMapping("/query") 28 | public Map map() { 29 | List> list = jdbcTemplate.queryForList("select * FROM user"); 30 | return list.get(0); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-data-jdbc/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | username: root 4 | password: 123 5 | url: jdbc:mysql://127.0.0.1:3306/springboot?characterEncoding=utf-8&serverTimezone=GMT%2B8 6 | driver-class-name: com.mysql.jdbc.Driver 7 | type: com.alibaba.druid.pool.DruidDataSource 8 | 9 | initialSize: 5 10 | minIdle: 5 11 | maxActive: 20 12 | maxWait: 60000 13 | timeBetweenEvictionRunsMillis: 60000 14 | minEvictableIdleTimeMillis: 300000 15 | validationQuery: SELECT 1 FROM DUAL 16 | testWhileIdle: true 17 | testOnBorrow: false 18 | testOnReturn: false 19 | poolPreparedStatements: true 20 | # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 21 | filters: stat 22 | maxPoolPreparedStatementPerConnectionSize: 20 23 | useGlobalDataSourceStat: true 24 | connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 -------------------------------------------------------------------------------- /springboot-data-jdbc/src/test/java/net/codingme/boot/SpringbootDataJdbcApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | import javax.sql.DataSource; 11 | import java.sql.Connection; 12 | import java.sql.SQLException; 13 | import java.sql.Statement; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest 17 | public class SpringbootDataJdbcApplicationTests { 18 | 19 | @Autowired 20 | DataSource dataSource; 21 | 22 | /** 23 | * 测试JDBC数据源 24 | * @throws SQLException 25 | */ 26 | @Test 27 | public void contextLoads() throws SQLException { 28 | System.out.println(dataSource.getClass()); 29 | 30 | Connection connection = dataSource.getConnection(); 31 | System.out.println(connection); 32 | connection.close(); 33 | } 34 | 35 | } 36 | 37 | -------------------------------------------------------------------------------- /springboot-data-jpa/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | *

8 | * Spring Boot 启动类 9 | *

10 | */ 11 | 12 | @SpringBootApplication 13 | public class BootApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(BootApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/config/ConverterDate.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import net.codingme.boot.enums.ResponseEnum; 4 | import net.codingme.boot.exception.BaseException; 5 | import org.springframework.core.convert.converter.Converter; 6 | import org.springframework.util.StringUtils; 7 | 8 | import java.text.ParseException; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | 12 | /** 13 | *

14 | * 15 | * @Author niujinpeng 16 | * @Date 2019/1/11 15:18 17 | */ 18 | public class ConverterDate implements Converter { 19 | 20 | private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 21 | 22 | @Override 23 | public Date convert(String s) { 24 | if (StringUtils.isEmpty(s)) { 25 | return null; 26 | } 27 | try { 28 | return simpleDateFormat.parse(s); 29 | } catch (ParseException e) { 30 | e.printStackTrace(); 31 | } 32 | return null; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/config/LogHandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | *

12 | * 拦截器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/6 16:54 16 | */ 17 | @Slf4j 18 | public class LogHandlerInterceptor implements HandlerInterceptor { 19 | 20 | /** 21 | * 请求方法执行之前 22 | * 返回true则通过 23 | * 24 | * @param request 25 | * @param response 26 | * @param handler 27 | * @return 28 | */ 29 | @Override 30 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 31 | StringBuffer requestURL = request.getRequestURL(); 32 | log.info("请求URL:" + requestURL.toString()); 33 | return true; 34 | } 35 | 36 | /** 37 | * 返回modelAndView之前执行 38 | * @param request 39 | * @param response 40 | * @param handler 41 | * @param modelAndView 42 | */ 43 | @Override 44 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { 45 | } 46 | 47 | /** 48 | * 执行Handler完成执行此方法 49 | * @param request 50 | * @param response 51 | * @param handler 52 | * @param ex 53 | */ 54 | @Override 55 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.utils.ResponseUtill; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | *

13 | * 测试控制器 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/4 14:41 17 | */ 18 | @Slf4j 19 | @RestController 20 | public class HelloController { 21 | 22 | @Value("${bootapp.description}") 23 | private String description; 24 | 25 | @RequestMapping("/") 26 | public String index() { 27 | log.info(description); 28 | return "Greetings from Spring Boot!"; 29 | } 30 | 31 | @RequestMapping(value = "/er") 32 | @ResponseBody 33 | public Response error(Integer num) throws Exception { 34 | num = num / num; 35 | return ResponseUtill.success(num); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | import java.util.Collection; 9 | 10 | 11 | /** 12 | *

13 | * 响应实体类封装 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/19 17:13 17 | */ 18 | 19 | @Data 20 | @AllArgsConstructor 21 | @NoArgsConstructor 22 | public class Response { 23 | /** 24 | * 响应码 25 | */ 26 | private String code; 27 | /** 28 | * 响应信息 29 | */ 30 | private String message; 31 | 32 | /** 33 | * 响应数据 34 | */ 35 | private Collection data; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/domain/User.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | import org.springframework.format.annotation.DateTimeFormat; 8 | 9 | import javax.persistence.*; 10 | import javax.validation.constraints.NotNull; 11 | import java.util.Date; 12 | 13 | /** 14 | *

15 | * 16 | * @Entity JPA实体 17 | * @Data GET SET TOSTRING 18 | * @NoArgsConstructor 无参构造 19 | * @AllArgsConstructor 全参构造 20 | * @Author niujinpeng 21 | * @Date 2018/12/19 17:13 22 | */ 23 | @Data 24 | @NoArgsConstructor 25 | @AllArgsConstructor 26 | @Entity 27 | @Table(name = "user") 28 | public class User { 29 | 30 | /** 31 | * 用户ID 32 | * 33 | * @Id 主键 34 | * @GeneratedValue 自增主键 35 | */ 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.IDENTITY) 38 | private Integer id; 39 | 40 | /** 41 | * 用户名 42 | */ 43 | @Column(name = "username", length = 32, nullable = false) 44 | @NotNull(message = "用户名不能为空") 45 | private String username; 46 | /** 47 | * 密码 48 | */ 49 | @Column(name = "password", length = 32, nullable = false) 50 | @NotNull(message = "密码不能为空") 51 | private String password; 52 | /** 53 | * 年龄 54 | */ 55 | @Column(name = "age", length = 3) 56 | private Integer age; 57 | /** 58 | * 生日 59 | */ 60 | @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") 61 | private Date birthday; 62 | /** 63 | * 技能 64 | */ 65 | private String skills; 66 | } 67 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/domain/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.repository; 2 | 3 | import net.codingme.boot.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | /** 8 | *

9 | * 10 | * @Author niujinpeng 11 | * @Date 2019/1/1114:26 12 | */ 13 | @Repository 14 | public interface UserRepository extends JpaRepository { 15 | 16 | /** 17 | * 一个自定义方法,根据 username 和 password 查询 User 信息 18 | */ 19 | User findByUsernameAndPassword(String username, String password); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/enums/ResponseEnum.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.enums; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | *

9 | * 用于系统返回的枚举类 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/8/24 14:30 13 | */ 14 | public enum ResponseEnum { 15 | 16 | /** 17 | * 系统总体相关 18 | */ 19 | SUCCESS("0", "操作成功"), 20 | UNKNOW_ERROR("-1", "未知错误"), 21 | ERROR("999", "请求处理失败"), 22 | LOGIN_FIELD("1009", "用户名或者密码错误"); 23 | 24 | @Setter 25 | @Getter 26 | private String code; 27 | 28 | @Setter 29 | @Getter 30 | private String message; 31 | 32 | ResponseEnum(String code, String msg) { 33 | this.code = code; 34 | this.message = msg; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/exception/BaseException.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import net.codingme.boot.enums.ResponseEnum; 7 | 8 | /** 9 | *

10 | * 自定义异常类 11 | * 12 | * @Author niujinpeng 13 | * @Date 2018/8/24 14:39 14 | */ 15 | @Getter 16 | @Setter 17 | public class BaseException extends RuntimeException { 18 | 19 | private String code; 20 | 21 | public BaseException(String message, String code) { 22 | super(message); 23 | this.code = code; 24 | } 25 | 26 | public BaseException(ResponseEnum resultEnum) { 27 | super(resultEnum.getMessage()); 28 | this.code = resultEnum.getCode(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/exception/ErrorAttributesCustom.java: -------------------------------------------------------------------------------- 1 | //package net.codingme.boot.exception; 2 | // 3 | //import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; 4 | //import org.springframework.stereotype.Component; 5 | //import org.springframework.web.context.request.WebRequest; 6 | // 7 | //import java.util.HashMap; 8 | //import java.util.Map; 9 | // 10 | ///** 11 | // *

12 | // * 自定义错误信息JSON值 13 | // * 14 | // * @Author niujinpeng 15 | // * @Date 2019/1/7 15:21 16 | // */ 17 | //@Component 18 | //public class ErrorAttributesCustom extends DefaultErrorAttributes { 19 | // 20 | // @Override 21 | // public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { 22 | // Map map = super.getErrorAttributes(webRequest, includeStackTrace); 23 | // String code = map.get("status").toString(); 24 | // String message = map.get("error").toString(); 25 | // map.remove("status"); 26 | // map.remove("error"); 27 | // HashMap hashMap = new HashMap<>(); 28 | // hashMap.put("code", code); 29 | // hashMap.put("message", message); 30 | // hashMap.put("data", map); 31 | // return hashMap; 32 | // } 33 | //} 34 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/exception/ExceptionHandle.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.enums.ResponseEnum; 6 | import net.codingme.boot.utils.ResponseUtill; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | 13 | /** 14 | *

15 | * 统一的异常处理 16 | * 17 | * @Author niujinpeng 18 | * @Date 2019/1/7 14:26 19 | */ 20 | 21 | @Slf4j 22 | @ControllerAdvice 23 | public class ExceptionHandle { 24 | 25 | @ResponseBody 26 | @ExceptionHandler(Exception.class) 27 | public Response handleException(Exception e) { 28 | if (e instanceof BaseException) { 29 | BaseException exception = (BaseException) e; 30 | String code = exception.getCode(); 31 | String message = exception.getMessage(); 32 | return ResponseUtill.error(code, message); 33 | } 34 | log.info("未知异常",e); 35 | return ResponseUtill.error(ResponseEnum.UNKNOW_ERROR); 36 | } 37 | 38 | 39 | /** 40 | * 判断是否是Ajax请求 41 | * 42 | * @param request 43 | * @return 44 | */ 45 | public static boolean isAjax(HttpServletRequest request) { 46 | String rquested = request.getHeader("X-Rquested-With"); 47 | if (request == null) { 48 | return false; 49 | } 50 | if (!"XMLHttpRequest".equals(rquested.toString())) { 51 | return false; 52 | } 53 | return true; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import net.codingme.boot.domain.User; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | *

10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/12/7 0:05 13 | */ 14 | 15 | public interface UserService { 16 | 17 | void create(User user) throws Exception; 18 | 19 | List findAll() throws Exception; 20 | 21 | User login(User user) throws Exception; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.domain.User; 4 | import net.codingme.boot.domain.repository.UserRepository; 5 | import net.codingme.boot.enums.ResponseEnum; 6 | import net.codingme.boot.exception.BaseException; 7 | import net.codingme.boot.service.UserService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | *

15 | * 测试server bean的加载方式 16 | * 1.使用@Service 注解声明 17 | * 2.使用XML配置通过@ImportResource 引入 18 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 19 | * 20 | * @Service 添加到容器中 21 | * @Author niujinpeng 22 | * @Date 2018/12/27 15:59 23 | */ 24 | 25 | @Service 26 | public class UserServiceImpl implements UserService { 27 | 28 | @Autowired 29 | private UserRepository userRepository; 30 | 31 | @Override 32 | public void create(User user) throws Exception { 33 | userRepository.save(user); 34 | } 35 | 36 | @Override 37 | public List findAll() throws Exception { 38 | return userRepository.findAll(); 39 | } 40 | 41 | @Override 42 | public User login(User user) throws Exception { 43 | String username = user.getUsername(); 44 | String password = user.getPassword(); 45 | User loginUser = userRepository.findByUsernameAndPassword(username, password); 46 | if (loginUser == null) { 47 | throw new BaseException(ResponseEnum.LOGIN_FIELD); 48 | } 49 | return user; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | import net.codingme.boot.enums.ResponseEnum; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | 9 | /** 10 | *

11 | * 方便响应的工具类 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/19 17:33 15 | */ 16 | public class ResponseUtill { 17 | 18 | public static Response success(Object objects) { 19 | ArrayList list = new ArrayList<>(); 20 | if (!(objects instanceof Collection)) { 21 | list.add(objects); 22 | } else { 23 | list = (ArrayList) objects; 24 | } 25 | return new Response("0000", "success", list); 26 | } 27 | 28 | public static Response success() { 29 | return success(new ArrayList<>()); 30 | } 31 | 32 | public static Response error(String code, String message) { 33 | return new Response(code, message, new ArrayList<>()); 34 | } 35 | 36 | public static Response error(ResponseEnum responseEnum) { 37 | return error(responseEnum.getCode(), responseEnum.getMessage()); 38 | } 39 | 40 | public static Response error(ResponseEnum responseEnum, String message) { 41 | return error(responseEnum.getCode(), message); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/resources/templates/error/4xx.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | [[${status}]] 7 | 8 | 9 | 10 | 11 |

12 |

错误码:[[${status}]]

13 |

信息:[[${error}]]

14 |

时间:[[${#dates.format(timestamp,'yyyy-MM-dd hh:mm:ss ')}]]

15 |

请求路径:[[${path}]]

16 |
17 | 18 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/main/resources/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Floating labels example for Bootstrap 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 |
22 | 23 |

欢迎

24 |

25 |
26 | 27 |
28 | 29 |
30 | 31 |
32 | 33 |
34 | 35 |
36 | 39 |
40 | 41 |

© 2018-2019

42 |
43 | 44 | 45 | -------------------------------------------------------------------------------- /springboot-data-jpa/src/test/java/net/codingme/boot/SpringbootDataJpaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import net.codingme.boot.domain.User; 5 | import net.codingme.boot.domain.repository.UserRepository; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | import javax.sql.DataSource; 13 | import java.sql.SQLException; 14 | import java.util.List; 15 | 16 | @RunWith(SpringRunner.class) 17 | @SpringBootTest 18 | public class SpringbootDataJpaApplicationTests { 19 | 20 | @Autowired 21 | private DataSource dataSource; 22 | 23 | @Autowired 24 | private UserRepository userRepository; 25 | 26 | @Test 27 | public void testUser(){ 28 | List one = userRepository.findAll(); 29 | System.out.println(JSON.toJSONString(one)); 30 | } 31 | 32 | 33 | @Test 34 | public void contextLoads() throws SQLException { 35 | System.out.println(dataSource.getClass()); 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-data-mybatis-multiple-datasource/src/.DS_Store -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-data-mybatis-multiple-datasource/src/main/.DS_Store -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/BootApplication.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | /** 8 | *

9 | * Spring Boot 启动类 10 | *

11 | */ 12 | @SpringBootApplication 13 | public class BootApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(BootApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.controller; 2 | 3 | import java.util.List; 4 | 5 | import com.wdbyte.domain.Book; 6 | import com.wdbyte.service.BookService; 7 | import com.wdbyte.domain.Response; 8 | import com.wdbyte.utils.ResponseUtill; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | /** 14 | *

15 | * 16 | * @Author niujinpeng 17 | * @Date 2019/1/21 14:16 18 | */ 19 | @RestController 20 | public class BookController { 21 | 22 | @Autowired 23 | private BookService bookService; 24 | 25 | @GetMapping(value = "/books") 26 | public Response selectAll() throws Exception { 27 | List books = bookService.selectAll(); 28 | return ResponseUtill.success(books); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.controller; 2 | 3 | import com.wdbyte.domain.Response; 4 | import com.wdbyte.utils.ResponseUtill; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | *

13 | * 测试控制器 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/4 14:41 17 | */ 18 | @Slf4j 19 | @RestController 20 | public class HelloController { 21 | 22 | @Value("${bootapp.description}") 23 | private String description; 24 | 25 | @RequestMapping("/") 26 | public String index() { 27 | log.info(description); 28 | return "Greetings from Spring Boot!"; 29 | } 30 | 31 | @RequestMapping(value = "/er") 32 | @ResponseBody 33 | public Response error(Integer num) throws Exception { 34 | num = num / num; 35 | return ResponseUtill.success(num); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.controller; 2 | 3 | import java.util.List; 4 | 5 | import com.wdbyte.domain.Response; 6 | import com.wdbyte.domain.User; 7 | import com.wdbyte.service.UserService; 8 | import com.wdbyte.utils.ResponseUtill; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | /** 15 | *

16 | * 17 | * @Author niujinpeng 18 | * @Date 2018/12/19 17:17 19 | */ 20 | @RestController 21 | public class UserController { 22 | 23 | @Autowired 24 | private UserService userService; 25 | 26 | @ResponseBody 27 | @GetMapping(value = "/users") 28 | public Response selectAll() { 29 | List userList = userService.selectAll(); 30 | return ResponseUtill.success(userList); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/domain/Response.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Collection; 8 | 9 | 10 | /** 11 | *

12 | * 响应实体类封装 13 | * 14 | * @Author niujinpeng 15 | * @Date 2018/12/19 17:13 16 | */ 17 | 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | public class Response { 22 | /** 23 | * 响应码 24 | */ 25 | private String code; 26 | /** 27 | * 响应信息 28 | */ 29 | private String message; 30 | 31 | /** 32 | * 响应数据 33 | */ 34 | private Collection data; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/enums/ResponseEnum.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.enums; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | *

9 | * 用于系统返回的枚举类 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/8/24 14:30 13 | */ 14 | public enum ResponseEnum { 15 | /** 16 | * 系统总体相关 17 | */ 18 | SUCCESS("0", "操作成功"), 19 | UNKNOW_ERROR("-1", "未知错误"), 20 | ERROR("999", "请求处理失败"), 21 | LOGIN_FIELD("1009", "用户名或者密码错误"); 22 | 23 | @Setter 24 | @Getter 25 | private String code; 26 | 27 | @Setter 28 | @Getter 29 | private String message; 30 | 31 | ResponseEnum(String code, String msg) { 32 | this.code = code; 33 | this.message = msg; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/mapper/datasource2/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.mapper.datasource2; 2 | 3 | import com.wdbyte.domain.User; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import java.util.List; 7 | 8 | @Repository 9 | public interface UserMapper { 10 | /** 11 | * This method was generated by MyBatis Generator. 12 | * This method corresponds to the database table User 13 | * 14 | * @mbg.generated Sat Dec 19 16:25:25 CST 2020 15 | */ 16 | int deleteByPrimaryKey(Integer id); 17 | 18 | /** 19 | * This method was generated by MyBatis Generator. 20 | * This method corresponds to the database table User 21 | * 22 | * @mbg.generated Sat Dec 19 16:25:25 CST 2020 23 | */ 24 | int insert(User record); 25 | 26 | /** 27 | * This method was generated by MyBatis Generator. 28 | * This method corresponds to the database table User 29 | * 30 | * @mbg.generated Sat Dec 19 16:25:25 CST 2020 31 | */ 32 | User selectByPrimaryKey(Integer id); 33 | 34 | /** 35 | * This method was generated by MyBatis Generator. 36 | * This method corresponds to the database table User 37 | * 38 | * @mbg.generated Sat Dec 19 16:25:25 CST 2020 39 | */ 40 | List selectAll(); 41 | 42 | /** 43 | * This method was generated by MyBatis Generator. 44 | * This method corresponds to the database table User 45 | * 46 | * @mbg.generated Sat Dec 19 16:25:25 CST 2020 47 | */ 48 | int updateByPrimaryKey(User record); 49 | } -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/mapper/primary/BookMapper.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.mapper.primary; 2 | 3 | import com.wdbyte.domain.Book; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import java.util.List; 7 | 8 | @Repository 9 | public interface BookMapper { 10 | /** 11 | * This method was generated by MyBatis Generator. 12 | * This method corresponds to the database table Book 13 | * 14 | * @mbg.generated Sat Dec 19 16:22:25 CST 2020 15 | */ 16 | int deleteByPrimaryKey(Integer id); 17 | 18 | /** 19 | * This method was generated by MyBatis Generator. 20 | * This method corresponds to the database table Book 21 | * 22 | * @mbg.generated Sat Dec 19 16:22:25 CST 2020 23 | */ 24 | int insert(Book record); 25 | 26 | /** 27 | * This method was generated by MyBatis Generator. 28 | * This method corresponds to the database table Book 29 | * 30 | * @mbg.generated Sat Dec 19 16:22:25 CST 2020 31 | */ 32 | Book selectByPrimaryKey(Integer id); 33 | 34 | /** 35 | * This method was generated by MyBatis Generator. 36 | * This method corresponds to the database table Book 37 | * 38 | * @mbg.generated Sat Dec 19 16:22:25 CST 2020 39 | */ 40 | List selectAll(); 41 | 42 | /** 43 | * This method was generated by MyBatis Generator. 44 | * This method corresponds to the database table Book 45 | * 46 | * @mbg.generated Sat Dec 19 16:22:25 CST 2020 47 | */ 48 | int updateByPrimaryKey(Book record); 49 | } -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.service; 2 | 3 | import java.util.List; 4 | 5 | import com.wdbyte.domain.Book; 6 | 7 | /** 8 | * @author niujinpeng 9 | * @website: https://www.wdbyte.com 10 | * @date 2020/12/19 11 | */ 12 | public interface BookService { 13 | List selectAll(); 14 | } 15 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.service; 2 | 3 | import java.util.List; 4 | 5 | import com.wdbyte.domain.User; 6 | 7 | /** 8 | * @author niujinpeng 9 | * @website: https://www.wdbyte.com 10 | * @date 2020/12/19 11 | */ 12 | public interface UserService { 13 | 14 | List selectAll(); 15 | } 16 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.service.impl; 2 | 3 | import java.util.List; 4 | 5 | import com.wdbyte.domain.Book; 6 | import com.wdbyte.mapper.primary.BookMapper; 7 | import com.wdbyte.service.BookService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | /** 12 | * @author niujinpeng 13 | * @website: https://www.wdbyte.com 14 | * @date 2020/12/19 15 | */ 16 | @Service 17 | public class BookServiceImpl implements BookService { 18 | 19 | @Autowired 20 | private BookMapper bookMapper; 21 | 22 | @Override 23 | public List selectAll() { 24 | return bookMapper.selectAll(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.service.impl; 2 | 3 | import java.util.List; 4 | 5 | import com.wdbyte.domain.User; 6 | import com.wdbyte.mapper.datasource2.UserMapper; 7 | import com.wdbyte.service.UserService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | /** 12 | * @author niujinpeng 13 | * @website: https://www.wdbyte.com 14 | * @date 2020/12/19 15 | */ 16 | @Service 17 | public class UserServiceImpl implements UserService { 18 | 19 | @Autowired 20 | private UserMapper userMapper; 21 | 22 | @Override 23 | public List selectAll() { 24 | return userMapper.selectAll(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/utils/MybatisGenerator.java: -------------------------------------------------------------------------------- 1 | //package com.wdbyte.utils; 2 | // 3 | //import org.mybatis.generator.api.MyBatisGenerator; 4 | //import org.mybatis.generator.config.Configuration; 5 | //import org.mybatis.generator.config.xml.ConfigurationParser; 6 | //import org.mybatis.generator.internal.DefaultShellCallback; 7 | // 8 | //import java.io.File; 9 | //import java.util.ArrayList; 10 | // 11 | ///** 12 | // *

13 | // * Mybatis generator的逆向生成工具类 14 | // * 15 | // * @Author niujinpeng 16 | // * @Date 2018/8/30 22:57 17 | // */ 18 | //public class MybatisGenerator { 19 | // 20 | // public void generator() throws Exception { 21 | // ArrayList warnings = new ArrayList<>(); 22 | // boolean overwrite = true; 23 | // // 指定你想工程配置文件 24 | // File configFile = new File("generatorConfig2.xml"); 25 | // System.out.println(configFile.getAbsolutePath()); 26 | // ConfigurationParser cp = new ConfigurationParser(warnings); 27 | // Configuration config = cp.parseConfiguration(configFile); 28 | // DefaultShellCallback callback = new DefaultShellCallback(overwrite); 29 | // MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 30 | // myBatisGenerator.generate(null); 31 | // } 32 | // 33 | // public static void main(String[] args) throws Exception { 34 | // MybatisGenerator mybatisGenerator = new MybatisGenerator(); 35 | // mybatisGenerator.generator(); 36 | // 37 | // } 38 | //} 39 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/java/com/wdbyte/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.utils; 2 | 3 | import com.wdbyte.domain.Response; 4 | import com.wdbyte.enums.ResponseEnum; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | 9 | /** 10 | *

11 | * 方便响应的工具类 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/19 17:33 15 | */ 16 | public class ResponseUtill { 17 | 18 | public static Response success(Object objects) { 19 | ArrayList list = new ArrayList<>(); 20 | if (!(objects instanceof Collection)) { 21 | list.add(objects); 22 | } else { 23 | list = (ArrayList) objects; 24 | } 25 | return new Response("0000", "success", list); 26 | } 27 | 28 | public static Response success() { 29 | return success(new ArrayList<>()); 30 | } 31 | 32 | public static Response error(String code, String message) { 33 | return new Response(code, message, new ArrayList<>()); 34 | } 35 | 36 | public static Response error(ResponseEnum responseEnum) { 37 | return error(responseEnum.getCode(), responseEnum.getMessage()); 38 | } 39 | 40 | public static Response error(ResponseEnum responseEnum, String message) { 41 | return error(responseEnum.getCode(), message); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/resources/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-data-mybatis-multiple-datasource/src/main/resources/.DS_Store -------------------------------------------------------------------------------- /springboot-data-mybatis-multiple-datasource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ############################################################ 2 | # \u5C5E\u6027\u7684\u5360\u4F4D\u7B26 3 | bootapp.name=SpringBoot 4 | bootapp.description=${bootapp.name}\u662F\u4E00\u4E2Aspring\u5E94\u7528\u7A0B\u5E8F 5 | # \u670D\u52A1\u542F\u52A8\u7AEF\u53E3\u53F7 6 | server.port=8080 7 | spring.profiles.active=dev 8 | # \u7F16\u7801 9 | server.tomcat.uri-encoding=utf-8 10 | server.servlet.encoding.force=true 11 | server.servlet.encoding.charset=UTF-8 12 | server.servlet.encoding.enabled=true 13 | ########################## \u4E3B\u6570\u636E\u6E90 ################################## 14 | spring.datasource.primary.jdbc-url=jdbc:mysql://127.0.0.1:3306/demo1?characterEncoding=utf-8&serverTimezone=GMT%2B8 15 | spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver 16 | spring.datasource.primary.username=root 17 | spring.datasource.primary.password= 18 | 19 | ########################## \u7B2C\u4E8C\u4E2A\u6570\u636E\u6E90 ############################### 20 | spring.datasource.datasource2.jdbc-url=jdbc:mysql://127.0.0.1:3306/demo2?characterEncoding=utf-8&serverTimezone=GMT%2B8 21 | spring.datasource.datasource2.driver-class-name=com.mysql.jdbc.Driver 22 | spring.datasource.datasource2.username=root 23 | spring.datasource.datasource2.password= 24 | 25 | # mybatis 26 | mybatis.mapper-locations=classpath:mapper/*.xml 27 | mybatis.type-aliases-package=com.wdbyte.domain 28 | 29 | 30 | -------------------------------------------------------------------------------- /springboot-data-mybatis-page/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | -------------------------------------------------------------------------------- /springboot-data-mybatis-page/src/main/java/net/codingme/boot/MybatisPageApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.transaction.annotation.EnableTransactionManagement; 6 | 7 | @tk.mybatis.spring.annotation.MapperScan(basePackages = "net.codingme.boot.domain.mapper") 8 | @SpringBootApplication 9 | public class MybatisPageApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MybatisPageApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-data-mybatis-page/src/main/java/net/codingme/boot/domain/mapper/BookMapper.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.mapper; 2 | 3 | import net.codingme.boot.domain.Book; 4 | import org.springframework.stereotype.Repository; 5 | import tk.mybatis.mapper.common.Mapper; 6 | import tk.mybatis.mapper.common.MySqlMapper; 7 | 8 | @Repository 9 | public interface BookMapper extends MySqlMapper, Mapper { 10 | 11 | } -------------------------------------------------------------------------------- /springboot-data-mybatis-page/src/main/java/net/codingme/boot/util/MybatisGenerator.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.util; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | 6 | import org.mybatis.generator.api.MyBatisGenerator; 7 | import org.mybatis.generator.config.Configuration; 8 | import org.mybatis.generator.config.xml.ConfigurationParser; 9 | import org.mybatis.generator.internal.DefaultShellCallback; 10 | 11 | /** 12 | *

13 | * Mybatis generator的逆向生成工具类 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/8/30 22:57 17 | */ 18 | public class MybatisGenerator { 19 | 20 | public void generator() throws Exception { 21 | ArrayList warnings = new ArrayList<>(); 22 | boolean overwrite = true; 23 | // 指定你想工程配置文件 24 | File configFile = new File("src/main/resources/generator/generatorConfig.xml"); 25 | System.out.println(configFile.getAbsolutePath()); 26 | ConfigurationParser cp = new ConfigurationParser(warnings); 27 | Configuration config = cp.parseConfiguration(configFile); 28 | DefaultShellCallback callback = new DefaultShellCallback(overwrite); 29 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 30 | myBatisGenerator.generate(null); 31 | for (String warning : warnings) { 32 | System.out.println(warning); 33 | } 34 | } 35 | 36 | public static void main(String[] args) throws Exception { 37 | MybatisGenerator mybatisGenerator = new MybatisGenerator(); 38 | mybatisGenerator.generator(); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /springboot-data-mybatis-page/src/main/java/net/codingme/boot/util/MybatisMapper.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.util; 2 | 3 | import tk.mybatis.mapper.common.Mapper; 4 | import tk.mybatis.mapper.common.MySqlMapper; 5 | 6 | /** 7 | *

8 | * 继承自己的MyMapper 9 | * 10 | * @Author niujinpeng 11 | * @Date 2018/8/30 23:16 12 | */ 13 | public interface MybatisMapper extends Mapper, MySqlMapper { 14 | // todo 15 | // 特别注意,这个接口不能被扫描到,否则会报错 16 | } 17 | -------------------------------------------------------------------------------- /springboot-data-mybatis-page/src/main/resources/mapper/BookMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /springboot-data-mybatis-page/src/test/java/net/codingme/boot/SpringbootDataMybatisPageApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootDataMybatisPageApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() {} 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-data-mybatis/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | /** 8 | *

9 | * Spring Boot 启动类 10 | *

11 | */ 12 | 13 | @SpringBootApplication 14 | @MapperScan("net.codingme.boot.domain.mapper") 15 | public class BootApplication { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(BootApplication.class, args); 19 | } 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/config/ConverterDate.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import org.springframework.core.convert.converter.Converter; 4 | import org.springframework.util.StringUtils; 5 | 6 | import java.text.ParseException; 7 | import java.text.SimpleDateFormat; 8 | import java.util.Date; 9 | 10 | /** 11 | *

12 | * 时间转换器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/11 15:18 16 | */ 17 | public class ConverterDate implements Converter { 18 | 19 | private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 20 | 21 | @Override 22 | public Date convert(String s) { 23 | if (StringUtils.isEmpty(s)) { 24 | return null; 25 | } 26 | try { 27 | return simpleDateFormat.parse(s); 28 | } catch (ParseException e) { 29 | e.printStackTrace(); 30 | } 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/config/LogHandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | *

12 | * 拦截器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/6 16:54 16 | */ 17 | @Slf4j 18 | public class LogHandlerInterceptor implements HandlerInterceptor { 19 | 20 | /** 21 | * 请求方法执行之前 22 | * 返回true则通过 23 | * 24 | * @param request 25 | * @param response 26 | * @param handler 27 | * @return 28 | */ 29 | @Override 30 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 31 | StringBuffer requestURL = request.getRequestURL(); 32 | log.info("请求URL:" + requestURL.toString()); 33 | return true; 34 | } 35 | 36 | /** 37 | * 返回modelAndView之前执行 38 | * @param request 39 | * @param response 40 | * @param handler 41 | * @param modelAndView 42 | */ 43 | @Override 44 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { 45 | } 46 | 47 | /** 48 | * 执行Handler完成执行此方法 49 | * @param request 50 | * @param response 51 | * @param handler 52 | * @param ex 53 | */ 54 | @Override 55 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import springfox.documentation.builders.ApiInfoBuilder; 6 | import springfox.documentation.builders.PathSelectors; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.service.ApiInfo; 9 | import springfox.documentation.spi.DocumentationType; 10 | import springfox.documentation.spring.web.plugins.Docket; 11 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 | 13 | /** 14 | *

15 | * 16 | * @Author niujinpeng 17 | * @Date 2019/1/11 17:44 18 | */ 19 | @Configuration 20 | @EnableSwagger2 21 | public class SwaggerConfig { 22 | 23 | @Bean 24 | public Docket createRestApi() { 25 | return new Docket(DocumentationType.SWAGGER_2) 26 | .apiInfo(apiInfo()) 27 | .select() 28 | .apis(RequestHandlerSelectors.basePackage("net.codingme.boot.controller")) 29 | .paths(PathSelectors.any()) 30 | .build(); 31 | } 32 | 33 | 34 | private ApiInfo apiInfo() { 35 | return new ApiInfoBuilder() 36 | .title("Springboot-mybatis-apis") 37 | //.termsOfServiceUrl("https://www.codingme.net") 38 | //.contact("niumoo") 39 | .version("1.0") 40 | .build(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import io.swagger.annotations.Api; 4 | import io.swagger.annotations.ApiOperation; 5 | import net.codingme.boot.domain.Book; 6 | import net.codingme.boot.domain.Response; 7 | import net.codingme.boot.service.BookService; 8 | import net.codingme.boot.utils.ResponseUtill; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | import javax.websocket.server.PathParam; 17 | import java.util.List; 18 | 19 | /** 20 | *

21 | * 22 | * @Author niujinpeng 23 | * @Date 2019/1/21 14:16 24 | */ 25 | @RestController 26 | @RequestMapping("/books") 27 | @Api(value = "Mybatis 书籍操作接口", tags = "Mybatis 书籍操作接口") 28 | public class BookController { 29 | 30 | @Autowired 31 | private BookService bookService; 32 | 33 | @GetMapping(value = "") 34 | @ApiOperation(value = "查询所有书籍信息") 35 | public Response selectAll() throws Exception { 36 | List books = bookService.selectAll(); 37 | return ResponseUtill.success(books); 38 | } 39 | 40 | @GetMapping(value = "/author/{author}") 41 | @ApiOperation(value = "根据作者查询书籍信息") 42 | public Response selectByAuthor(@PathVariable("author") String author) throws Exception { 43 | List books = bookService.selectByAuthor(author); 44 | return ResponseUtill.success(books); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import io.swagger.annotations.Api; 4 | import lombok.extern.slf4j.Slf4j; 5 | import net.codingme.boot.domain.Response; 6 | import net.codingme.boot.utils.ResponseUtill; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | *

14 | * 测试控制器 15 | * 16 | * @Author niujinpeng 17 | * @Date 2018/12/4 14:41 18 | */ 19 | @Slf4j 20 | @RestController 21 | @Api(value = "Hello springboot接口") 22 | public class HelloController { 23 | 24 | @Value("${bootapp.description}") 25 | private String description; 26 | 27 | @RequestMapping("/") 28 | public String index() { 29 | log.info(description); 30 | return "Greetings from Spring Boot!"; 31 | } 32 | 33 | @RequestMapping(value = "/er") 34 | @ResponseBody 35 | public Response error(Integer num) throws Exception { 36 | num = num / num; 37 | return ResponseUtill.success(num); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Collection; 8 | 9 | 10 | /** 11 | *

12 | * 响应实体类封装 13 | * 14 | * @Author niujinpeng 15 | * @Date 2018/12/19 17:13 16 | */ 17 | 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | public class Response { 22 | /** 23 | * 响应码 24 | */ 25 | private String code; 26 | /** 27 | * 响应信息 28 | */ 29 | private String message; 30 | 31 | /** 32 | * 响应数据 33 | */ 34 | private Collection data; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/domain/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.repository; 2 | 3 | import net.codingme.boot.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | /** 8 | *

9 | * 10 | * @Author niujinpeng 11 | * @Date 2019/1/1114:26 12 | */ 13 | @Repository 14 | public interface UserRepository extends JpaRepository { 15 | 16 | User findByUsernameAndPassword(String username, String password); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/enums/ResponseEnum.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.enums; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | *

9 | * 用于系统返回的枚举类 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/8/24 14:30 13 | */ 14 | public enum ResponseEnum { 15 | 16 | /** 17 | * 系统总体相关 18 | */ 19 | SUCCESS("0", "操作成功"), 20 | UNKNOW_ERROR("-1", "未知错误"), 21 | ERROR("999", "请求处理失败"), 22 | LOGIN_FIELD("1009", "用户名或者密码错误"); 23 | 24 | @Setter 25 | @Getter 26 | private String code; 27 | 28 | @Setter 29 | @Getter 30 | private String message; 31 | 32 | ResponseEnum(String code, String msg) { 33 | this.code = code; 34 | this.message = msg; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/exception/BaseException.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import net.codingme.boot.enums.ResponseEnum; 7 | 8 | /** 9 | *

10 | * 自定义异常类 11 | * 12 | * @Author niujinpeng 13 | * @Date 2018/8/24 14:39 14 | */ 15 | @Getter 16 | @Setter 17 | public class BaseException extends RuntimeException { 18 | 19 | private String code; 20 | 21 | public BaseException(String message, String code) { 22 | super(message); 23 | this.code = code; 24 | } 25 | 26 | public BaseException(ResponseEnum resultEnum) { 27 | super(resultEnum.getMessage()); 28 | this.code = resultEnum.getCode(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/exception/ErrorAttributesCustom.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception;//package net.codingme.boot.exception; 2 | // 3 | //import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; 4 | //import org.springframework.stereotype.Component; 5 | //import org.springframework.web.context.request.WebRequest; 6 | // 7 | //import java.util.HashMap; 8 | //import java.util.Map; 9 | // 10 | ///** 11 | // *

12 | // * 自定义错误信息JSON值 13 | // * 14 | // * @Author niujinpeng 15 | // * @Date 2019/1/7 15:21 16 | // */ 17 | //@Component 18 | //public class ErrorAttributesCustom extends DefaultErrorAttributes { 19 | // 20 | // @Override 21 | // public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { 22 | // Map map = super.getErrorAttributes(webRequest, includeStackTrace); 23 | // String code = map.get("status").toString(); 24 | // String message = map.get("error").toString(); 25 | // map.remove("status"); 26 | // map.remove("error"); 27 | // HashMap hashMap = new HashMap<>(); 28 | // hashMap.put("code", code); 29 | // hashMap.put("message", message); 30 | // hashMap.put("data", map); 31 | // return hashMap; 32 | // } 33 | //} 34 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/exception/ExceptionHandle.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.enums.ResponseEnum; 6 | import net.codingme.boot.utils.ResponseUtill; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | 13 | /** 14 | *

15 | * 统一的异常处理 16 | * 17 | * @Author niujinpeng 18 | * @Date 2019/1/7 14:26 19 | */ 20 | 21 | @Slf4j 22 | @ControllerAdvice 23 | public class ExceptionHandle { 24 | 25 | @ResponseBody 26 | @ExceptionHandler(Exception.class) 27 | public Response handleException(Exception e) { 28 | if (e instanceof BaseException) { 29 | BaseException exception = (BaseException) e; 30 | String code = exception.getCode(); 31 | String message = exception.getMessage(); 32 | return ResponseUtill.error(code, message); 33 | } 34 | log.info("未知异常",e); 35 | return ResponseUtill.error(ResponseEnum.UNKNOW_ERROR); 36 | } 37 | 38 | 39 | /** 40 | * 判断是否是Ajax请求 41 | * 42 | * @param request 43 | * @return 44 | */ 45 | public static boolean isAjax(HttpServletRequest request) { 46 | String rquested = request.getHeader("X-Rquested-With"); 47 | if (request == null) { 48 | return false; 49 | } 50 | if (!"XMLHttpRequest".equals(rquested.toString())) { 51 | return false; 52 | } 53 | return true; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/service/BookService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | import net.codingme.boot.domain.Book; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | *

9 | * 10 | * @Author niujinpeng 11 | * @Date 2019/1/2114:11 12 | */ 13 | public interface BookService { 14 | 15 | List selectAll() throws Exception; 16 | 17 | Book selectByPrimaryKey(Integer id) throws Exception; 18 | 19 | List selectByAuthor(String author)throws Exception; 20 | } 21 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import net.codingme.boot.domain.User; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | *

10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/12/7 0:05 13 | */ 14 | 15 | public interface UserService { 16 | 17 | void create(User user) throws Exception; 18 | 19 | List findAll() throws Exception; 20 | 21 | User login(String username, String password) throws Exception; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/service/impl/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.domain.Book; 4 | import net.codingme.boot.domain.mapper.BookMapper; 5 | import net.codingme.boot.service.BookService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | *

13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/21 14:12 16 | */ 17 | @Service 18 | public class BookServiceImpl implements BookService { 19 | 20 | @Autowired 21 | private BookMapper bookMapper; 22 | 23 | @Override 24 | public List selectAll() throws Exception { 25 | List books = bookMapper.selectAll(); 26 | return books; 27 | } 28 | 29 | @Override 30 | public Book selectByPrimaryKey(Integer id) throws Exception { 31 | Book book = bookMapper.selectByPrimaryKey(id); 32 | return book; 33 | } 34 | 35 | @Override 36 | public List selectByAuthor(String author) throws Exception { 37 | return bookMapper.selectByAuthor(author); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.domain.User; 4 | import net.codingme.boot.domain.repository.UserRepository; 5 | import net.codingme.boot.enums.ResponseEnum; 6 | import net.codingme.boot.exception.BaseException; 7 | import net.codingme.boot.service.UserService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | *

15 | * 测试server bean的加载方式 16 | * 1.使用@Service 注解声明 17 | * 2.使用XML配置通过@ImportResource 引入 18 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 19 | * 20 | * @Service 添加到容器中 21 | * @Author niujinpeng 22 | * @Date 2018/12/27 15:59 23 | */ 24 | 25 | @Service 26 | public class UserServiceImpl implements UserService { 27 | 28 | @Autowired 29 | private UserRepository userRepository; 30 | 31 | @Override 32 | public void create(User user) throws Exception { 33 | userRepository.save(user); 34 | } 35 | 36 | @Override 37 | public List findAll() throws Exception { 38 | return userRepository.findAll(); 39 | } 40 | 41 | @Override 42 | public User login(String username,String password) throws Exception { 43 | User loginUser = userRepository.findByUsernameAndPassword(username, password); 44 | if (loginUser == null) { 45 | throw new BaseException(ResponseEnum.LOGIN_FIELD); 46 | } 47 | return loginUser; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/utils/MybatisGenerator.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import org.mybatis.generator.api.MyBatisGenerator; 4 | import org.mybatis.generator.config.Configuration; 5 | import org.mybatis.generator.config.xml.ConfigurationParser; 6 | import org.mybatis.generator.internal.DefaultShellCallback; 7 | 8 | import java.io.File; 9 | import java.util.ArrayList; 10 | 11 | /** 12 | *

13 | * Mybatis generator的逆向生成工具类 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/8/30 22:57 17 | */ 18 | public class MybatisGenerator { 19 | 20 | public void generator() throws Exception { 21 | ArrayList warnings = new ArrayList<>(); 22 | boolean overwrite = true; 23 | // 指定你想工程配置文件 24 | File configFile = new File("generatorConfig.xml"); 25 | System.out.println(configFile.getAbsolutePath()); 26 | ConfigurationParser cp = new ConfigurationParser(warnings); 27 | Configuration config = cp.parseConfiguration(configFile); 28 | DefaultShellCallback callback = new DefaultShellCallback(overwrite); 29 | MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 30 | myBatisGenerator.generate(null); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | MybatisGenerator mybatisGenerator = new MybatisGenerator(); 35 | mybatisGenerator.generator(); 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | import net.codingme.boot.enums.ResponseEnum; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | 9 | /** 10 | *

11 | * 方便响应的工具类 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/19 17:33 15 | */ 16 | public class ResponseUtill { 17 | 18 | public static Response success(Object objects) { 19 | ArrayList list = new ArrayList<>(); 20 | if (!(objects instanceof Collection)) { 21 | list.add(objects); 22 | } else { 23 | list = (ArrayList) objects; 24 | } 25 | return new Response("0000", "success", list); 26 | } 27 | 28 | public static Response success() { 29 | return success(new ArrayList<>()); 30 | } 31 | 32 | public static Response error(String code, String message) { 33 | return new Response(code, message, new ArrayList<>()); 34 | } 35 | 36 | public static Response error(ResponseEnum responseEnum) { 37 | return error(responseEnum.getCode(), responseEnum.getMessage()); 38 | } 39 | 40 | public static Response error(ResponseEnum responseEnum, String message) { 41 | return error(responseEnum.getCode(), message); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/resources/templates/error/4xx.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | [[${status}]] 7 | 8 | 9 | 10 | 11 |

12 |

错误码:[[${status}]]

13 |

信息:[[${error}]]

14 |

时间:[[${#dates.format(timestamp,'yyyy-MM-dd hh:mm:ss ')}]]

15 |

请求路径:[[${path}]]

16 |
17 | 18 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/main/resources/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Floating labels example for Bootstrap 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/test/java/net/codingme/boot/SpringbootDataMybatisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import net.codingme.boot.domain.Book; 5 | import net.codingme.boot.service.BookService; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | import javax.sql.DataSource; 13 | import java.util.List; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest 17 | public class SpringbootDataMybatisApplicationTests { 18 | 19 | @Autowired 20 | private DataSource dataSource; 21 | 22 | @Autowired 23 | private BookService bookService; 24 | 25 | @Test 26 | public void contextLoads() { 27 | System.out.println(dataSource.getClass()); 28 | } 29 | 30 | @Test 31 | public void testBook() throws Exception { 32 | List books = bookService.selectAll(); 33 | System.out.println(JSON.toJSONString(books)); 34 | } 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /springboot-data-mybatis/src/test/java/net/codingme/boot/domain/mapper/BookMapperTest.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.mapper; 2 | 3 | import lombok.ToString; 4 | import net.codingme.boot.domain.Book; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | import java.util.List; 13 | 14 | import static org.junit.jupiter.api.Assertions.*; 15 | 16 | @RunWith(SpringRunner.class) 17 | @SpringBootTest 18 | public class BookMapperTest { 19 | 20 | @Autowired 21 | private BookMapper bookMapper; 22 | 23 | @Test 24 | public void testSelectAll() { 25 | List bookList = bookMapper.selectAll(); 26 | Assert.assertNotNull(bookList); 27 | bookList.forEach((book) -> System.out.println(book)); 28 | } 29 | 30 | 31 | @Test 32 | public void testSelectByAuthro() { 33 | List bookList = bookMapper.selectByAuthor("金庸"); 34 | Assert.assertNotNull(bookList); 35 | bookList.forEach((book) -> System.out.println(book)); 36 | } 37 | 38 | @Test 39 | public void testSelectByPrimaryKey() { 40 | Book book = bookMapper.selectByPrimaryKey(2); 41 | Assert.assertNotNull(book); 42 | System.out.println(book); 43 | } 44 | 45 | @Test 46 | public void testDeleteByPrimaryKey() { 47 | int primaryKey = bookMapper.deleteByPrimaryKey(8); 48 | Assert.assertNotEquals(0, primaryKey); 49 | System.out.println(primaryKey); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /springboot-hello/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.mvn/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-hello/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | net.coding 7 | springboot-hello 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | springboot-hello 12 | springboot-hello 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.1.1.RELEASE 18 | 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /springboot-hello/src/main/java/net/coding/boot/HelloApplication.java: -------------------------------------------------------------------------------- 1 | package net.coding.boot; 2 | 3 | import org.springframework.boot.CommandLineRunner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.annotation.Bean; 8 | 9 | import java.util.Arrays; 10 | 11 | @SpringBootApplication 12 | public class HelloApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(HelloApplication.class, args); 16 | } 17 | 18 | @Bean 19 | public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 20 | return args -> { 21 | // 开始检查spring boot 提供的 beans 22 | System.out.println("Let's inspect the beans provided by Spring Boot:"); 23 | String[] beanNames = ctx.getBeanDefinitionNames(); 24 | Arrays.sort(beanNames); 25 | for (String beanName : beanNames) { 26 | System.out.println(beanName); 27 | } 28 | }; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /springboot-hello/src/main/java/net/coding/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.coding.boot.controller; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.ResponseBody; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | *

10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/12/4 14:41 13 | */ 14 | @RestController 15 | public class HelloController { 16 | 17 | @RequestMapping("/") 18 | public String index() { 19 | return "Greetings from Spring Boot!"; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /springboot-hello/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 -------------------------------------------------------------------------------- /springboot-hello/src/test/java/net/coding/boot/HelloApplicationTestBySpringBoot.java: -------------------------------------------------------------------------------- 1 | package net.coding.boot; 2 | 3 | 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.boot.test.web.client.TestRestTemplate; 10 | import org.springframework.boot.web.server.LocalServerPort; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | 14 | import java.net.URL; 15 | 16 | /** 17 | *

18 | * 嵌入式服务器由随机端口启动webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT 19 | * 并且在运行时发现实际端口@LocalServerPort 20 | * 21 | * @Author niujinpeng 22 | * @Date 2018/12/4 15:02 23 | */ 24 | @RunWith(SpringRunner.class) 25 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 26 | public class HelloApplicationTestBySpringBoot { 27 | 28 | @LocalServerPort 29 | private int port; 30 | 31 | private URL base; 32 | 33 | @Autowired 34 | private TestRestTemplate template; 35 | 36 | @Before 37 | public void setup() throws Exception { 38 | this.base = new URL("http://localhost:" + port + "/"); 39 | } 40 | 41 | @Test 42 | public void getHello() throws Exception { 43 | ResponseEntity response = template.getForEntity(base.toString(), String.class); 44 | assert (response.getBody().equals("Greetings from Spring Boot!")); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-hello/src/test/java/net/coding/boot/HelloApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.coding.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | import org.springframework.test.web.servlet.MockMvc; 11 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 12 | 13 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 14 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 15 | 16 | /** 17 | * 单元测试 18 | */ 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest 21 | @AutoConfigureMockMvc 22 | public class HelloApplicationTests { 23 | 24 | @Autowired 25 | private MockMvc mvc; 26 | 27 | @Test 28 | public void testGetHello() throws Exception { 29 | mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) 30 | .andExpect(status().isOk()) 31 | .andExpect(content().string("Greetings from Spring Boot!")); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /springboot-logback/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.mvn/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.context.ApplicationContext; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.ImportResource; 12 | import org.springframework.context.annotation.PropertySource; 13 | 14 | import java.util.Arrays; 15 | 16 | /** 17 | *

18 | * 用于引入传统的 XML 配置文件 19 | * 20 | * @ImportResource(value = "classpath:spring-service.xml") 21 | * @Author niujinpeng 22 | * @Date 2018/12/7 0:04 23 | */ 24 | 25 | //@ImportResource(value = "classpath:config/spring-service.xml") 26 | @SpringBootApplication 27 | public class BootApplication { 28 | 29 | public static void main(String[] args) { 30 | SpringApplication.run(BootApplication.class, args); 31 | } 32 | 33 | @Bean 34 | public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 35 | return args -> { 36 | // 开始检查spring boot 提供的 beans 37 | System.out.println("Let's inspect the beans provided by Spring Boot:"); 38 | String[] beanNames = ctx.getBeanDefinitionNames(); 39 | Arrays.sort(beanNames); 40 | for (String beanName : beanNames) { 41 | //System.out.println(beanName); 42 | } 43 | }; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/config/ServiceConfig.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import net.codingme.boot.service.HelloService; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | *

9 | * 配置类,相当于传统Spring 开发中的 xml-> bean的配置 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/12/7 0:04 13 | */ 14 | @Configuration 15 | public class ServiceConfig { 16 | 17 | /** 18 | * 默认添加到容器中的 ID 为方法名(helloService) 19 | * 20 | * @return 21 | */ 22 | @Bean 23 | public HelloService helloService() { 24 | return new HelloService(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | *

11 | * 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/4 14:41 15 | */ 16 | @Slf4j 17 | @RestController 18 | public class HelloController { 19 | 20 | @Value("${bootapp.description}") 21 | private String description; 22 | 23 | @RequestMapping("/") 24 | public String index() { 25 | log.info(description); 26 | return "Greetings from Spring Boot!"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/domain/Dog.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | *

11 | * 12 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 13 | * @Component 加到spring 容器中 14 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 15 | * @Author niujinpeng 16 | * @Date 2018/12/6 22:56 17 | */ 18 | 19 | @Data 20 | @Component 21 | @ConfigurationProperties(prefix = "person.dog") 22 | public class Dog { 23 | private String name; 24 | private Integer age; 25 | } 26 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/domain/Person.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.validation.annotation.Validated; 7 | 8 | import javax.validation.constraints.Email; 9 | import java.util.Date; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 17 | * @Component 加到spring 容器中 18 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 19 | * @Author niujinpeng 20 | * @Date 2018/12/6 22:54 21 | */ 22 | 23 | @Data 24 | @Component 25 | @ConfigurationProperties(prefix = "person") 26 | @Validated 27 | public class Person { 28 | 29 | private String lastName; 30 | private Integer age; 31 | private Date birth; 32 | private Map maps; 33 | private List lists; 34 | private Dog dog; 35 | 36 | /** 37 | * 支持数据校验 38 | */ 39 | @Email 40 | private String email; 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/domain/PersonSource.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.context.annotation.PropertySource; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.validation.annotation.Validated; 8 | 9 | import javax.validation.constraints.Email; 10 | import java.util.Date; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | *

16 | * 使用@PropertySource加载自定义的配置文件,需要注意的是,由于@PropertySource指定的文件会优先加载, 17 | * 所以如果在 applocation.properties中存在相同的属性配置,会覆盖前者的值。 18 | * 19 | * @@PropertySource 加载自定义的配置文件 20 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 21 | * @Component 加到spring 容器中 22 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 23 | * @Author niujinpeng 24 | * @Date 2018/12/6 22:54 25 | */ 26 | 27 | @Data 28 | @Component 29 | @Validated 30 | @PropertySource(value = "classpath:config/domain-person.properties") 31 | @ConfigurationProperties(value = "person") 32 | public class PersonSource { 33 | 34 | private String lastName; 35 | private Integer age; 36 | private Date birth; 37 | private Map maps; 38 | private List lists; 39 | private Dog dog; 40 | 41 | /** 42 | * 支持数据校验 43 | */ 44 | @Email 45 | private String email; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/domain/PersonValue.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.validation.annotation.Validated; 7 | 8 | import javax.validation.constraints.Email; 9 | import java.util.Date; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 17 | * @Component 加到spring 容器中 18 | * @Author niujinpeng 19 | * @Date 2018/12/6 23:21 20 | */ 21 | @Data 22 | @Component 23 | @Validated 24 | public class PersonValue { 25 | 26 | /** 27 | * 直接从配置文件读取一个值 28 | */ 29 | @Value("${person.last-name}") 30 | private String lastName; 31 | 32 | /** 33 | * 支持SpEL表达式 34 | */ 35 | @Value("#{11*4/2}") 36 | private Integer age; 37 | 38 | @Value("${person.birth}") 39 | private Date birth; 40 | 41 | /** 42 | * 不支持复杂类型 43 | */ 44 | private Map maps; 45 | private List lists; 46 | private Dog dog; 47 | 48 | /** 49 | * 不支持数据校验 50 | */ 51 | @Email 52 | @Value("xxx@@@@") 53 | private String email; 54 | } 55 | -------------------------------------------------------------------------------- /springboot-logback/src/main/java/net/codingme/boot/service/HelloService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 测试server bean的加载方式 9 | * 1.使用@Service 注解声明 10 | * 2.使用XML配置通过@ImportResource 引入 11 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 12 | * 13 | * @Service 添加到容器中 14 | * @Author niujinpeng 15 | * @Date 2018/12/7 0:05 16 | */ 17 | //@Service 18 | public class HelloService { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-logback/src/main/resources/config/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-logback/src/main/resources/config/application.yml: -------------------------------------------------------------------------------- 1 | ### 服务启动端口号 2 | ##server: 3 | ## port: 8080 4 | ### 配置person属性值 5 | #person: 6 | # last-name: Darcy 7 | # age: 20 8 | # birth: 2018/01/01 9 | # email: gmail@gmail.com 10 | # maps: 11 | # key1:java 12 | # key2:golang 13 | # lists: 14 | # - a 15 | # - b 16 | # - c 17 | # dog: 18 | # name: 旺财 19 | # age: 2 20 | ## 属性的占位符 21 | #bootapp: 22 | # name: SpringBoot 23 | # description: ${bootapp.name}是一个spring应用程序 24 | #server: 25 | # port: 8083 26 | # profiles: 27 | # active: dev #指定环境为dev 28 | # # 使用三个---进行文档块区分 29 | # 30 | #--- 31 | #server: 32 | # port: 8084 33 | #spring: 34 | # profiles: dev 35 | #--- 36 | #server: 37 | # port: 8085 38 | #spring: 39 | # profiles: prod 40 | -------------------------------------------------------------------------------- /springboot-logback/src/main/resources/config/domain-person.properties: -------------------------------------------------------------------------------- 1 | # \u914D\u7F6E\u5C5E\u6027\u503C\uFF08\u4F7F\u7528IDE\u8FDB\u884C\u914D\u7F6E\u9700\u8981\u5904\u7406\u7F16\u7801\u95EE\u9898\uFF0C\u4E0D\u7136\u4E2D\u6587\u4F1A\u53D1\u9001\u4E71\u7801\u73B0\u8C61\uFF09 2 | person.last-name=\u674E\u56DB 3 | person.age=20 4 | person.birth=2018/12/06 5 | person.email=niu@gmail.com 6 | person.maps.key1=c 7 | person.maps.key2=java 8 | person.maps.key3=golang 9 | person.lists=a,b,c,d 10 | person.dog.name=\u65FA\u8D22 11 | person.dog.age=1 -------------------------------------------------------------------------------- /springboot-logback/src/main/resources/config/spring-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /springboot-logback/src/test/java/net/codingme/boot/LogbackTest.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | /** 11 | *

12 | * 测试日志输出, 13 | * SLF4J 日志级别从小到大trace,debug,info,warn,error 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/11 21:12 17 | */ 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class LogbackTest { 21 | 22 | Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testLog() { 26 | logger.trace("Trace 日志..."); 27 | logger.debug("Debug 日志..."); 28 | logger.info("Info 日志..."); 29 | logger.warn("Warn 日志..."); 30 | logger.error("Error 日志..."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-mail/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | -------------------------------------------------------------------------------- /springboot-mail/apple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-mail/apple.png -------------------------------------------------------------------------------- /springboot-mail/src/main/java/net/codingme/boot/MailApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MailApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MailApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-mail/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mail.host=smtp.126.com 2 | spring.mail.port=25 3 | # \u4F60\u7684\u90AE\u7BB1\u5730\u5740 4 | spring.mail.username=niumoo@126.com 5 | # \u4F60\u7684\u6388\u6743\u7801\uFF08126 \u548C 163 \u4EE5\u53CA qq \u90AE\u7BB1 \u90FD\u9700\u8981\u6388\u6743\u7801\u767B\u5F55\uFF0C\u6CA1\u6709\u6388\u6743\u7801\u7684\u76F4\u63A5\u767B\u5F55\u7F51\u9875\u7248\u90AE\u7BB1\u8BBE\u7F6E\u91CC\u8BBE\u7F6E\uFF09 6 | spring.mail.password=PASSWORD 7 | spring.mail.default-encoding=UTF-8 8 | -------------------------------------------------------------------------------- /springboot-mail/src/main/resources/templates/RegisterSuccess.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 注册成功通知 6 | 7 | 8 |

[[${username}]],您好! 9 |

10 |

11 | 新的公钥已添加到你的账户:
12 | 标题: HP-WIN10
13 | 如果公钥无法使用,你可以在这里重新添加: SSH Keys 14 |

15 | 16 | 17 | -------------------------------------------------------------------------------- /springboot-mail/src/test/java/net/codingme/boot/SpringbootMailApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootMailApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() {} 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-module-demo/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /springboot-module-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | pom 6 | 7 | product-common 8 | product-web 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 2.2.5.RELEASE 14 | 15 | 16 | com.wdbyte 17 | springboot-module-demo 18 | 0.0.1-SNAPSHOT 19 | springboot-module-demo 20 | Demo project for Spring Boot 21 | 22 | 23 | 1.8 24 | 0.0.1-SNAPSHOT 25 | 26 | 27 | 28 | 29 | 30 | com.wdbyte 31 | product-common 32 | ${product-common.version} 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /springboot-module-demo/product-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | springboot-module-demo 7 | com.wdbyte 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | product-common 12 | 13 | 14 | org.projectlombok 15 | lombok 16 | true 17 | 18 | 19 | -------------------------------------------------------------------------------- /springboot-module-demo/product-common/src/main/java/com/wdbyte/entity/Product.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.entity; 2 | 3 | import lombok.Data; 4 | 5 | import java.math.BigDecimal; 6 | 7 | /** 8 | *

9 | * 10 | * @author niujinpeng 11 | * @Date 2020/3/17 22:21 12 | */ 13 | @Data 14 | public class Product { 15 | /** 商品名称. */ 16 | private String productName; 17 | /** 商品价格. */ 18 | private BigDecimal productPrice; 19 | /** 商品库存。 */ 20 | private int productStock; 21 | } 22 | -------------------------------------------------------------------------------- /springboot-module-demo/product-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | springboot-module-demo 7 | com.wdbyte 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | product-web 12 | 13 | 14 | 15 | com.wdbyte 16 | product-common 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-test 27 | test 28 | 29 | 30 | org.junit.vintage 31 | junit-vintage-engine 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-maven-plugin 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /springboot-module-demo/product-web/src/main/java/com/wdbyte/Application.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-module-demo/product-web/src/main/java/com/wdbyte/controller/ProductController.java: -------------------------------------------------------------------------------- 1 | package com.wdbyte.controller; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.Arrays; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import com.wdbyte.entity.Product; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | /** 14 | *

15 | * 16 | * @author niujinpeng 17 | * @Date 2020/3/17 22:23 18 | */ 19 | @RestController 20 | @RequestMapping("/product") 21 | public class ProductController { 22 | 23 | /** 24 | * 获取商品列表 25 | * 26 | * @return 27 | */ 28 | @GetMapping("/list") 29 | public Map list() { 30 | // 模拟查询逻辑 31 | Product product = new Product(); 32 | product.setProductName("小米粥"); 33 | product.setProductPrice(new BigDecimal(2.0)); 34 | product.setProductStock(100); 35 | 36 | Map resultMap = new HashMap<>(); 37 | resultMap.put("code", 000); 38 | resultMap.put("message", "成功"); 39 | resultMap.put("data", Arrays.asList(product)); 40 | return resultMap; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /springboot-module-demo/product-web/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /springboot-properties/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.mvn/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-properties/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.CommandLineRunner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.ImportResource; 9 | import org.springframework.context.annotation.PropertySource; 10 | 11 | import java.util.Arrays; 12 | 13 | /** 14 | *

15 | * 配置类,相当于传统Spring 开发中的 xml-> bean的配置 16 | * 17 | * @Author niujinpeng 18 | * @Date 2018/12/7 0:04 19 | */ 20 | @SpringBootApplication 21 | public class BootApplication { 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(BootApplication.class, args); 25 | } 26 | 27 | @Bean 28 | public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 29 | return args -> { 30 | // 开始检查spring boot 提供的 beans 31 | System.out.println("Let's inspect the beans provided by Spring Boot:"); 32 | String[] beanNames = ctx.getBeanDefinitionNames(); 33 | Arrays.sort(beanNames); 34 | for (String beanName : beanNames) { 35 | //System.out.println(beanName); 36 | } 37 | }; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /springboot-properties/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | *

11 | * 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/4 14:41 15 | */ 16 | @Slf4j 17 | @RestController 18 | public class HelloController { 19 | 20 | @Value("${bootapp.description}") 21 | private String description; 22 | 23 | @RequestMapping("/") 24 | public String index() { 25 | log.info(description); 26 | return "Greetings from Spring Boot!"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-properties/src/main/java/net/codingme/boot/domain/proper/Dog.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.proper; 2 | 3 | import lombok.Data; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import org.springframework.boot.context.properties.ConfigurationProperties; 7 | import org.springframework.stereotype.Component; 8 | 9 | /** 10 | *

11 | * 12 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 13 | * @Component 加到spring 容器中 14 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 15 | * @Author niujinpeng 16 | * @Date 2018/12/6 22:56 17 | */ 18 | 19 | @Data 20 | @Component 21 | @ConfigurationProperties(prefix = "person.dog") 22 | public class Dog { 23 | private String name; 24 | private Integer age; 25 | } 26 | -------------------------------------------------------------------------------- /springboot-properties/src/main/java/net/codingme/boot/domain/proper/Person.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.proper; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.validation.annotation.Validated; 7 | 8 | import javax.validation.constraints.Email; 9 | import java.util.Date; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 17 | * @Component 加到spring 容器中 18 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 19 | * @Author niujinpeng 20 | * @Date 2018/12/6 22:54 21 | */ 22 | 23 | @Data 24 | @Component 25 | @ConfigurationProperties(prefix = "person") 26 | @Validated 27 | public class Person { 28 | 29 | private String lastName; 30 | private Integer age; 31 | private Date birth; 32 | private Map maps; 33 | private List lists; 34 | private Dog dog; 35 | 36 | /** 37 | * 支持数据校验 38 | */ 39 | @Email 40 | private String email; 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /springboot-properties/src/main/java/net/codingme/boot/domain/proper/PersonSource.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.proper; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | import org.springframework.context.annotation.PropertySource; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.validation.annotation.Validated; 8 | 9 | import javax.validation.constraints.Email; 10 | import java.util.Date; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | *

16 | * 使用@PropertySource加载自定义的配置文件,需要注意的是,由于@PropertySource指定的文件会优先加载, 17 | * 所以如果在 applocation.properties中存在相同的属性配置,会覆盖前者的值。 18 | * 19 | * @@PropertySource 加载自定义的配置文件 20 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 21 | * @Component 加到spring 容器中 22 | * @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀 23 | * @Author niujinpeng 24 | * @Date 2018/12/6 22:54 25 | */ 26 | 27 | @Data 28 | @Component 29 | @Validated 30 | @PropertySource(value = "classpath:domain-person.properties") 31 | @ConfigurationProperties(value = "person") 32 | public class PersonSource { 33 | 34 | private String lastName; 35 | private Integer age; 36 | private Date birth; 37 | private Map maps; 38 | private List lists; 39 | private Dog dog; 40 | 41 | /** 42 | * 支持数据校验 43 | */ 44 | @Email 45 | private String email; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /springboot-properties/src/main/java/net/codingme/boot/domain/proper/PersonValue.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain.proper; 2 | 3 | import lombok.Data; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.validation.annotation.Validated; 7 | 8 | import javax.validation.constraints.Email; 9 | import java.util.Date; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | /** 14 | *

15 | * 16 | * @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法 17 | * @Component 加到spring 容器中 18 | * @Author niujinpeng 19 | * @Date 2018/12/6 23:21 20 | */ 21 | @Data 22 | @Component 23 | @Validated 24 | public class PersonValue { 25 | 26 | /** 27 | * 直接从配置文件读取一个值 28 | */ 29 | @Value("${person.last-name}") 30 | private String lastName; 31 | 32 | /** 33 | * 支持SpEL表达式 34 | */ 35 | @Value("#{11*4/2}") 36 | private Integer age; 37 | 38 | @Value("${person.birth}") 39 | private Date birth; 40 | 41 | /** 42 | * 不支持复杂类型 43 | */ 44 | private Map maps; 45 | private List lists; 46 | private Dog dog; 47 | 48 | /** 49 | * 不支持数据校验 50 | */ 51 | @Email 52 | @Value("xxx@@@@") 53 | private String email; 54 | } 55 | -------------------------------------------------------------------------------- /springboot-properties/src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-properties/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ################################################################ 2 | # \u670D\u52A1\u542F\u52A8\u7AEF\u53E3\u53F7 3 | server.port=8080 4 | spring.profiles.active=prod 5 | ################################################################ 6 | # \u914D\u7F6E\u5C5E\u6027\u503C\uFF08\u4F7F\u7528IDE\u8FDB\u884C\u914D\u7F6E\u9700\u8981\u5904\u7406\u7F16\u7801\u95EE\u9898\uFF0C\u4E0D\u7136\u4E2D\u6587\u4F1A\u53D1\u9001\u4E71\u7801\u73B0\u8C61\uFF09 7 | person.last-name=\u5F20\u4E09 8 | person.age=18 9 | person.birth=2018/12/06 10 | person.email=gmail@gmail.com 11 | person.maps.key1=c 12 | person.maps.key2=java 13 | person.maps.key3=golang 14 | person.lists=a,b,c,d 15 | person.dog.name=\u65FA\u8D22 16 | person.dog.age=1 17 | ################################################################ 18 | # \u751F\u6210\u968F\u673A\u503C 19 | bootapp.secret=$ {random.value} 20 | bootapp.number=$ {random.int} 21 | bootapp.bignumber=$ {random.long} 22 | bootapp.uuid=$ {random.uuid} 23 | bootapp.number.less.than.ten=$ {random.int\uFF0810\uFF09} 24 | bootapp.number.in.range=$ {random.int [1024,65536]} 25 | # \u5C5E\u6027\u7684\u5360\u4F4D\u7B26 26 | bootapp.name=SpringBoot 27 | bootapp.description=${bootapp.name}\u662F\u4E00\u4E2Aspring\u5E94\u7528\u7A0B\u5E8F -------------------------------------------------------------------------------- /springboot-properties/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | ### 服务启动端口号 2 | ##server: 3 | ## port: 8080 4 | ### 配置person属性值 5 | #person: 6 | # last-name: Darcy 7 | # age: 20 8 | # birth: 2018/01/01 9 | # email: gmail@gmail.com 10 | # maps: 11 | # key1:java 12 | # key2:golang 13 | # lists: 14 | # - a 15 | # - b 16 | # - c 17 | # dog: 18 | # name: 旺财 19 | # age: 2 20 | ## 属性的占位符 21 | #bootapp: 22 | # name: SpringBoot 23 | # description: ${bootapp.name}是一个spring应用程序 24 | #server: 25 | # port: 8083 26 | # profiles: 27 | # active: dev #指定环境为dev 28 | # # 使用三个---进行文档块区分 29 | # 30 | #--- 31 | #server: 32 | # port: 8084 33 | #spring: 34 | # profiles: dev 35 | #--- 36 | #server: 37 | # port: 8085 38 | #spring: 39 | # profiles: prod 40 | -------------------------------------------------------------------------------- /springboot-properties/src/main/resources/domain-person.properties: -------------------------------------------------------------------------------- 1 | # \u914D\u7F6E\u5C5E\u6027\u503C\uFF08\u4F7F\u7528IDE\u8FDB\u884C\u914D\u7F6E\u9700\u8981\u5904\u7406\u7F16\u7801\u95EE\u9898\uFF0C\u4E0D\u7136\u4E2D\u6587\u4F1A\u53D1\u9001\u4E71\u7801\u73B0\u8C61\uFF09 2 | person.last-name=\u674E\u56DB 3 | person.age=20 4 | person.birth=2018/12/06 5 | person.email=niu@gmail.com 6 | person.maps.key1=c 7 | person.maps.key2=java 8 | person.maps.key3=golang 9 | person.lists=a,b,c,d 10 | person.dog.name=\u65FA\u8D22 11 | person.dog.age=1 -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-autoconfigure/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-autoconfigure/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RELEASE 9 | 10 | 11 | net.codingme.starter 12 | myapp-spring-boot-autoconfigure 13 | 0.0.1-SNAPSHOT 14 | myapp-spring-boot-autoconfigure 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-autoconfigure/src/main/java/net/codingme/starter/HelloProperties.java: -------------------------------------------------------------------------------- 1 | package net.codingme.starter; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | 5 | /** 6 | *

7 | * 8 | * @Author niujinpeng 9 | * @Date 2019/10/29 23:51 10 | */ 11 | @ConfigurationProperties(prefix = "myapp.hello") 12 | public class HelloProperties { 13 | 14 | private String suffix; 15 | 16 | public String getSuffix() { 17 | return suffix; 18 | } 19 | 20 | public void setSuffix(String suffix) { 21 | this.suffix = suffix; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-autoconfigure/src/main/java/net/codingme/starter/HelloService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.starter; 2 | 3 | /** 4 | *

5 | * 6 | * @Author niujinpeng 7 | * @Date 2019/10/29 23:51 8 | */ 9 | public class HelloService { 10 | 11 | HelloProperties helloProperties; 12 | 13 | public String sayHello(String name) { 14 | return "Hello " + name + "," + helloProperties.getSuffix(); 15 | } 16 | 17 | public HelloProperties getHelloProperties() { 18 | return helloProperties; 19 | } 20 | 21 | public void setHelloProperties(HelloProperties helloProperties) { 22 | this.helloProperties = helloProperties; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-autoconfigure/src/main/java/net/codingme/starter/HelloServiceAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package net.codingme.starter; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | /** 10 | *

11 | * 12 | * @Author niujinpeng 13 | * @Date 2019/10/30 8:18 14 | */ 15 | 16 | /** 17 | * web应用才生效 18 | */ 19 | @ConditionalOnWebApplication 20 | /** 21 | * 让属性文件生效 22 | */ 23 | @EnableConfigurationProperties(HelloProperties.class) 24 | /*** 25 | * 声明是一个配置类 26 | */ 27 | @Configuration 28 | public class HelloServiceAutoConfiguration { 29 | 30 | @Autowired 31 | private HelloProperties helloProperties; 32 | 33 | @Bean 34 | public HelloService helloService() { 35 | HelloService helloService = new HelloService(); 36 | helloService.setHelloProperties(helloProperties); 37 | return helloService; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | # Auto Configure 2 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 3 | net.codingme.starter.HelloServiceAutoConfiguration -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-starter-test/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-starter-test/src/main/java/net/codingme/starter/MyappSpringBootStarterTestApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.starter; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MyappSpringBootStarterTestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MyappSpringBootStarterTestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-starter-test/src/main/java/net/codingme/starter/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.starter.controller; 2 | 3 | import net.codingme.starter.HelloService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | *

10 | * 11 | * @Author niujinpeng 12 | * @Date 2019/10/30 8:31 13 | */ 14 | @RestController 15 | public class HelloController { 16 | 17 | @Autowired 18 | HelloService helloService; 19 | 20 | @GetMapping("/hello") 21 | public String sayHello(String name) { 22 | return helloService.sayHello(name); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-starter-test/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | myapp.hello.suffix=\u65E9\u4E0A\u597D -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-starter-test/src/test/java/net/codingme/starter/MyappSpringBootStarterTestApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.starter; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MyappSpringBootStarterTestApplicationTests { 8 | 9 | @Test 10 | void contextLoads() {} 11 | 12 | } 13 | -------------------------------------------------------------------------------- /springboot-starter/myapp-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | net.codingme.starter 8 | myapp-spring-boot-starter 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | net.codingme.starter 15 | myapp-spring-boot-autoconfigure 16 | 0.0.1-SNAPSHOT 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /springboot-web-error/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | *

8 | * Spring Boot 启动类 9 | *

10 | */ 11 | 12 | @SpringBootApplication 13 | public class BootApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(BootApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/config/LogHandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | *

12 | * 拦截器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/6 16:54 16 | */ 17 | @Slf4j 18 | public class LogHandlerInterceptor implements HandlerInterceptor { 19 | 20 | /** 21 | * 请求方法执行之前 22 | * 返回true则通过 23 | * 24 | * @param request 25 | * @param response 26 | * @param handler 27 | * @return 28 | */ 29 | @Override 30 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 31 | StringBuffer requestURL = request.getRequestURL(); 32 | log.info("请求URL:" + requestURL.toString()); 33 | return true; 34 | } 35 | 36 | /** 37 | * 返回modelAndView之前执行 38 | * @param request 39 | * @param response 40 | * @param handler 41 | * @param modelAndView 42 | */ 43 | @Override 44 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { 45 | } 46 | 47 | /** 48 | * 执行Handler完成执行此方法 49 | * @param request 50 | * @param response 51 | * @param handler 52 | * @param ex 53 | */ 54 | @Override 55 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.utils.ResponseUtill; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | *

14 | * 测试控制器 15 | * 16 | * @Author niujinpeng 17 | * @Date 2018/12/4 14:41 18 | */ 19 | @Slf4j 20 | @RestController 21 | public class HelloController { 22 | 23 | @Value("${bootapp.description}") 24 | private String description; 25 | 26 | @RequestMapping("/") 27 | public String index() { 28 | log.info(description); 29 | return "Greetings from Spring Boot!"; 30 | } 31 | 32 | @RequestMapping(value = "/er") 33 | @ResponseBody 34 | public Response error(Integer num) throws Exception { 35 | num = num / num; 36 | return ResponseUtill.success(num); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Collection; 8 | 9 | 10 | /** 11 | *

12 | * 响应实体类封装 13 | * 14 | * @Author niujinpeng 15 | * @Date 2018/12/19 17:13 16 | */ 17 | 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | public class Response { 22 | /** 23 | * 响应码 24 | */ 25 | private String code; 26 | /** 27 | * 响应信息 28 | */ 29 | private String message; 30 | 31 | /** 32 | * 响应数据 33 | */ 34 | private Collection data; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/domain/User.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Date; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | *

13 | * 14 | * @Data GET SET TOSTRING 15 | * @NoArgsConstructor 无参构造 16 | * @AllArgsConstructor 全参构造 17 | * @Author niujinpeng 18 | * @Date 2018/12/19 17:13 19 | */ 20 | @Data 21 | @NoArgsConstructor 22 | @AllArgsConstructor 23 | public class User { 24 | 25 | /** 26 | * 用户名 27 | */ 28 | private String username; 29 | /** 30 | * 密码 31 | */ 32 | private String password; 33 | /** 34 | * 年龄 35 | */ 36 | private Integer age; 37 | /** 38 | * 生日 39 | */ 40 | private Date birthday; 41 | /** 42 | * 技能 43 | */ 44 | private List skills; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/enums/ResponseEnum.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.enums; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | *

9 | * 用于系统返回的枚举类 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/8/24 14:30 13 | */ 14 | public enum ResponseEnum { 15 | 16 | /** 17 | * 系统总体相关 18 | */ 19 | SUCCESS("0", "success"), 20 | UNKNOW_ERROR("-1", "未知错误"), 21 | ERROR("999", "请求处理失败"); 22 | 23 | @Setter 24 | @Getter 25 | private String code; 26 | 27 | @Setter 28 | @Getter 29 | private String message; 30 | 31 | ResponseEnum(String code, String msg) { 32 | this.code = code; 33 | this.message = msg; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/exception/BaseException.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import net.codingme.boot.enums.ResponseEnum; 7 | 8 | /** 9 | *

10 | * 自定义异常类 11 | * 12 | * @Author niujinpeng 13 | * @Date 2018/8/24 14:39 14 | */ 15 | @Getter 16 | @Setter 17 | public class BaseException extends RuntimeException { 18 | 19 | private String code; 20 | 21 | public BaseException(String message, String code) { 22 | super(message); 23 | this.code = code; 24 | } 25 | 26 | public BaseException(ResponseEnum resultEnum) { 27 | super(resultEnum.getMessage()); 28 | this.code = resultEnum.getCode(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/exception/ErrorAttributesCustom.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.context.request.WebRequest; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | /** 11 | *

12 | * 自定义错误信息JSON值 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/7 15:21 16 | */ 17 | @Component 18 | public class ErrorAttributesCustom extends DefaultErrorAttributes { 19 | 20 | @Override 21 | public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { 22 | Map map = super.getErrorAttributes(webRequest, includeStackTrace); 23 | String code = map.get("status").toString(); 24 | String message = map.get("error").toString(); 25 | map.remove("status"); 26 | map.remove("error"); 27 | HashMap hashMap = new HashMap<>(); 28 | hashMap.put("code", code); 29 | hashMap.put("message", message); 30 | hashMap.put("data", map); 31 | return hashMap; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/exception/ExceptionHandle.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.enums.ResponseEnum; 6 | import net.codingme.boot.utils.ResponseUtill; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | 13 | /** 14 | *

15 | * 统一的异常处理 16 | * 17 | * @Author niujinpeng 18 | * @Date 2019/1/7 14:26 19 | */ 20 | 21 | @Slf4j 22 | @ControllerAdvice 23 | public class ExceptionHandle { 24 | 25 | @ResponseBody 26 | @ExceptionHandler(Exception.class) 27 | public Response handleException(Exception e) { 28 | log.info("异常 {}", e); 29 | if (e instanceof BaseException) { 30 | BaseException exception = (BaseException) e; 31 | String code = exception.getCode(); 32 | String message = exception.getMessage(); 33 | return ResponseUtill.error(code, message); 34 | } 35 | return ResponseUtill.error(ResponseEnum.UNKNOW_ERROR); 36 | } 37 | 38 | 39 | /** 40 | * 判断是否是Ajax请求 41 | * 42 | * @param request 43 | * @return 44 | */ 45 | public static boolean isAjax(HttpServletRequest request) { 46 | String rquested = request.getHeader("X-Rquested-With"); 47 | if (request == null) { 48 | return false; 49 | } 50 | if (!"XMLHttpRequest".equals(rquested.toString())) { 51 | return false; 52 | } 53 | return true; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 9 | * @Author niujinpeng 10 | * @Date 2018/12/7 0:05 11 | */ 12 | 13 | public interface UserService { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.service.UserService; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 测试server bean的加载方式 9 | * 1.使用@Service 注解声明 10 | * 2.使用XML配置通过@ImportResource 引入 11 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 12 | * 13 | * @Service 添加到容器中 14 | * @Author niujinpeng 15 | * @Date 2018/12/27 15:59 16 | */ 17 | 18 | @Service 19 | public class UserServiceImpl implements UserService { 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | import net.codingme.boot.enums.ResponseEnum; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | 9 | /** 10 | *

11 | * 方便响应的工具类 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/19 17:33 15 | */ 16 | public class ResponseUtill { 17 | 18 | public static Response success(Object objects) { 19 | ArrayList list = new ArrayList<>(); 20 | if (!(objects instanceof Collection)) { 21 | list.add(objects); 22 | } else { 23 | list = (ArrayList) objects; 24 | } 25 | return new Response("0000", "success", list); 26 | } 27 | 28 | public static Response success() { 29 | return success(null); 30 | } 31 | 32 | public static Response error(String code, String message) { 33 | return new Response(code, message, new ArrayList<>()); 34 | } 35 | 36 | public static Response error(ResponseEnum responseEnum) { 37 | return error(responseEnum.getCode(), responseEnum.getMessage()); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /springboot-web-error/src/main/resources/config/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-web-error/src/main/resources/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-web-error/src/main/resources/static/favicon.ico -------------------------------------------------------------------------------- /springboot-web-error/src/main/resources/templates/error/4xx.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | [[${status}]] 7 | 8 | 9 | 10 | 11 |

12 |

错误码:[[${status}]]

13 |

信息:[[${error}]]

14 |

时间:[[${#dates.format(timestamp,'yyyy-MM-dd hh:mm:ss ')}]]

15 |

请求路径:[[${path}]]

16 |
17 | 18 | -------------------------------------------------------------------------------- /springboot-web-error/src/test/java/net/codingme/boot/LogbackTest.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | /** 11 | *

12 | * 测试日志输出, 13 | * SLF4J 日志级别从小到大trace,debug,info,warn,error 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/11 21:12 17 | */ 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class LogbackTest { 21 | 22 | Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testLog() { 26 | logger.trace("Trace 日志..."); 27 | logger.debug("Debug 日志..."); 28 | logger.info("Info 日志..."); 29 | logger.warn("Warn 日志..."); 30 | logger.error("Error 日志..."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-web-error/src/test/java/net/codingme/boot/SpringbootWebTemplateApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootWebTemplateApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /springboot-web-https/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /springboot-web-https/src/main/java/net/codingme/https/SpringbootHttpsApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.https; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootHttpsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootHttpsApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /springboot-web-https/src/main/java/net/codingme/https/controller/HttpsController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.https.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | *

11 | * Https 接口控制类 12 | * 13 | * @Author niujinpeng 14 | * @Date 2019/4/20 22:59 15 | */ 16 | @RestController 17 | public class HttpsController { 18 | 19 | @GetMapping(value = "/hello") 20 | public String hello() { 21 | return "Hello HTTPS"; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /springboot-web-https/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # \u914D\u7F6E HTTPS \u76F8\u5173\u4FE1\u606F 2 | server: 3 | port: 443 4 | http-port: 80 5 | ssl: 6 | enabled: true 7 | key-store: classpath:tomcat_https.keystore 8 | key-password: 123456 9 | key-store-type: JKS 10 | key-alias: tomcat_https -------------------------------------------------------------------------------- /springboot-web-https/src/main/resources/tomcat_https.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-web-https/src/main/resources/tomcat_https.keystore -------------------------------------------------------------------------------- /springboot-web-interceptor/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | *

8 | * Spring Boot 启动类 9 | *

10 | */ 11 | 12 | @SpringBootApplication 13 | public class BootApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(BootApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.utils.ResponseUtill; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | *

14 | * 测试控制器 15 | * 16 | * @Author niujinpeng 17 | * @Date 2018/12/4 14:41 18 | */ 19 | @Slf4j 20 | @RestController 21 | public class HelloController { 22 | 23 | @Value("${bootapp.description}") 24 | private String description; 25 | 26 | @RequestMapping("/") 27 | public String index() { 28 | return "Greetings from Spring Boot!" + description; 29 | } 30 | 31 | @RequestMapping(value = "/er") 32 | @ResponseBody 33 | public Response error(Integer num) throws Exception { 34 | num = num / num; 35 | return ResponseUtill.success(num); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Collection; 8 | 9 | 10 | /** 11 | *

12 | * 响应实体类封装 13 | * 14 | * @Author niujinpeng 15 | * @Date 2018/12/19 17:13 16 | */ 17 | 18 | @Data 19 | @AllArgsConstructor 20 | @NoArgsConstructor 21 | public class Response { 22 | /** 23 | * 响应码 24 | */ 25 | private String code; 26 | /** 27 | * 响应信息 28 | */ 29 | private String message; 30 | 31 | /** 32 | * 响应数据 33 | */ 34 | private Collection data; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/domain/User.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Date; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | *

13 | * 14 | * @Data GET SET TOSTRING 15 | * @NoArgsConstructor 无参构造 16 | * @AllArgsConstructor 全参构造 17 | * @Author niujinpeng 18 | * @Date 2018/12/19 17:13 19 | */ 20 | @Data 21 | @NoArgsConstructor 22 | @AllArgsConstructor 23 | public class User { 24 | 25 | /** 26 | * 用户名 27 | */ 28 | private String username; 29 | /** 30 | * 密码 31 | */ 32 | private String password; 33 | /** 34 | * 年龄 35 | */ 36 | private Integer age; 37 | /** 38 | * 生日 39 | */ 40 | private Date birthday; 41 | /** 42 | * 技能 43 | */ 44 | private List skills; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/enums/ResponseEnum.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.enums; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | *

9 | * 用于系统返回的枚举类 10 | * 11 | * @Author niujinpeng 12 | * @Date 2018/8/24 14:30 13 | */ 14 | public enum ResponseEnum { 15 | 16 | /** 17 | * 系统总体相关 18 | */ 19 | SUCCESS("0", "success"), 20 | UNKNOW_ERROR("-1", "未知错误"), 21 | ERROR("999", "请求处理失败"); 22 | 23 | @Setter 24 | @Getter 25 | private String code; 26 | 27 | @Setter 28 | @Getter 29 | private String message; 30 | 31 | ResponseEnum(String code, String msg) { 32 | this.code = code; 33 | this.message = msg; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/exception/BaseException.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import net.codingme.boot.enums.ResponseEnum; 7 | 8 | /** 9 | *

10 | * 自定义异常类 11 | * 12 | * @Author niujinpeng 13 | * @Date 2018/8/24 14:39 14 | */ 15 | @Getter 16 | @Setter 17 | public class BaseException extends RuntimeException { 18 | 19 | private String code; 20 | 21 | public BaseException(String message, String code) { 22 | super(message); 23 | this.code = code; 24 | } 25 | 26 | public BaseException(ResponseEnum resultEnum) { 27 | super(resultEnum.getMessage()); 28 | this.code = resultEnum.getCode(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/exception/ErrorAttributesCustom.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.context.request.WebRequest; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | /** 11 | *

12 | * 自定义错误信息JSON值 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/7 15:21 16 | */ 17 | @Component 18 | public class ErrorAttributesCustom extends DefaultErrorAttributes { 19 | 20 | @Override 21 | public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { 22 | Map map = super.getErrorAttributes(webRequest, includeStackTrace); 23 | String code = map.get("status").toString(); 24 | String message = map.get("error").toString(); 25 | map.remove("status"); 26 | map.remove("error"); 27 | HashMap hashMap = new HashMap<>(); 28 | hashMap.put("code", code); 29 | hashMap.put("message", message); 30 | hashMap.put("data", map); 31 | return hashMap; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/exception/ExceptionHandle.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.exception; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.enums.ResponseEnum; 6 | import net.codingme.boot.utils.ResponseUtill; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.ResponseBody; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | 13 | /** 14 | *

15 | * 统一的异常处理 16 | * 17 | * @Author niujinpeng 18 | * @Date 2019/1/7 14:26 19 | */ 20 | 21 | @Slf4j 22 | @ControllerAdvice 23 | public class ExceptionHandle { 24 | 25 | @ResponseBody 26 | @ExceptionHandler(Exception.class) 27 | public Response handleException(Exception e) { 28 | log.info("异常 {}", e); 29 | if (e instanceof BaseException) { 30 | BaseException exception = (BaseException) e; 31 | String code = exception.getCode(); 32 | String message = exception.getMessage(); 33 | return ResponseUtill.error(code, message); 34 | } 35 | return ResponseUtill.error(ResponseEnum.UNKNOW_ERROR); 36 | } 37 | 38 | 39 | /** 40 | * 判断是否是Ajax请求 41 | * 42 | * @param request 43 | * @return 44 | */ 45 | public static boolean isAjax(HttpServletRequest request) { 46 | String rquested = request.getHeader("X-Rquested-With"); 47 | if (request == null) { 48 | return false; 49 | } 50 | if (!"XMLHttpRequest".equals(rquested.toString())) { 51 | return false; 52 | } 53 | return true; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 9 | * @Author niujinpeng 10 | * @Date 2018/12/7 0:05 11 | */ 12 | 13 | public interface UserService { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.service.UserService; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 测试server bean的加载方式 9 | * 1.使用@Service 注解声明 10 | * 2.使用XML配置通过@ImportResource 引入 11 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 12 | * 13 | * @Service 添加到容器中 14 | * @Author niujinpeng 15 | * @Date 2018/12/27 15:59 16 | */ 17 | 18 | @Service 19 | public class UserServiceImpl implements UserService { 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | import net.codingme.boot.enums.ResponseEnum; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collection; 8 | 9 | /** 10 | *

11 | * 方便响应的工具类 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/19 17:33 15 | */ 16 | public class ResponseUtill { 17 | 18 | public static Response success(Object objects) { 19 | ArrayList list = new ArrayList<>(); 20 | if (!(objects instanceof Collection)) { 21 | list.add(objects); 22 | } else { 23 | list = (ArrayList) objects; 24 | } 25 | return new Response("0000", "success", list); 26 | } 27 | 28 | public static Response success() { 29 | return success(null); 30 | } 31 | 32 | public static Response error(String code, String message) { 33 | return new Response(code, message, null); 34 | } 35 | 36 | public static Response error(ResponseEnum responseEnum) { 37 | return error(responseEnum.getCode(), responseEnum.getMessage()); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/resources/config/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/resources/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-web-interceptor/src/main/resources/static/favicon.ico -------------------------------------------------------------------------------- /springboot-web-interceptor/src/main/resources/templates/error/4xx.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | [[${status}]] 7 | 8 | 9 | 10 | 11 |

12 |

错误码:[[${status}]]

13 |

信息:[[${error}]]

14 |

时间:[[${#dates.format(timestamp,'yyyy-MM-dd hh:mm:ss ')}]]

15 |

请求路径:[[${path}]]

16 |
17 | 18 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/test/java/net/codingme/boot/LogbackTest.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | /** 11 | *

12 | * 测试日志输出, 13 | * SLF4J 日志级别从小到大trace,debug,info,warn,error 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/11 21:12 17 | */ 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class LogbackTest { 21 | 22 | Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testLog() { 26 | logger.trace("Trace 日志..."); 27 | logger.debug("Debug 日志..."); 28 | logger.info("Info 日志..."); 29 | logger.warn("Warn 日志..."); 30 | logger.error("Error 日志..."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-web-interceptor/src/test/java/net/codingme/boot/SpringbootWebTemplateApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootWebTemplateApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /systemlog/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.web.servlet.ServletComponentScan; 6 | import org.springframework.context.annotation.ComponentScan; 7 | 8 | @SpringBootApplication 9 | @ServletComponentScan 10 | public class BootApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(BootApplication.class, args); 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/main/java/net/codingme/boot/config/WebCoreConfig.java: -------------------------------------------------------------------------------- 1 | //package net.codingme.boot.config; 2 | // 3 | //import net.codingme.boot.filter.MyFilter; 4 | //import net.codingme.boot.listener.MyListener; 5 | //import net.codingme.boot.servlet.MyServlet; 6 | //import org.springframework.boot.web.servlet.FilterRegistrationBean; 7 | //import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; 8 | //import org.springframework.boot.web.servlet.ServletRegistrationBean; 9 | //import org.springframework.context.annotation.Bean; 10 | //import org.springframework.context.annotation.Configuration; 11 | // 12 | //import javax.servlet.FilterRegistration; 13 | //import javax.servlet.Servlet; 14 | // 15 | ///** 16 | // *

17 | // * 在这里注册Servlet Filter Listener 或者使用 @ServletComponentScan 18 | // * 19 | // * @Author niujinpeng 20 | // * @Date 2019/1/24 16:30 21 | // */ 22 | //@Configuration 23 | //public class WebCoreConfig { 24 | // 25 | // @Bean 26 | // public ServletRegistrationBean myServlet() { 27 | // return new ServletRegistrationBean<>(new MyServlet()); 28 | // } 29 | // 30 | // @Bean 31 | // public FilterRegistrationBean myFitler() { 32 | // return new FilterRegistrationBean<>(new MyFilter()); 33 | // } 34 | // 35 | // @Bean 36 | // public ServletListenerRegistrationBean myListener() { 37 | // return new ServletListenerRegistrationBean(new MyListener()); 38 | // } 39 | // 40 | // 41 | //} 42 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/main/java/net/codingme/boot/filter/MyFilter.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.filter; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | import javax.servlet.*; 6 | import javax.servlet.annotation.WebFilter; 7 | import java.io.IOException; 8 | 9 | /** 10 | *

11 | * 12 | * @Author niujinpeng 13 | * @Date 2019/1/24 16:35 14 | */ 15 | @Slf4j 16 | @WebFilter(urlPatterns = "/*") 17 | public class MyFilter implements Filter { 18 | 19 | 20 | @Override 21 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { 22 | log.info("拦截器开始拦截"); 23 | filterChain.doFilter(request, response); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/main/java/net/codingme/boot/listener/MyListener.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.listener; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | import javax.servlet.ServletContextEvent; 6 | import javax.servlet.ServletContextListener; 7 | import javax.servlet.annotation.WebListener; 8 | 9 | /** 10 | *

11 | * 12 | * @Author niujinpeng 13 | * @Date 2019/1/24 16:45 14 | */ 15 | @Slf4j 16 | @WebListener 17 | public class MyListener implements ServletContextListener { 18 | 19 | @Override 20 | public void contextInitialized(ServletContextEvent sce) { 21 | log.info("监听器开始初始化"); 22 | } 23 | 24 | @Override 25 | public void contextDestroyed(ServletContextEvent sce) { 26 | log.info("监听器开始销毁"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/main/java/net/codingme/boot/servlet/MyServlet.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.servlet; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | import java.io.PrintWriter; 12 | 13 | /** 14 | *

15 | * 16 | * @Author niujinpeng 17 | * @Date 2019/1/24 16:25 18 | */ 19 | @Slf4j 20 | @WebServlet(urlPatterns = "/myservlet") 21 | public class MyServlet extends HttpServlet { 22 | 23 | @Override 24 | public void init() throws ServletException { 25 | log.info("Servlet 开始初始化"); 26 | super.init(); 27 | } 28 | 29 | @Override 30 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 31 | log.info("Servlet 开始处理 GET 方法"); 32 | PrintWriter writer = resp.getWriter(); 33 | writer.println("Hello Servlet"); 34 | writer.flush(); 35 | writer.close(); 36 | } 37 | 38 | @Override 39 | protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 40 | doGet(req, resp); 41 | } 42 | 43 | @Override 44 | public void destroy() { 45 | log.info("Servlet 开始销毁"); 46 | super.destroy(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # \u65E5\u5FD7\u914D\u7F6E 2 | # \u6307\u5B9A\u5177\u4F53\u5305\u7684\u65E5\u5FD7\u7EA7\u522B 3 | logging.level.net.codingme=debug 4 | logging.level. = debug 5 | # \u63A7\u5236\u53F0\u548C\u65E5\u5FD7\u6587\u4EF6\u8F93\u51FA\u683C\u5F0F\u548C\u989C\u8272 6 | spring.output.ansi.enabled=ALWAYS 7 | #logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr([%thread]){yellow} %clr(%-5level){faint} %clr(%logger{50}){cyan} - %msg%n 8 | logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n 9 | # \u65E5\u5FD7\u6587\u4EF6\u5927\u5C0F 10 | logging.file.max-size=100mb 11 | # \u4FDD\u7559\u7684\u65E5\u5FD7\u4E2A\u6570 12 | logging.file.max-history=1 13 | # \u65E5\u5FD7\u8F93\u51FA\u8DEF\u5F84\uFF0C\u9ED8\u8BA4\u6587\u4EF6spring.log 14 | logging.path=systemlog 15 | #logging.file=log.log 16 | -------------------------------------------------------------------------------- /springboot-web-servlet-filter-listener/src/test/java/net/codingme/boot/SpringbootWebServletFilterListenerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootWebServletFilterListenerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /springboot-web-staticfile/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.mvn/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /build/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.CommandLineRunner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.annotation.Bean; 8 | 9 | import java.util.Arrays; 10 | 11 | /** 12 | *

13 | * 用于引入传统的 XML 配置文件 14 | * 15 | * @ImportResource(value = "classpath:spring-service.xml") 16 | * @Author niujinpeng 17 | * @Date 2018/12/7 0:04 18 | */ 19 | 20 | //@ImportResource(value = "classpath:config/spring-service.xml") 21 | @SpringBootApplication 22 | public class BootApplication { 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(BootApplication.class, args); 26 | } 27 | 28 | @Bean 29 | public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 30 | return args -> { 31 | // 开始检查spring boot 提供的 beans 32 | System.out.println("Let's inspect the beans provided by Spring Boot:"); 33 | String[] beanNames = ctx.getBeanDefinitionNames(); 34 | Arrays.sort(beanNames); 35 | for (String beanName : beanNames) { 36 | //System.out.println(beanName); 37 | } 38 | }; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | import com.alibaba.fastjson.support.config.FastJsonConfig; 6 | import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.converter.HttpMessageConverter; 10 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | *

17 | * 1.使用FastJSON 18 | * 2.配置时间格式化 19 | * 3.解决中文乱码 20 | * 21 | * @Author niujinpeng 22 | * @Date 2018/12/13 15:35 23 | */ 24 | @Configuration 25 | public class WebMvcConfig implements WebMvcConfigurer { 26 | 27 | /** 28 | * 自定义JSON转换器 29 | * 30 | * @param converters 31 | */ 32 | @Override 33 | public void configureMessageConverters(List> converters) { 34 | FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); 35 | FastJsonConfig fastJsonConfig = new FastJsonConfig(); 36 | fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); 37 | //日期格式化 38 | fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); 39 | //处理中文乱码问题 40 | List fastMediaTypes = new ArrayList<>(); 41 | fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); 42 | 43 | converter.setSupportedMediaTypes(fastMediaTypes); 44 | converter.setFastJsonConfig(fastJsonConfig); 45 | 46 | converters.add(converter); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | *

11 | * 测试控制器 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/4 14:41 15 | */ 16 | @Slf4j 17 | @RestController 18 | public class HelloController { 19 | 20 | @Value("${bootapp.description}") 21 | private String description; 22 | 23 | @RequestMapping("/") 24 | public String index() { 25 | log.info(description); 26 | return "Greetings from Spring Boot!"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/controller/PersonController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import net.codingme.boot.domain.Person; 4 | import net.codingme.boot.domain.Response; 5 | import net.codingme.boot.utils.ResponseUtill; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.Arrays; 10 | import java.util.Date; 11 | 12 | /** 13 | *

14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/19 17:17 17 | */ 18 | @RestController 19 | public class PersonController { 20 | 21 | @GetMapping(value = "/person") 22 | public Response getPerson() { 23 | Person person = new Person("Darcy", 24, new Date(), Arrays.asList("java", "golang")); 24 | return ResponseUtill.success(person); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/domain/Person.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Date; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | *

13 | * 14 | * @Author niujinpeng 15 | * @Date 2018/12/19 17:13 16 | */ 17 | @Data 18 | @NoArgsConstructor 19 | @AllArgsConstructor 20 | public class Person { 21 | 22 | /** 23 | * 用户名 24 | */ 25 | private String username; 26 | /** 27 | * 年龄 28 | */ 29 | private Integer age; 30 | /** 31 | * 生日 32 | */ 33 | private Date birthday; 34 | /** 35 | * 技能 36 | */ 37 | private List skills; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | 8 | /** 9 | *

10 | * 响应实体类封装 11 | * 12 | * @Author niujinpeng 13 | * @Date 2018/12/19 17:13 14 | */ 15 | 16 | @Data 17 | @AllArgsConstructor 18 | @NoArgsConstructor 19 | public class Response { 20 | /** 21 | * 响应码 22 | */ 23 | private String code; 24 | /** 25 | * 响应信息 26 | */ 27 | private String message; 28 | 29 | /** 30 | * 响应数据 31 | */ 32 | private T data; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/service/PersonService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 测试server bean的加载方式 9 | * 1.使用@Service 注解声明 10 | * 2.使用XML配置通过@ImportResource 引入 11 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 12 | * 13 | * @Service 添加到容器中 14 | * @Author niujinpeng 15 | * @Date 2018/12/7 0:05 16 | */ 17 | @Service 18 | public class PersonService { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | 5 | /** 6 | *

7 | * 8 | * @Author niujinpeng 9 | * @Date 2018/12/19 17:33 10 | */ 11 | public class ResponseUtill { 12 | 13 | public static Response success(Object data) { 14 | return new Response("0000", "success", data); 15 | } 16 | 17 | public static Response success() { 18 | return new Response("0000", "success", null); 19 | } 20 | 21 | public static Response error(String code, String message) { 22 | return new Response(code, message, null); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/resources/config/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/resources/public/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Floating labels example for Bootstrap 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |

43 | 44 | 45 | -------------------------------------------------------------------------------- /springboot-web-staticfile/src/main/resources/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-web-staticfile/src/main/resources/static/favicon.ico -------------------------------------------------------------------------------- /springboot-web-staticfile/src/test/java/net/codingme/boot/LogbackTest.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | /** 11 | *

12 | * 测试日志输出, 13 | * SLF4J 日志级别从小到大trace,debug,info,warn,error 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/11 21:12 17 | */ 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class LogbackTest { 21 | 22 | Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testLog() { 26 | logger.trace("Trace 日志..."); 27 | logger.debug("Debug 日志..."); 28 | logger.info("Info 日志..."); 29 | logger.warn("Warn 日志..."); 30 | logger.error("Error 日志..."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-web-swagger/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | *

8 | * Spring Boot 启动类 9 | *

10 | */ 11 | @SpringBootApplication 12 | public class BootApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(BootApplication.class, args); 16 | } 17 | 18 | } 19 | 20 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/config/ConverterDate.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import org.springframework.core.convert.converter.Converter; 4 | import org.springframework.util.StringUtils; 5 | 6 | import java.text.ParseException; 7 | import java.text.SimpleDateFormat; 8 | import java.util.Date; 9 | 10 | /** 11 | *

12 | * 时间转换器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/11/19 23:17 16 | */ 17 | public class ConverterDate implements Converter { 18 | 19 | private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 20 | 21 | @Override 22 | public Date convert(String s) { 23 | if (StringUtils.isEmpty(s)) { 24 | return null; 25 | } 26 | try { 27 | return simpleDateFormat.parse(s); 28 | } catch (ParseException e) { 29 | e.printStackTrace(); 30 | } 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import springfox.documentation.builders.ApiInfoBuilder; 6 | import springfox.documentation.builders.PathSelectors; 7 | import springfox.documentation.builders.RequestHandlerSelectors; 8 | import springfox.documentation.service.ApiInfo; 9 | import springfox.documentation.spi.DocumentationType; 10 | import springfox.documentation.spring.web.plugins.Docket; 11 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 | 13 | /** 14 | *

15 | * Springfox-swagger2 配置 16 | * 17 | * @Author niujinpeng 18 | * @Date 2019/11/19 23:17 19 | */ 20 | @Configuration 21 | @EnableSwagger2 22 | public class SwaggerConfig { 23 | 24 | @Bean 25 | public Docket createRestApi() { 26 | return new Docket(DocumentationType.SWAGGER_2) 27 | .apiInfo(apiInfo()) 28 | .select() 29 | .apis(RequestHandlerSelectors.basePackage("net.codingme.boot.controller")) 30 | .paths(PathSelectors.any()) 31 | .build(); 32 | } 33 | 34 | private ApiInfo apiInfo() { 35 | return new ApiInfoBuilder() 36 | .title("未读代码 API") 37 | .description("公众号:未读代码(weidudaima) springboot-swagger2 在线借口文档") 38 | .termsOfServiceUrl("https://www.codingme.net") 39 | .contact("达西呀") 40 | .version("1.0") 41 | .build(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.format.FormatterRegistry; 5 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 7 | 8 | /** 9 | *

10 | * 2.配置时间格式化 11 | * 4.添加自定义拦截器 12 | * 5.时间转换器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/11/19 23:17 16 | */ 17 | @Configuration 18 | public class WebMvcConfig implements WebMvcConfigurer { 19 | 20 | 21 | @Override 22 | public void addResourceHandlers(ResourceHandlerRegistry registry) { 23 | //registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); 24 | } 25 | 26 | /** 27 | * 时间转换器 28 | * 29 | * @param registry 30 | */ 31 | @Override 32 | public void addFormatters(FormatterRegistry registry) { 33 | registry.addConverter(new ConverterDate()); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.util.Collection; 10 | 11 | 12 | /** 13 | *

14 | * 响应实体类封装 15 | * 16 | * @Author niujinpeng 17 | * @Date 2019/11/19 23:17 18 | */ 19 | 20 | @Data 21 | @AllArgsConstructor 22 | @NoArgsConstructor 23 | @ApiModel(value = "响应信息") 24 | public class Response { 25 | /** 26 | * 响应码 27 | */ 28 | @ApiModelProperty(value = "响应码") 29 | private String code; 30 | /** 31 | * 响应信息 32 | */ 33 | @ApiModelProperty(value = "响应信息") 34 | private String message; 35 | 36 | /** 37 | * 响应数据 38 | */ 39 | @ApiModelProperty(value = "响应数据") 40 | private Collection content; 41 | } 42 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/domain/User.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | import org.springframework.format.annotation.DateTimeFormat; 9 | 10 | import javax.validation.constraints.NotNull; 11 | import java.util.Date; 12 | 13 | /** 14 | *

15 | * 用户实体类 16 | * 17 | * @Author niujinpeng 18 | * @Date 2018/12/19 17:13 19 | */ 20 | @Data 21 | @NoArgsConstructor 22 | @AllArgsConstructor 23 | @ApiModel(value = "用户对象") 24 | public class User { 25 | 26 | /** 27 | * 用户ID 28 | * 29 | * @Id 主键 30 | * @GeneratedValue 自增主键 31 | */ 32 | @NotNull(message = "用户 ID 不能为空") 33 | @ApiModelProperty(value = "用户ID", required = true, example = "1000") 34 | private Integer id; 35 | 36 | /** 37 | * 用户名 38 | */ 39 | @NotNull(message = "用户名不能为空") 40 | @ApiModelProperty(value = "用户名", required = true) 41 | private String username; 42 | /** 43 | * 密码 44 | */ 45 | @NotNull(message = "密码不能为空") 46 | @ApiModelProperty(value = "用户密码", required = true) 47 | private String password; 48 | /** 49 | * 年龄 50 | */ 51 | @ApiModelProperty(value = "用户年龄", example = "18") 52 | private Integer age; 53 | /** 54 | * 生日 55 | */ 56 | @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") 57 | @ApiModelProperty(value = "用户生日") 58 | private Date birthday; 59 | /** 60 | * 技能 61 | */ 62 | @ApiModelProperty(value = "用户技能") 63 | private String skills; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/enums/ResponseEnum.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.enums; 2 | 3 | 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | /** 8 | *

9 | * 用于系统返回的枚举类 10 | * 11 | * @Author niujinpeng 12 | * @Date 2019/11/19 23:17 13 | */ 14 | public enum ResponseEnum { 15 | 16 | /** 17 | * 系统总体相关 18 | */ 19 | SUCCESS("200", "操作成功"), 20 | UNKNOW_ERROR("-1", "未知错误"), 21 | ERROR("999", "请求处理失败"); 22 | 23 | @Setter 24 | @Getter 25 | private String code; 26 | 27 | @Setter 28 | @Getter 29 | private String message; 30 | 31 | ResponseEnum(String code, String msg) { 32 | this.code = code; 33 | this.message = msg; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | import net.codingme.boot.enums.ResponseEnum; 5 | import org.springframework.util.StringUtils; 6 | 7 | import java.util.ArrayList; 8 | import java.util.Collection; 9 | 10 | /** 11 | *

12 | * 方便响应的工具类 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/11/19 23:17 16 | */ 17 | public class ResponseUtill { 18 | 19 | public static Response success(Object objects) { 20 | ArrayList list = new ArrayList<>(); 21 | if (!(objects instanceof Collection)) { 22 | list.add(objects); 23 | } else { 24 | list = (ArrayList) objects; 25 | } 26 | return new Response(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), list); 27 | } 28 | 29 | public static Response success() { 30 | return success(new ArrayList<>()); 31 | } 32 | 33 | public static Response success(String message) { 34 | Response success = success(new ArrayList<>()); 35 | if (!StringUtils.isEmpty(message)) { 36 | success.setMessage(message); 37 | } 38 | return success; 39 | } 40 | 41 | public static Response error(String code, String message) { 42 | return new Response(code, message, new ArrayList<>()); 43 | } 44 | 45 | public static Response error(ResponseEnum responseEnum) { 46 | return error(responseEnum.getCode(), responseEnum.getMessage()); 47 | } 48 | 49 | public static Response error(ResponseEnum responseEnum, String message) { 50 | return error(responseEnum.getCode(), message); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ############################################################ 2 | # 服务启动端口号 3 | server.port=8080 -------------------------------------------------------------------------------- /springboot-web-swagger/src/main/resources/templates/error/4xx.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | [[${status}]] 7 | 8 | 9 | 10 | 11 |

12 |

错误码:[[${status}]]

13 |

信息:[[${error}]]

14 |

时间:[[${#dates.format(timestamp,'yyyy-MM-dd hh:mm:ss ')}]]

15 |

请求路径:[[${path}]]

16 |
17 | 18 | -------------------------------------------------------------------------------- /springboot-web-template/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/BootApplication.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | *

8 | * Spring Boot 启动类 9 | *

10 | */ 11 | 12 | @SpringBootApplication 13 | public class BootApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(BootApplication.class, args); 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/config/LogHandlerInterceptor.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.config; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | *

12 | * 拦截器 13 | * 14 | * @Author niujinpeng 15 | * @Date 2019/1/6 16:54 16 | */ 17 | @Slf4j 18 | public class LogHandlerInterceptor implements HandlerInterceptor { 19 | 20 | /** 21 | * 请求方法执行之前 22 | * 返回true则通过 23 | * 24 | * @param request 25 | * @param response 26 | * @param handler 27 | * @return 28 | */ 29 | @Override 30 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 31 | StringBuffer requestURL = request.getRequestURL(); 32 | log.info("请求URL:" + requestURL.toString()); 33 | return true; 34 | } 35 | 36 | @Override 37 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { 38 | } 39 | 40 | @Override 41 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.controller; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | *

11 | * 测试控制器 12 | * 13 | * @Author niujinpeng 14 | * @Date 2018/12/4 14:41 15 | */ 16 | @Slf4j 17 | @RestController 18 | public class HelloController { 19 | 20 | @Value("${bootapp.description}") 21 | private String description; 22 | 23 | @RequestMapping("/") 24 | public String index() { 25 | log.info(description); 26 | return "Greetings from Spring Boot!"; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/domain/Response.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | 8 | /** 9 | *

10 | * 响应实体类封装 11 | * 12 | * @Author niujinpeng 13 | * @Date 2018/12/19 17:13 14 | */ 15 | 16 | @Data 17 | @AllArgsConstructor 18 | @NoArgsConstructor 19 | public class Response { 20 | /** 21 | * 响应码 22 | */ 23 | private String code; 24 | /** 25 | * 响应信息 26 | */ 27 | private String message; 28 | 29 | /** 30 | * 响应数据 31 | */ 32 | private T data; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/domain/User.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.Date; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | *

13 | * 14 | * @Data GET SET TOSTRING 15 | * @NoArgsConstructor 无参构造 16 | * @AllArgsConstructor 全参构造 17 | * @Author niujinpeng 18 | * @Date 2018/12/19 17:13 19 | */ 20 | @Data 21 | @NoArgsConstructor 22 | @AllArgsConstructor 23 | public class User { 24 | 25 | /** 26 | * 用户名 27 | */ 28 | private String username; 29 | /** 30 | * 密码 31 | */ 32 | private String password; 33 | /** 34 | * 年龄 35 | */ 36 | private Integer age; 37 | /** 38 | * 生日 39 | */ 40 | private Date birthday; 41 | /** 42 | * 技能 43 | */ 44 | private List skills; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service; 2 | 3 | 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 9 | * @Author niujinpeng 10 | * @Date 2018/12/7 0:05 11 | */ 12 | 13 | public interface UserService { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.service.impl; 2 | 3 | import net.codingme.boot.service.UserService; 4 | import org.springframework.stereotype.Service; 5 | 6 | /** 7 | *

8 | * 测试server bean的加载方式 9 | * 1.使用@Service 注解声明 10 | * 2.使用XML配置通过@ImportResource 引入 11 | * 3.使用@Configuration 结合@Bean 的方式添加到容器 12 | * 13 | * @Service 添加到容器中 14 | * @Author niujinpeng 15 | * @Date 2018/12/27 15:59 16 | */ 17 | 18 | @Service 19 | public class UserServiceImpl implements UserService { 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/java/net/codingme/boot/utils/ResponseUtill.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot.utils; 2 | 3 | import net.codingme.boot.domain.Response; 4 | 5 | /** 6 | *

7 | * 方便响应的工具类 8 | * 9 | * @Author niujinpeng 10 | * @Date 2018/12/19 17:33 11 | */ 12 | public class ResponseUtill { 13 | 14 | public static Response success(Object data) { 15 | return new Response("0000", "success", data); 16 | } 17 | 18 | public static Response success() { 19 | return new Response("0000", "success", null); 20 | } 21 | 22 | public static Response error(String code, String message) { 23 | return new Response(code, message, null); 24 | } 25 | 26 | public static Response error(String message) { 27 | return new Response("-1", message, null); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /springboot-web-template/src/main/resources/config/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 -------------------------------------------------------------------------------- /springboot-web-template/src/main/resources/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niumoo/springboot/f72b270471f3ee2b334d0d60a9ccc57948768f37/springboot-web-template/src/main/resources/static/favicon.ico -------------------------------------------------------------------------------- /springboot-web-template/src/test/java/net/codingme/boot/LogbackTest.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | /** 11 | *

12 | * 测试日志输出, 13 | * SLF4J 日志级别从小到大trace,debug,info,warn,error 14 | * 15 | * @Author niujinpeng 16 | * @Date 2018/12/11 21:12 17 | */ 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class LogbackTest { 21 | 22 | Logger logger = LoggerFactory.getLogger(getClass()); 23 | 24 | @Test 25 | public void testLog() { 26 | logger.trace("Trace 日志..."); 27 | logger.debug("Debug 日志..."); 28 | logger.info("Info 日志..."); 29 | logger.warn("Warn 日志..."); 30 | logger.error("Error 日志..."); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /springboot-web-template/src/test/java/net/codingme/boot/SpringbootWebTemplateApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.codingme.boot; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootWebTemplateApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | 18 | --------------------------------------------------------------------------------