├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ └── maven-wrapper.properties ├── LICENSE ├── README.MD ├── basic-dependency-loosely-coupled ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── looselycoupled │ ├── BubbleSortAlgorithm.java │ ├── Main.java │ ├── QuicksortAlgorithm.java │ ├── SortAlgorithm.java │ └── VeryComplexService.java ├── docs ├── emoji.md └── images │ ├── how_to_config_annotation.jpg │ ├── qr-code.png │ └── stars.gif ├── example-independent-maven-spring-project ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── mvnw ├── mvnw.cmd ├── pom.xml ├── readme.md └── src │ ├── main │ ├── java │ │ └── me │ │ │ └── loda │ │ │ └── spring │ │ │ └── exampleindependentmavenspringproject │ │ │ └── ExampleIndependentMavenSpringProjectApplication.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── me │ └── loda │ └── spring │ └── exampleindependentmavenspringproject │ └── ExampleIndependentMavenSpringProjectApplicationTests.java ├── hibernate-and-hibernate-only ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── hibernate │ │ └── example │ │ ├── App.java │ │ ├── HibernateUtils.java │ │ └── Todo.java │ └── resources │ └── hibernate.cfg.xml ├── jpa-hibernate-criteria ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── jpa │ └── criteria │ ├── App.java │ ├── CustomUserRepository.java │ ├── DatasourceConfig.java │ ├── User.java │ └── UserRepository.java ├── jpa-hibernate-custom-validation ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── hibernate │ └── customvalidation │ ├── App.java │ ├── LodaId.java │ ├── LodaIdValidator.java │ ├── User.java │ └── UserController.java ├── jpa-hibernate-many-to-many ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── jpa │ │ └── manytomany │ │ ├── AbstractRepository.java │ │ ├── Address.java │ │ ├── AddressRepository.java │ │ ├── ManyToManyExampleApplication.java │ │ ├── Person.java │ │ └── PersonRepository.java │ └── resources │ └── application.properties ├── jpa-hibernate-one-to-many ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── jpa │ │ └── manytomany │ │ ├── Address.java │ │ ├── AddressRepository.java │ │ ├── OneToManyExampleApplication.java │ │ ├── Person.java │ │ └── PersonRepository.java │ └── resources │ └── application.properties ├── jpa-hibernate-one-to-one ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── jpa │ │ └── manytomany │ │ ├── Address.java │ │ ├── AddressRepository.java │ │ ├── OneToOneExampleApplication.java │ │ ├── Person.java │ │ └── PersonRepository.java │ └── resources │ └── application.properties ├── jpa-hibernate-pageable ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── pageable │ ├── App.java │ ├── DatasourceConfig.java │ ├── User.java │ └── UserRepository.java ├── jpa-hibernate-specifications ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── specification │ ├── App.java │ ├── DatasourceConfig.java │ ├── User.java │ ├── UserRepository.java │ └── UserSpecification.java ├── mvnw ├── mvnw.cmd ├── pom.xml ├── spring-boot-1-helloworld-@Component-@Autowired ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── helloworld │ │ ├── App.java │ │ ├── Bikini.java │ │ ├── Girl.java │ │ └── Outfit.java │ └── resources │ └── application.yml ├── spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── postmappingmodelattibute │ │ ├── App.java │ │ ├── Todo.java │ │ └── WebController.java │ └── resources │ ├── static │ ├── css │ │ ├── bootstrap.css │ │ └── main.css │ └── js │ │ └── bootstrap.js │ └── templates │ ├── addTodo.html │ ├── index.html │ ├── listTodo.html │ └── success.html ├── spring-boot-11-JPA-MySQL ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── jpamysql │ │ ├── App.java │ │ ├── User.java │ │ └── UserRepository.java │ └── resources │ └── application.properties ├── spring-boot-12-jpa-method-and-@Query-@Param ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── jpaquery │ │ ├── App.java │ │ ├── User.java │ │ └── UserRepository.java │ └── resources │ └── application.properties ├── spring-boot-13-spring-boot-thymeleaf-mysql-web ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── thymeleafweb │ │ ├── App.java │ │ ├── config │ │ └── TodoConfig.java │ │ ├── controller │ │ └── TodoController.java │ │ ├── model │ │ ├── Todo.java │ │ └── TodoValidator.java │ │ ├── repository │ │ └── TodoRepository.java │ │ └── service │ │ └── TodoService.java │ └── resources │ ├── application.properties │ ├── i18n │ ├── messages.properties │ ├── messages_en.properties │ └── messages_vi.properties │ ├── static │ ├── css │ │ ├── bootstrap.css │ │ └── main.css │ └── js │ │ └── bootstrap.js │ └── templates │ ├── addTodo.html │ ├── failed.html │ ├── index.html │ ├── listTodo.html │ └── success.html ├── spring-boot-14-restful-api-@RestController-@RequestBody-@PathVariable ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── restfulapi │ │ ├── App.java │ │ ├── RestApiController.java │ │ └── Todo.java │ └── resources │ └── application.properties ├── spring-boot-15-@ControllerAdvice-@RestControllerAdvice-@ExceptionHandler-@ResponseStatus ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── exceptionhandling │ ├── ApiExceptionHandler.java │ ├── App.java │ ├── ErrorMessage.java │ ├── RestApiController.java │ └── Todo.java ├── spring-boot-16-@ConfigurationProperties ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── configurationpropertiesanno │ │ ├── App.java │ │ └── LodaAppProperties.java │ └── resources │ └── application.yml ├── spring-boot-17-spring-profiles ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── springprofiles │ │ ├── App.java │ │ ├── ApplicationInitializer.java │ │ ├── LocalDatasource.java │ │ └── LocalDatasourceConfig.java │ └── resources │ ├── application-aws.yml │ ├── application-common.yml │ ├── application-local.yml │ └── application.yml ├── spring-boot-18-testing-in-spring-boot ├── pom.xml ├── readme.md └── src │ ├── main │ └── java │ │ └── me │ │ └── loda │ │ └── spring │ │ └── testinginspringboot │ │ ├── App.java │ │ ├── Todo.java │ │ ├── TodoRepository.java │ │ ├── TodoRestController.java │ │ └── TodoService.java │ └── test │ └── java │ └── me │ └── loda │ └── spring │ └── testinginspringboot │ ├── TodoRestControllerTest.java │ ├── TodoServiceTest.java │ └── TodoServiceTest2.java ├── spring-boot-19-testing-in-spring-boot-2 ├── pom.xml ├── readme.md └── src │ ├── main │ └── java │ │ └── me │ │ └── loda │ │ └── spring │ │ └── testinginspringboot2 │ │ ├── App.java │ │ ├── Todo.java │ │ ├── TodoRepository.java │ │ ├── TodoRestController.java │ │ └── TodoService.java │ └── test │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── testinginspringboot2 │ │ ├── DataJpaAnnotationTest.java │ │ └── SqlAnnotationTest.java │ └── resources │ └── createTodo.sql ├── spring-boot-2-helloworld-@Primary - @Qualifier ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me.loda.spring.helloprimaryqualifier │ ├── App.java │ ├── Bikini.java │ ├── Girl.java │ ├── Naked.java │ └── Outfit.java ├── spring-boot-3-bean-life-cycle-@PostConstruct-@PreDestroy ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── beanlifecycle │ ├── App.java │ └── Girl.java ├── spring-boot-4-@Component-@Service-@Repository ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── componentservicerepository │ │ ├── App.java │ │ ├── Girl.java │ │ ├── GirlRepository.java │ │ ├── GirlRepositoryImpl.java │ │ └── GirlService.java │ └── resources │ └── application.properties ├── spring-boot-5-Component-Scan ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── componentscan │ ├── App.java │ ├── Girl.java │ ├── others │ └── OtherGirl.java │ └── others2 │ └── OtherGirl2.java ├── spring-boot-6-@configuration-@Bean ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── configurationbean │ ├── App.java │ ├── AppConfig.java │ ├── DatabaseConnector.java │ ├── MongoDbConnector.java │ ├── MySqlConnector.java │ ├── PostgreSqlConnector.java │ └── SimpleBean.java ├── spring-boot-7-spring-application-properties-@Value ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── applicationproperties │ │ ├── App.java │ │ ├── AppConfig.java │ │ ├── DatabaseConnector.java │ │ └── MySqlConnector.java │ └── resources │ └── application.properties ├── spring-boot-8-@Controller-web-helloworld ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── controllerweb │ │ ├── App.java │ │ └── WebController.java │ └── resources │ └── templates │ ├── about.html │ ├── hello.html │ └── index.html ├── spring-boot-9-thymeleaf ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── thymeleaf │ │ ├── App.java │ │ ├── Info.java │ │ └── WebController.java │ └── resources │ ├── application.properties │ ├── i18n │ ├── messages.properties │ ├── messages_en.properties │ └── messages_vi.properties │ ├── static │ ├── css │ │ └── bootstrap.css │ └── js │ │ └── bootstrap.js │ └── templates │ ├── index.html │ └── profile.html ├── spring-boot-@Conditional-2-custom-conditional ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── customconditional │ ├── App.java │ ├── AppConfiguration.java │ ├── ConditionalOnWindow.java │ ├── MacRequired.java │ ├── WindowOrMacRequired.java │ └── WindowRequired.java ├── spring-boot-@Conditional ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── conditional │ │ ├── ABeanWithCondition.java │ │ ├── ABeanWithCondition2.java │ │ ├── App.java │ │ ├── ConditionalOnBeanExample.java │ │ ├── ConditionalOnExpressionExample.java │ │ ├── ConditionalOnMissingBeanExample.java │ │ ├── ConditionalOnPropertyExample.java │ │ ├── ConditionalOnResourceExample.java │ │ └── RandomBean.java │ └── resources │ └── application.properties ├── spring-boot-@EventListener-@Async ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── springeventlistener │ ├── App.java │ ├── DoorBellEvent.java │ ├── ListenerConfiguration.java │ ├── MyDog.java │ └── MyHouse.java ├── spring-boot-@Lazy-Anotation ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── lazy │ └── anotation │ ├── ApplicationConfig.java │ ├── ExampleApplication.java │ ├── FirstBean.java │ ├── SecondBean.java │ └── ServiceBean.java ├── spring-boot-application-context-events ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── applicationcontextevent │ └── App.java ├── spring-boot-jpa-auditing ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── jpaauditing │ │ ├── App.java │ │ ├── AppParams.java │ │ └── AppParamsRepository.java │ └── resources │ ├── application.properties │ └── init.sql ├── spring-boot-swagger2 ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── spring │ └── swagger │ ├── App.java │ ├── config │ └── Swagger2Config.java │ ├── controller │ └── UserController.java │ ├── model │ └── User.java │ └── repository │ └── UserRepository.java ├── spring-boot-swagger3-openapi3 ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me.loda.spring.openapi3 │ ├── App.java │ ├── config │ └── OpenApiConfig.java │ ├── controller │ └── UserController.java │ ├── model │ └── User.java │ └── repository │ └── UserRepository.java ├── spring-boot-webflux ├── pom.xml ├── readme.md └── src │ ├── main │ └── java │ │ └── loda │ │ └── me │ │ ├── Employee.java │ │ ├── EmployeeApplication.java │ │ ├── EmployeeController.java │ │ ├── EmployeeService.java │ │ └── EmployeeServiceImpl.java │ └── test │ └── java │ └── loda │ └── me │ └── EmployeeControllerTest.java ├── spring-cloud-config-client ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ ├── ClientService.java │ │ └── InfoRestController.java │ └── resources │ └── bootstrap.properties ├── spring-cloud-config-server ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── springcloudconfig │ │ └── ConfigServer.java │ └── resources │ ├── bootstrap.properties │ └── config │ ├── Service1.properties │ ├── Service2.properties │ └── global.properties ├── spring-configuration-properties ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── configurationproperties │ │ ├── App.java │ │ └── LodaAppProperties.java │ └── resources │ └── application.yml ├── spring-redis ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── springredis │ │ ├── App.java │ │ ├── RedisConfig.java │ │ └── RedisExample.java │ └── resources │ └── application.properties ├── spring-security-example ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── springsecurity │ │ ├── App.java │ │ ├── WebController.java │ │ └── WebSecurityConfig.java │ └── resources │ └── templates │ ├── hello.html │ └── home.html ├── spring-security-hibernate-jwt ├── pom.xml ├── readme.md └── src │ └── main │ └── java │ └── me │ └── loda │ └── springsecurityhibernatejwt │ ├── App.java │ ├── LodaRestController.java │ ├── WebSecurityConfig.java │ ├── jwt │ ├── JwtAuthenticationFilter.java │ └── JwtTokenProvider.java │ ├── payload │ ├── LoginRequest.java │ ├── LoginResponse.java │ └── RandomStuff.java │ └── user │ ├── CustomUserDetails.java │ ├── User.java │ ├── UserRepository.java │ └── UserService.java ├── spring-security-hibernate ├── pom.xml ├── readme.md └── src │ └── main │ ├── java │ └── me │ │ └── loda │ │ └── spring │ │ └── springsecurityhibernate │ │ ├── App.java │ │ ├── WebController.java │ │ ├── WebSecurityConfig.java │ │ └── user │ │ ├── CustomUserDetails.java │ │ ├── User.java │ │ ├── UserRepository.java │ │ └── UserService.java │ └── resources │ ├── application.properties │ └── templates │ ├── hello.html │ └── home.html └── test-mockito-basic ├── pom.xml ├── readme.md └── src ├── main └── java │ └── me │ └── loda │ └── springtest │ └── App.java └── test └── java └── me └── loda └── springtest ├── CaptorTest.java ├── InjectMockTest.java ├── MockitoAnnotationTest.java └── SpyTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | .DS_Store 5 | 6 | ### STS ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | 15 | ### IntelliJ IDEA ### 16 | .idea 17 | *.iws 18 | *.iml 19 | *.ipr 20 | 21 | ### NetBeans ### 22 | /nbproject/private/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ 27 | /build/ 28 | 29 | ### VS Code ### 30 | .vscode/ 31 | 32 | /**/target 33 | # Compiled class file 34 | *.class 35 | 36 | # Log file 37 | *.log 38 | 39 | # BlueJ files 40 | *.ctxt 41 | 42 | # Mobile Tools for Java (J2ME) 43 | .mtj.tmp/ 44 | 45 | # Package Files # 46 | *.jar 47 | *.war 48 | *.nar 49 | *.ear 50 | *.zip 51 | *.tar.gz 52 | *.rar 53 | 54 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 55 | hs_err_pid* 56 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nguyen Hoang Nam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /basic-dependency-loosely-coupled/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | basic-dependency-loosely-coupled 13 | 14 | 15 | -------------------------------------------------------------------------------- /basic-dependency-loosely-coupled/src/main/java/me/loda/spring/looselycoupled/BubbleSortAlgorithm.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.looselycoupled; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.Collections; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 5/8/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | public class BubbleSortAlgorithm implements SortAlgorithm{ 21 | 22 | @Override 23 | public void sort(int[] array) { 24 | // TODO: Add your logic here 25 | System.out.println("Đã sắp xếp bằng thuật toán sx nổi bọt"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /basic-dependency-loosely-coupled/src/main/java/me/loda/spring/looselycoupled/Main.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.looselycoupled; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/8/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class Main { 19 | public static void main(String[] args) { 20 | SortAlgorithm bubbleSortAlgorithm = new BubbleSortAlgorithm(); 21 | SortAlgorithm quickSortAlgorithm = new QuicksortAlgorithm(); 22 | VeryComplexService business1 = new VeryComplexService(bubbleSortAlgorithm); 23 | VeryComplexService business2 = new VeryComplexService(quickSortAlgorithm); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /basic-dependency-loosely-coupled/src/main/java/me/loda/spring/looselycoupled/QuicksortAlgorithm.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.looselycoupled; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/8/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class QuicksortAlgorithm implements SortAlgorithm { 19 | @Override 20 | public void sort(int[] array) { 21 | // TODO: Add your logic here 22 | System.out.println("Đã sắp xếp bằng thuật sx nhanh"); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /basic-dependency-loosely-coupled/src/main/java/me/loda/spring/looselycoupled/SortAlgorithm.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.looselycoupled; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/8/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public interface SortAlgorithm { 19 | /** 20 | * Sắp xếp mảng đầu vào 21 | * @param array 22 | */ 23 | public void sort(int array[]); 24 | } 25 | -------------------------------------------------------------------------------- /basic-dependency-loosely-coupled/src/main/java/me/loda/spring/looselycoupled/VeryComplexService.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.looselycoupled; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/8/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class VeryComplexService { 19 | private SortAlgorithm sortAlgorithm; 20 | public VeryComplexService(SortAlgorithm sortAlgorithm){ 21 | this.sortAlgorithm = sortAlgorithm; 22 | } 23 | 24 | public void complexBusiness(int array[]){ 25 | sortAlgorithm.sort(array); 26 | // TODO: more logic here 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /docs/images/how_to_config_annotation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TranDatk/spring-boot-learning/c8735c57074d7ab72614b5574d38eac77f72295c/docs/images/how_to_config_annotation.jpg -------------------------------------------------------------------------------- /docs/images/qr-code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TranDatk/spring-boot-learning/c8735c57074d7ab72614b5574d38eac77f72295c/docs/images/qr-code.png -------------------------------------------------------------------------------- /docs/images/stars.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TranDatk/spring-boot-learning/c8735c57074d7ab72614b5574d38eac77f72295c/docs/images/stars.gif -------------------------------------------------------------------------------- /example-independent-maven-spring-project/.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 | -------------------------------------------------------------------------------- /example-independent-maven-spring-project/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TranDatk/spring-boot-learning/c8735c57074d7ab72614b5574d38eac77f72295c/example-independent-maven-spring-project/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /example-independent-maven-spring-project/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.2/apache-maven-3.6.2-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar 3 | -------------------------------------------------------------------------------- /example-independent-maven-spring-project/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | 3 | This is just a example Maven project with Spring Boot Framework 4 | 5 | [Loda.me][loda-link] 6 | 7 | [loda-link]: https://loda.me 8 | 9 | -------------------------------------------------------------------------------- /example-independent-maven-spring-project/src/main/java/me/loda/spring/exampleindependentmavenspringproject/ExampleIndependentMavenSpringProjectApplication.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.exampleindependentmavenspringproject; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ExampleIndependentMavenSpringProjectApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ExampleIndependentMavenSpringProjectApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /example-independent-maven-spring-project/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /example-independent-maven-spring-project/src/test/java/me/loda/spring/exampleindependentmavenspringproject/ExampleIndependentMavenSpringProjectApplicationTests.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.exampleindependentmavenspringproject; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ExampleIndependentMavenSpringProjectApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /hibernate-and-hibernate-only/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | hibernate-and-hibernate-only 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 8.0.16 20 | 21 | 22 | 23 | 24 | org.hibernate 25 | hibernate-core 26 | 5.4.2.Final 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /hibernate-and-hibernate-only/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | Vào link để xem chi tiết có hình ảnh minh họa: 3 | 4 | [Loda.me - Hibernate là gì?][loda-link] 5 | 6 | [loda-link]: https://loda.me/hibernate-la-gi-loda1554623701594 7 | 8 | # Content without images -------------------------------------------------------------------------------- /hibernate-and-hibernate-only/src/main/java/me/loda/hibernate/example/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.hibernate.example; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Entity; 12 | import javax.persistence.GeneratedValue; 13 | import javax.persistence.GenerationType; 14 | import javax.persistence.Id; 15 | 16 | import lombok.Data; 17 | 18 | /** 19 | * Copyright 2019 {@author Loda} (https://loda.me). 20 | * This project is licensed under the MIT license. 21 | * 22 | * @since 5/26/2019 23 | * Github: https://github.com/loda-kun 24 | */ 25 | @Entity 26 | @Data 27 | public class Todo { 28 | @Id 29 | @GeneratedValue(strategy = GenerationType.IDENTITY) 30 | private Long id; 31 | 32 | private String title; 33 | private String detail; 34 | } 35 | -------------------------------------------------------------------------------- /jpa-hibernate-criteria/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-criteria 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | 28 | 29 | org.hibernate 30 | hibernate-jpamodelgen 31 | 5.4.9.Final 32 | provided 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /jpa-hibernate-criteria/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | 3 | Vào link để xem chi tiết có hình ảnh minh họa: 4 | 5 | [Loda.me - Hướng dẫn sử dụng Criteria API trong Hibernate (Phần 2)][loda-link] 6 | 7 | [loda-link]: https://loda.me/huong-dan-su-dung-criteria-api-trong-hibernate-phan-2-loda1575949746794 8 | 9 | # Content without images -------------------------------------------------------------------------------- /jpa-hibernate-criteria/src/main/java/me/loda/jpa/criteria/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.criteria; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Entity; 12 | import javax.persistence.GeneratedValue; 13 | import javax.persistence.GenerationType; 14 | import javax.persistence.Id; 15 | 16 | import lombok.Builder; 17 | import lombok.Data; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 12/10/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Entity 27 | @Data 28 | @Builder 29 | public class User { 30 | @Id 31 | @GeneratedValue(strategy = GenerationType.IDENTITY) 32 | private Long id; 33 | 34 | private UserType type; 35 | private String name; 36 | 37 | public enum UserType { 38 | NORMAL, VIP; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /jpa-hibernate-criteria/src/main/java/me/loda/jpa/criteria/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.criteria; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 12/10/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | 22 | public interface UserRepository extends JpaRepository, 23 | JpaSpecificationExecutor { 24 | } 25 | -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-custom-validation 13 | 14 | 15 | 16 | 17 | org.hibernate 18 | hibernate-validator 19 | 6.1.0.Final 20 | 21 | 22 | -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | Vào link để xem chi tiết có hình ảnh minh họa: 3 | 4 | [Loda.me - Hướng dẫn Custom Validator][loda-link] 5 | 6 | [loda-link]: https://loda.me/spring-boot-huong-dan-tu-tao-validator-de-kiem-tra-model-and-entity-loda1576748879560 7 | 8 | # Content without images 9 | 10 | ### Giới thiệu -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/src/main/java/me/loda/hibernate/customvalidation/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.hibernate.customvalidation; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | public static void main(String[] args) { 9 | SpringApplication.run(App.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/src/main/java/me/loda/hibernate/customvalidation/LodaId.java: -------------------------------------------------------------------------------- 1 | package me.loda.hibernate.customvalidation; 2 | 3 | import javax.validation.Constraint; 4 | import javax.validation.Payload; 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * Khai báo một custom annotation 9 | */ 10 | @Documented 11 | @Constraint(validatedBy = LodaIdValidator.class) 12 | @Target({ElementType.METHOD, ElementType.FIELD}) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | public @interface LodaId { 15 | // trường message là bắt buộc, khai báo nội dung sẽ trả về khi field k hợp lệ 16 | String message() default "LodaId must start with loda://"; 17 | // Cái này là bắt buộc phải có để Hibernate Validator có thể hoạt động 18 | Class[] groups() default {}; 19 | // Cái này là bắt buộc phải có để Hibernate Validator có thể hoạt động 20 | Class[] payload() default {}; 21 | } 22 | -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/src/main/java/me/loda/hibernate/customvalidation/LodaIdValidator.java: -------------------------------------------------------------------------------- 1 | package me.loda.hibernate.customvalidation; 2 | 3 | import javax.validation.ConstraintValidator; 4 | import javax.validation.ConstraintValidatorContext; 5 | 6 | public class LodaIdValidator implements ConstraintValidator { 7 | private static final String LODA_PREFIX = "loda://"; 8 | 9 | /** 10 | * Kiểm tra tính hợp lệ của trường được đánh dấu bởi @LodaId 11 | */ 12 | @Override 13 | public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) { 14 | if (s == null || s.isEmpty()) return false; 15 | 16 | return s.startsWith(LODA_PREFIX); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/src/main/java/me/loda/hibernate/customvalidation/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.hibernate.customvalidation; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class User { 7 | // Đánh dấu field lodaId sẽ cần validate bởi @LodaId 8 | @LodaId 9 | private String lodaId; 10 | } 11 | -------------------------------------------------------------------------------- /jpa-hibernate-custom-validation/src/main/java/me/loda/hibernate/customvalidation/UserController.java: -------------------------------------------------------------------------------- 1 | package me.loda.hibernate.customvalidation; 2 | 3 | import org.springframework.validation.BindingResult; 4 | import org.springframework.web.bind.annotation.PostMapping; 5 | import org.springframework.web.bind.annotation.RequestBody; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import javax.validation.Valid; 10 | 11 | @RestController 12 | @RequestMapping("api/v1/users") 13 | public class UserController { 14 | 15 | /* 16 | Đánh dấu object với @Valid để validator tự động kiểm tra object đó có hợp lệ hay không 17 | */ 18 | @PostMapping 19 | public Object createUser(@Valid @RequestBody User user) { 20 | return user; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /jpa-hibernate-many-to-many/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-many-to-many 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | -------------------------------------------------------------------------------- /jpa-hibernate-many-to-many/src/main/java/me/loda/jpa/manytomany/AbstractRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | 3 | import java.io.Serializable; 4 | import java.util.Optional; 5 | 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 8 | import org.springframework.data.repository.NoRepositoryBean; 9 | 10 | /** 11 | * Copyright 2019 {@author Loda} (https://loda.me). 12 | * This project is licensed under the MIT license. 13 | * 14 | * @since 2019-06-18 15 | * Github: https://github.com/loda-kun 16 | */ 17 | @NoRepositoryBean 18 | public interface AbstractRepository extends JpaRepository, 19 | JpaSpecificationExecutor{ 20 | 21 | T findById(ID id); 22 | } 23 | -------------------------------------------------------------------------------- /jpa-hibernate-many-to-many/src/main/java/me/loda/jpa/manytomany/AddressRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 4/5/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | // Kế thừa JpaRepository để giao tiếp với database 21 | public interface AddressRepository extends JpaRepository { 22 | } 23 | -------------------------------------------------------------------------------- /jpa-hibernate-many-to-many/src/main/java/me/loda/jpa/manytomany/PersonRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 4/5/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | public interface PersonRepository extends JpaRepository { 21 | } 22 | -------------------------------------------------------------------------------- /jpa-hibernate-many-to-many/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 7 | # Enabling H2 Console 8 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /jpa-hibernate-one-to-many/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-one-to-many 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-many/src/main/java/me/loda/jpa/manytomany/AddressRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 4/5/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | // Kế thừa JpaRepository để giao tiếp với database 21 | public interface AddressRepository extends JpaRepository { 22 | } 23 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-many/src/main/java/me/loda/jpa/manytomany/PersonRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 4/5/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | public interface PersonRepository extends JpaRepository { 21 | } 22 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-many/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 7 | # Enabling H2 Console 8 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /jpa-hibernate-one-to-one/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-one-to-one 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-one/src/main/java/me/loda/jpa/manytomany/Address.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Entity; 12 | import javax.persistence.GeneratedValue; 13 | import javax.persistence.Id; 14 | import javax.persistence.JoinColumn; 15 | import javax.persistence.OneToOne; 16 | 17 | import lombok.Builder; 18 | import lombok.Data; 19 | 20 | /** 21 | * Copyright 2019 {@author Loda} (https://loda.me). 22 | * This project is licensed under the MIT license. 23 | * 24 | * @since 4/5/2019 25 | * Github: https://github.com/loda-kun 26 | */ 27 | @Entity // Đánh dấu đây là table trong db 28 | @Data // lombok giúp generate các hàm constructor, get, set v.v. 29 | @Builder // lombok giúp tạo class builder 30 | public class Address { 31 | 32 | @Id //Đánh dấu là primary key 33 | @GeneratedValue // Giúp tự động tăng 34 | private Long id; 35 | 36 | private String city; 37 | private String province; 38 | 39 | @OneToOne // Quan hệ 1-1 với đối tượng ở dưới (Person) 40 | @JoinColumn(name = "person_id") // thông qua khóa ngoại person_id 41 | private Person person; 42 | } 43 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-one/src/main/java/me/loda/jpa/manytomany/AddressRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 4/5/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | // Kế thừa JpaRepository để giao tiếp với database 21 | public interface AddressRepository extends JpaRepository { 22 | } 23 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-one/src/main/java/me/loda/jpa/manytomany/Person.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Entity; 12 | import javax.persistence.GeneratedValue; 13 | import javax.persistence.Id; 14 | 15 | import lombok.Builder; 16 | import lombok.Data; 17 | 18 | /** 19 | * Copyright 2019 {@author Loda} (https://loda.me). 20 | * This project is licensed under the MIT license. 21 | * 22 | * @since 4/5/2019 23 | * Github: https://github.com/loda-kun 24 | */ 25 | @Entity 26 | @Data 27 | @Builder 28 | public class Person { 29 | 30 | @Id 31 | @GeneratedValue 32 | private Long id; 33 | private String name; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-one/src/main/java/me/loda/jpa/manytomany/PersonRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.jpa.manytomany; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 4/5/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | public interface PersonRepository extends JpaRepository { 21 | } 22 | -------------------------------------------------------------------------------- /jpa-hibernate-one-to-one/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 7 | # Enabling H2 Console 8 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /jpa-hibernate-pageable/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-pageable 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /jpa-hibernate-pageable/src/main/java/me/loda/spring/pageable/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.pageable; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Entity; 12 | import javax.persistence.GeneratedValue; 13 | import javax.persistence.GenerationType; 14 | import javax.persistence.Id; 15 | 16 | import lombok.Builder; 17 | import lombok.Data; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 6/16/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Entity 27 | @Data 28 | @Builder 29 | public class User { 30 | @Id 31 | @GeneratedValue(strategy = GenerationType.IDENTITY) 32 | private Long id; 33 | 34 | private String name; 35 | } 36 | -------------------------------------------------------------------------------- /jpa-hibernate-pageable/src/main/java/me/loda/spring/pageable/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.pageable; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.springframework.data.domain.Pageable; 14 | import org.springframework.data.jpa.repository.JpaRepository; 15 | 16 | /** 17 | * Copyright 2019 {@author Loda} (https://loda.me). 18 | * This project is licensed under the MIT license. 19 | * 20 | * @since 6/16/2019 21 | * Github: https://github.com/loda-kun 22 | */ 23 | 24 | public interface UserRepository extends JpaRepository { 25 | List findAllByNameLike(String name, Pageable pageable); 26 | } 27 | -------------------------------------------------------------------------------- /jpa-hibernate-specifications/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | jpa-hibernate-specifications 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | 28 | 29 | org.hibernate 30 | hibernate-jpamodelgen 31 | 5.4.9.Final 32 | provided 33 | 34 | 35 | -------------------------------------------------------------------------------- /jpa-hibernate-specifications/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | Vào link để xem chi tiết có hình ảnh minh họa: 3 | 4 | [Loda.me - [Spring JPA] Hướng dẫn sử dụng Specification (Phần 1)][loda-link] 5 | 6 | [loda-link]: https://loda.me/spring-jpa-huong-dan-su-dung-specification-phan-1-loda1575947295198 7 | 8 | # Content without images -------------------------------------------------------------------------------- /jpa-hibernate-specifications/src/main/java/me/loda/spring/specification/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.specification; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Entity; 12 | import javax.persistence.GeneratedValue; 13 | import javax.persistence.GenerationType; 14 | import javax.persistence.Id; 15 | 16 | import lombok.Builder; 17 | import lombok.Data; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 12/10/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Entity 27 | @Data 28 | @Builder 29 | public class User { 30 | @Id 31 | @GeneratedValue(strategy = GenerationType.IDENTITY) 32 | private Long id; 33 | 34 | private UserType type; 35 | private String name; 36 | 37 | public enum UserType { 38 | NORMAL, VIP; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /jpa-hibernate-specifications/src/main/java/me/loda/spring/specification/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.specification; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 12/10/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | 22 | public interface UserRepository extends JpaRepository, 23 | JpaSpecificationExecutor { 24 | } 25 | -------------------------------------------------------------------------------- /jpa-hibernate-specifications/src/main/java/me/loda/spring/specification/UserSpecification.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.specification; 2 | 3 | import java.util.Collection; 4 | 5 | import org.springframework.data.jpa.domain.Specification; 6 | 7 | import me.loda.spring.specification.User.UserType; 8 | 9 | /** 10 | * Copyright 2019 {@author Loda} (https://loda.me). 11 | * This project is licensed under the MIT license. 12 | * 13 | * @since 12/10/2019 14 | * Github: https://github.com/loda-kun 15 | */ 16 | public final class UserSpecification { 17 | /** 18 | * Lấy ra user có UserType chỉ định 19 | * @param type 20 | * @return 21 | */ 22 | public static Specification hasType(UserType type) { 23 | return (root, query, cb) -> cb.equal(root.get(User_.TYPE), type); 24 | } 25 | 26 | /** 27 | * Lấy ra user có id chỉ định 28 | * @param userId 29 | * @return 30 | */ 31 | public static Specification hasId(long userId) { 32 | return (root, query, cb) -> cb.equal(root.get(User_.ID), userId); 33 | } 34 | 35 | /** 36 | * Lấy ra user nằm trong tập ID chỉ định 37 | * @param userIds 38 | * @return 39 | */ 40 | public static Specification hasIdIn(Collection userIds) { 41 | return (root, query, cb) -> root.get(User_.ID).in(userIds); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spring-boot-1-helloworld-@Component-@Autowired/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-helloworld 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-1-helloworld-@Component-@Autowired/src/main/java/me/loda/spring/helloworld/Bikini.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloworld; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.context.annotation.Scope; 12 | import org.springframework.stereotype.Component; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/11/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | /* 22 | Đánh dấu class bằng @Component 23 | Class này sẽ được Spring Boot hiểu là một Bean (hoặc dependency) 24 | Và sẽ được Spring Boot quản lý 25 | */ 26 | @Component 27 | // @Scope("prototype") 28 | public class Bikini implements Outfit { 29 | @Override 30 | public void wear() { 31 | System.out.println("Mặc bikini"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-boot-1-helloworld-@Component-@Autowired/src/main/java/me/loda/spring/helloworld/Girl.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloworld; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Component; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/11/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Component 22 | public class Girl { 23 | 24 | @Autowired 25 | Outfit outfit; 26 | 27 | public Girl(Outfit outfit) { 28 | this.outfit = outfit; 29 | } 30 | 31 | // GET 32 | // SET 33 | } 34 | -------------------------------------------------------------------------------- /spring-boot-1-helloworld-@Component-@Autowired/src/main/java/me/loda/spring/helloworld/Outfit.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloworld; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/11/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public interface Outfit { 19 | public void wear(); 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-1-helloworld-@Component-@Autowired/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-thymeleaf 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/java/me/loda/spring/postmappingmodelattibute/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.postmappingmodelattibute; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | public static void main(String[] args) { 9 | SpringApplication.run(App.class, args); 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/java/me/loda/spring/postmappingmodelattibute/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.postmappingmodelattibute; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class Todo { 7 | public String title; 8 | public String detail; 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/resources/static/css/main.css: -------------------------------------------------------------------------------- 1 | body{ 2 | padding: 1em; 3 | } -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/resources/templates/addTodo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |

To-do

18 | 19 |
20 |

title:

21 |

detail:

22 |

23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

Todo App

17 | 18 | Xem danh sách 19 | + Thêm công việc 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/resources/templates/listTodo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

Danh sách việc cần làm:

17 | 18 | 19 |
    20 | 21 |
  • 22 | 23 | : 24 |
  • 25 |
26 | 27 | + Thêm công việc 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-10-@PostMapping-@ModelAttribute-thymeleaf/src/main/resources/templates/success.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

To-do

14 |

Thêm thành công!

15 | 16 | Xem danh sách công việc 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-11-JPA-MySQL/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-11-JPA-MySQL 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | mysql 22 | mysql-connector-java 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-boot-11-JPA-MySQL/src/main/java/me/loda/spring/jpamysql/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.jpamysql; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | import lombok.Data; 13 | 14 | @Entity 15 | @Table(name = "user") 16 | @Data 17 | public class User implements Serializable { 18 | private static final long serialVersionUID = -297553281792804396L; 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | private Long id; 23 | 24 | // Mapping thông tin biến với tên cột trong Database 25 | @Column(name = "hp") 26 | private int hp; 27 | @Column(name = "stamina") 28 | private int stamina; 29 | 30 | // Nếu không đánh dấu @Column thì sẽ mapping tự động theo tên biến 31 | private int atk; 32 | private int def; 33 | private int agi; 34 | } -------------------------------------------------------------------------------- /spring-boot-11-JPA-MySQL/src/main/java/me/loda/spring/jpamysql/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.jpamysql; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | @Repository 9 | public interface UserRepository extends JpaRepository { 10 | List findAllByAtk(int atk); 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-11-JPA-MySQL/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | spring.datasource.url=jdbc:mysql://localhost:3306/micro_db?useSSL=false 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | 7 | ## Hibernate Properties 8 | # The SQL dialect makes Hibernate generate better SQL for the chosen database 9 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect 10 | # Hibernate ddl auto (create, create-drop, validate, update) 11 | spring.jpa.hibernate.ddl-auto = update 12 | 13 | logging.level.org.hibernate = ERROR -------------------------------------------------------------------------------- /spring-boot-12-jpa-method-and-@Query-@Param/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-12-jpa-method-and-@Query-@Param 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | mysql 22 | mysql-connector-java 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-boot-12-jpa-method-and-@Query-@Param/src/main/java/me/loda/spring/jpaquery/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.jpaquery; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | import lombok.Data; 13 | 14 | @Entity 15 | @Table(name = "user") 16 | @Data 17 | public class User implements Serializable { 18 | private static final long serialVersionUID = -297553281792804396L; 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | private Long id; 23 | 24 | // Mapping thông tin biến với tên cột trong Database 25 | @Column(name = "hp") 26 | private int hp; 27 | @Column(name = "stamina") 28 | private int stamina; 29 | 30 | // Nếu không đánh dấu @Column thì sẽ mapping tự động theo tên biến 31 | private int atk; 32 | private int def; 33 | private int agi; 34 | } -------------------------------------------------------------------------------- /spring-boot-12-jpa-method-and-@Query-@Param/src/main/java/me/loda/spring/jpaquery/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.jpaquery; 2 | 3 | import java.util.List; 4 | import java.util.stream.Stream; 5 | 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | import org.springframework.data.jpa.repository.Query; 8 | import org.springframework.data.repository.query.Param; 9 | import org.springframework.stereotype.Repository; 10 | 11 | @Repository 12 | public interface UserRepository extends JpaRepository { 13 | List findAllByAtk(int atk); 14 | 15 | List findAllByAgiBetween(int start, int end); 16 | 17 | @Query("SELECT u FROM User u WHERE u.def = :def") 18 | User findUserByDefQuery(@Param("def") Integer def); 19 | 20 | List findAllByAgiGreaterThan(int agiThreshold); 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-12-jpa-method-and-@Query-@Param/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | spring.datasource.url=jdbc:mysql://localhost:3306/micro_db?useSSL=false 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | 7 | ## Hibernate Properties 8 | # The SQL dialect makes Hibernate generate better SQL for the chosen database 9 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect 10 | # Hibernate ddl auto (create, create-drop, validate, update) 11 | spring.jpa.hibernate.ddl-auto = update 12 | 13 | logging.level.org.hibernate = ERROR -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-13-spring-boot-thymeleaf-mysql-web 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-thymeleaf 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-jpa 23 | 24 | 25 | mysql 26 | mysql-connector-java 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/java/me/loda/spring/thymeleafweb/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleafweb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | public static void main(String[] args) { 9 | SpringApplication.run(App.class, args); 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/java/me/loda/spring/thymeleafweb/config/TodoConfig.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleafweb.config; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | 14 | import me.loda.spring.thymeleafweb.model.TodoValidator; 15 | 16 | /** 17 | * Copyright 2019 {@author Loda} (https://loda.me). 18 | * This project is licensed under the MIT license. 19 | * 20 | * @since 5/25/2019 21 | * Github: https://github.com/loda-kun 22 | */ 23 | @Configuration 24 | public class TodoConfig { 25 | /** 26 | * Tạo ra Bean TodoValidator để sử dụng sau này 27 | * @return 28 | */ 29 | @Bean 30 | public TodoValidator validator() { 31 | return new TodoValidator(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/java/me/loda/spring/thymeleafweb/model/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleafweb.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | import lombok.Data; 9 | 10 | @Entity 11 | @Data 12 | public class Todo { 13 | @Id 14 | @GeneratedValue(strategy = GenerationType.IDENTITY) 15 | private Long id; 16 | 17 | private String title; 18 | private String detail; 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/java/me/loda/spring/thymeleafweb/model/TodoValidator.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleafweb.model; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.Optional; 12 | 13 | import org.thymeleaf.util.StringUtils; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/25/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | 23 | /* 24 | Đối tượng này dùng để kiểm tra xem một Object Todo có hợp lệ không 25 | */ 26 | public class TodoValidator { 27 | 28 | /** 29 | * Kiểm tra một object Todo có hợp lệ không 30 | * @param todo 31 | * @return 32 | */ 33 | public boolean isValid(Todo todo) { 34 | return Optional.ofNullable(todo) 35 | .filter(t -> !StringUtils.isEmpty(t.getTitle())) // Kiểm tra title khác rỗng 36 | .filter(t -> !StringUtils.isEmpty(t.getDetail())) // Kiểm tra detail khác rỗng 37 | .isPresent(); // Trả về true nếu hợp lệ, ngược lại false 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/java/me/loda/spring/thymeleafweb/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleafweb.repository; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.apache.catalina.User; 14 | import org.springframework.data.jpa.repository.JpaRepository; 15 | import org.springframework.stereotype.Repository; 16 | 17 | import me.loda.spring.thymeleafweb.model.Todo; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 5/25/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Repository 27 | public interface TodoRepository extends JpaRepository { 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Chạy ứng dụng trên port 8085 2 | server.port=8085 3 | 4 | # Bỏ tính năng cache của thymeleaf để lập trình cho nhanh 5 | spring.thymeleaf.cache=false 6 | 7 | # Các message tĩnh sẽ được lưu tại thư mục i18n 8 | spring.messages.basename=i18n/messages 9 | 10 | 11 | # Bỏ properties này đi khi deploy 12 | # Nó có tác dụng cố định ngôn ngữ hiện tại chỉ là Tiếng Việt 13 | spring.mvc.locale-resolver=fixed 14 | 15 | # Mặc định ngôn ngữ là tiếng việt 16 | spring.mvc.locale=vi_VN 17 | # Đổi thành tiếng anh bằng cách bỏ comment ở dứoi 18 | #spring.mvc.locale=en_US 19 | 20 | spring.datasource.url=jdbc:mysql://localhost:3306/todo_db?useSSL=false 21 | spring.datasource.username=root 22 | spring.datasource.password=root 23 | 24 | 25 | ## Hibernate Properties 26 | # The SQL dialect makes Hibernate generate better SQL for the chosen database 27 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect 28 | # Hibernate ddl auto (create, create-drop, validate, update) 29 | spring.jpa.hibernate.ddl-auto = update -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/i18n/messages.properties: -------------------------------------------------------------------------------- 1 | loda.message.hello=Welcome to TodoApp 2 | loda.message.success=Thêm công việc thành công! 3 | loda.message.failed=Thêm công việc không thành công! 4 | 5 | loda.value.addTodo=Thêm công việc 6 | loda.value.viewListTodo=Xem danh sách công việc 7 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/i18n/messages_en.properties: -------------------------------------------------------------------------------- 1 | loda.message.hello=Welcome to TodoApp 2 | loda.message.success=Add To-do Successfully! 3 | loda.message.failed=Add To-do Failed! 4 | 5 | loda.value.addTodo=Add To-do 6 | loda.value.viewListTodo=View To-do list 7 | loda.value.listTodo=To-do list -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/i18n/messages_vi.properties: -------------------------------------------------------------------------------- 1 | loda.message.hello=Welcome to TodoApp 2 | loda.message.success=Thêm Todo thành công! 3 | loda.message.failed=Thêm Todo không thành công! 4 | 5 | loda.value.addTodo=Thêm công việc 6 | loda.value.viewListTodo=Xem danh sách công việc 7 | loda.value.listTodo=Danh sách công việc -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/static/css/main.css: -------------------------------------------------------------------------------- 1 | body{ 2 | padding: 1em; 3 | } -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/templates/addTodo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 |

To-do

18 | 19 |
20 |

title:

21 |

detail:

22 |

23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/templates/failed.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

To-do

14 |

15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/templates/listTodo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

17 | 18 | 19 |
    20 | 21 |
  • 22 | 23 | : 24 |
  • 25 |
26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-13-spring-boot-thymeleaf-mysql-web/src/main/resources/templates/success.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda To-do 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

To-do

14 |

15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-14-restful-api-@RestController-@RequestBody-@PathVariable/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-14-restful-api-@RestController 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-14-restful-api-@RestController-@RequestBody-@PathVariable/src/main/java/me/loda/spring/restfulapi/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.restfulapi; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class Todo { 7 | private String title; 8 | private String detail; 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-14-restful-api-@RestController-@RequestBody-@PathVariable/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.root=DEBUG -------------------------------------------------------------------------------- /spring-boot-15-@ControllerAdvice-@RestControllerAdvice-@ExceptionHandler-@ResponseStatus/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-15-Exception-handling-@ControllerAdvice-@ExceptionHandler 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-15-@ControllerAdvice-@RestControllerAdvice-@ExceptionHandler-@ResponseStatus/src/main/java/me/loda/spring/exceptionhandling/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.exceptionhandling; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/25/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @SpringBootApplication 22 | public class App { 23 | public static void main(String[] args) { 24 | SpringApplication.run(App.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-15-@ControllerAdvice-@RestControllerAdvice-@ExceptionHandler-@ResponseStatus/src/main/java/me/loda/spring/exceptionhandling/ErrorMessage.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.exceptionhandling; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import lombok.AllArgsConstructor; 12 | import lombok.Data; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/26/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Data 22 | @AllArgsConstructor 23 | public class ErrorMessage { 24 | private int statusCode; 25 | private String message; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /spring-boot-15-@ControllerAdvice-@RestControllerAdvice-@ExceptionHandler-@ResponseStatus/src/main/java/me/loda/spring/exceptionhandling/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.exceptionhandling; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class Todo { 9 | private String title; 10 | private String detail; 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-16-@ConfigurationProperties/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-16-@ConfigurationProperties 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-16-@ConfigurationProperties/src/main/java/me/loda/spring/configurationpropertiesanno/LodaAppProperties.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.configurationpropertiesanno; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | import org.springframework.boot.context.properties.ConfigurationProperties; 15 | import org.springframework.stereotype.Component; 16 | 17 | import lombok.Data; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 4/28/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Data // Lombok, xem chi tiết tại bài viết 27 | @Component // Là 1 spring bean 28 | //@PropertySource("classpath:loda.yml") // Đánh dấu để lấy config từ trong file loda.yml 29 | @ConfigurationProperties(prefix = "loda") // Chỉ lấy các config có tiền tố là "loda" 30 | public class LodaAppProperties { 31 | private String email; 32 | private String googleAnalyticsId; 33 | 34 | private List authors; 35 | 36 | private Map exampleMap; 37 | 38 | 39 | // standard getters and setters 40 | } 41 | -------------------------------------------------------------------------------- /spring-boot-16-@ConfigurationProperties/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | loda: 2 | email: loda.namnh@gmail.com 3 | googleAnalyticsId: U-xxxxx 4 | authors: 5 | - loda 6 | - atom 7 | exampleMap: 8 | key1: hello 9 | key2: world -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-17-spring-profiles 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/java/me/loda/spring/springprofiles/ApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springprofiles; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.servlet.ServletContext; 12 | import javax.servlet.ServletException; 13 | 14 | import org.springframework.context.annotation.Configuration; 15 | import org.springframework.web.WebApplicationInitializer; 16 | 17 | /** 18 | * Copyright 2019 {@author Loda} (https://loda.me). 19 | * This project is licensed under the MIT license. 20 | * 21 | * @since 5/26/2019 22 | * Github: https://github.com/loda-kun 23 | */ 24 | /** 25 | * Cách 1 26 | * @return 27 | */ 28 | @Configuration 29 | public class ApplicationInitializer implements WebApplicationInitializer { 30 | 31 | @Override 32 | public void onStartup(ServletContext servletContext) throws ServletException { 33 | servletContext.setInitParameter("spring.profiles.active", "aws"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/java/me/loda/spring/springprofiles/LocalDatasource.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springprofiles; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import lombok.AllArgsConstructor; 12 | import lombok.Data; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/26/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Data 22 | @AllArgsConstructor 23 | public class LocalDatasource { 24 | private String url; 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/java/me/loda/spring/springprofiles/LocalDatasourceConfig.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springprofiles; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Profile; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/26/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | 23 | /** 24 | * Chỉ khi profiles là "local" 25 | * Thì Configuration dưới đây mới được khởi tạo 26 | */ 27 | @Configuration 28 | @Profile("local") 29 | public class LocalDatasourceConfig { 30 | 31 | @Bean 32 | public LocalDatasource localDatasource() { 33 | return new LocalDatasource("Local object, Chỉ khởi tạo khi 'local' profile active"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/resources/application-aws.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | username: xxx 4 | password: xxx 5 | url: jdbc:mysql://10.127.24.12:2030/news?useSSL=false&characterEncoding=UTF-8 -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/resources/application-common.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | jpa: 3 | properties: 4 | hibernate: 5 | jdbc: 6 | batch_size: 50 7 | batch_versioned_data: true 8 | hibernate: 9 | ddl-auto: none -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/resources/application-local.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | username: root 4 | password: 5 | url: jdbc:mysql://localhost:3306/news?useSSL=false&characterEncoding=UTF-8 6 | 7 | logging: 8 | level: 9 | org: 10 | hibernate: 11 | SQL: debug -------------------------------------------------------------------------------- /spring-boot-17-spring-profiles/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #application.yml 2 | --- 3 | spring.profiles: local 4 | spring.profiles.include: common, local 5 | --- 6 | spring.profiles: aws 7 | spring.profiles.include: common, aws 8 | --- -------------------------------------------------------------------------------- /spring-boot-18-testing-in-spring-boot/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-18-testing-in-spring-boot 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-boot-18-testing-in-spring-boot/src/main/java/me/loda/spring/testinginspringboot/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/26/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @SpringBootApplication 22 | public class App { 23 | public static void main(String[] args) { 24 | SpringApplication.run(App.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-18-testing-in-spring-boot/src/main/java/me/loda/spring/testinginspringboot/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class Todo { 9 | private int id; 10 | private String title; 11 | private String detail; 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-18-testing-in-spring-boot/src/main/java/me/loda/spring/testinginspringboot/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 5/26/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | public interface TodoRepository { 21 | List findAll(); 22 | 23 | Todo findById(int id); 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-18-testing-in-spring-boot/src/main/java/me/loda/spring/testinginspringboot/TodoRestController.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.web.bind.annotation.GetMapping; 15 | import org.springframework.web.bind.annotation.RequestMapping; 16 | import org.springframework.web.bind.annotation.RestController; 17 | 18 | /** 19 | * Copyright 2019 {@author Loda} (https://loda.me). 20 | * This project is licensed under the MIT license. 21 | * 22 | * @since 5/26/2019 23 | * Github: https://github.com/loda-kun 24 | */ 25 | @RestController 26 | @RequestMapping("/api/v1") 27 | public class TodoRestController { 28 | @Autowired 29 | TodoService todoService; 30 | 31 | @GetMapping("/todo") 32 | public List findAll(){ 33 | return todoService.getAll(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-18-testing-in-spring-boot/src/main/java/me/loda/spring/testinginspringboot/TodoService.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | import lombok.RequiredArgsConstructor; 17 | 18 | /** 19 | * Copyright 2019 {@author Loda} (https://loda.me). 20 | * This project is licensed under the MIT license. 21 | * 22 | * @since 5/26/2019 23 | * Github: https://github.com/loda-kun 24 | */ 25 | @Service 26 | public class TodoService { 27 | @Autowired 28 | private TodoRepository todoRepository; 29 | 30 | public int countTodo(){ 31 | return todoRepository.findAll().size(); 32 | } 33 | 34 | public Todo getTodo(int id){ 35 | return todoRepository.findById(id); 36 | } 37 | 38 | public List getAll(){ 39 | return todoRepository.findAll(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-19-testing-in-spring-boot-2 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | 22 | com.h2database 23 | h2 24 | runtime 25 | 26 | 27 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/main/java/me/loda/spring/testinginspringboot2/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot2; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/26/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @SpringBootApplication 22 | public class App { 23 | public static void main(String[] args) { 24 | SpringApplication.run(App.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/main/java/me/loda/spring/testinginspringboot2/Todo.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot2; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | 8 | import lombok.AllArgsConstructor; 9 | import lombok.Data; 10 | import lombok.NoArgsConstructor; 11 | 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Entity 16 | public class Todo { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private int id; 20 | private String title; 21 | private String detail; 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/main/java/me/loda/spring/testinginspringboot2/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot2; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.springframework.data.jpa.repository.JpaRepository; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/26/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | public interface TodoRepository extends JpaRepository { 23 | List findAll(); 24 | 25 | Todo findById(int id); 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/main/java/me/loda/spring/testinginspringboot2/TodoRestController.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot2; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.springframework.web.bind.annotation.GetMapping; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | import lombok.RequiredArgsConstructor; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 5/26/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @RestController 27 | @RequestMapping("/api/v1") 28 | @RequiredArgsConstructor 29 | public class TodoRestController { 30 | private final TodoService todoService; 31 | 32 | @GetMapping("/todo") 33 | public List findAll() { 34 | return todoService.getAll(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/main/java/me/loda/spring/testinginspringboot2/TodoService.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot2; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import java.util.List; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.stereotype.Service; 15 | 16 | import lombok.RequiredArgsConstructor; 17 | 18 | /** 19 | * Copyright 2019 {@author Loda} (https://loda.me). 20 | * This project is licensed under the MIT license. 21 | * 22 | * @since 5/26/2019 23 | * Github: https://github.com/loda-kun 24 | */ 25 | @Service 26 | @RequiredArgsConstructor 27 | public class TodoService { 28 | private final TodoRepository todoRepository; 29 | 30 | public int countTodo(){ 31 | return todoRepository.findAll().size(); 32 | } 33 | 34 | public Todo getTodo(int id){ 35 | return todoRepository.findById(id); 36 | } 37 | 38 | public List getAll(){ 39 | return todoRepository.findAll(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/test/java/me/loda/spring/testinginspringboot2/SqlAnnotationTest.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.testinginspringboot2; 2 | 3 | import org.assertj.core.api.Assertions; 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.autoconfigure.orm.jpa.DataJpaTest; 8 | import org.springframework.test.context.jdbc.Sql; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | @RunWith(SpringRunner.class) 12 | @DataJpaTest 13 | public class SqlAnnotationTest { 14 | @Autowired 15 | private TodoRepository todoRepository; 16 | 17 | @Test 18 | @Sql("/createTodo.sql") 19 | public void testTodoRepositoryBySqlSchema() { 20 | Assertions.assertThat(todoRepository.findAll()).hasSize(2); 21 | Assertions.assertThat(todoRepository.findById(1).getTitle()).isEqualTo("Todo-1"); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-19-testing-in-spring-boot-2/src/test/resources/createTodo.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO todo (title, detail) VALUES ('Todo-1', 'Do homework'); 2 | INSERT INTO todo (title, detail) VALUES ('Todo-2', 'Walking'); 3 | -------------------------------------------------------------------------------- /spring-boot-2-helloworld-@Primary - @Qualifier/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-helloworld-@Primary - @Qualifier 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-2-helloworld-@Primary - @Qualifier/src/main/java/me.loda.spring.helloprimaryqualifier/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloprimaryqualifier; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.context.ApplicationContext; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/9/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | @SpringBootApplication 23 | public class App { 24 | public static void main(String[] args) { 25 | // ApplicationContext chính là container, chứa toàn bộ các Bean 26 | ApplicationContext context = SpringApplication.run(App.class, args); 27 | 28 | // Khi chạy xong, lúc này context sẽ chứa các Bean có đánh 29 | // dấu @Component. 30 | 31 | Girl girl = context.getBean(Girl.class); 32 | 33 | System.out.println("Girl Instance: " + girl); 34 | 35 | System.out.println("Girl Outfit: " + girl.outfit); 36 | 37 | girl.outfit.wear(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /spring-boot-2-helloworld-@Primary - @Qualifier/src/main/java/me.loda.spring.helloprimaryqualifier/Bikini.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloprimaryqualifier; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.beans.factory.annotation.Qualifier; 12 | import org.springframework.stereotype.Component; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/11/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | /* 22 | Đánh dấu class bằng @Component 23 | Class này sẽ được Spring Boot hiểu là một Bean (hoặc dependency) 24 | Và sẽ được Spring Boot quản lý 25 | */ 26 | @Component("bikini") 27 | public class Bikini implements Outfit { 28 | @Override 29 | public void wear() { 30 | System.out.println("Mặc bikini"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot-2-helloworld-@Primary - @Qualifier/src/main/java/me.loda.spring.helloprimaryqualifier/Naked.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloprimaryqualifier; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.context.annotation.Primary; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component("naked") 8 | public class Naked implements Outfit { 9 | @Override 10 | public void wear() { 11 | System.out.println("Đang không mặc gì"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-2-helloworld-@Primary - @Qualifier/src/main/java/me.loda.spring.helloprimaryqualifier/Outfit.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.helloprimaryqualifier; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/11/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public interface Outfit { 19 | public void wear(); 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-3-bean-life-cycle-@PostConstruct-@PreDestroy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-3-bean-life-cycle-@PostConstruct-@PreDestroy 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-3-bean-life-cycle-@PostConstruct-@PreDestroy/src/main/java/me/loda/spring/beanlifecycle/Girl.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.beanlifecycle; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.annotation.PostConstruct; 12 | import javax.annotation.PreDestroy; 13 | 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.beans.factory.annotation.Qualifier; 16 | import org.springframework.stereotype.Component; 17 | 18 | /** 19 | * Copyright 2019 {@author Loda} (https://loda.me). 20 | * This project is licensed under the MIT license. 21 | * 22 | * @since 5/11/2019 23 | * Github: https://github.com/loda-kun 24 | */ 25 | @Component 26 | public class Girl { 27 | 28 | @PostConstruct 29 | public void postConstruct(){ 30 | System.out.println("\t>> Đối tượng Girl sau khi khởi tạo xong sẽ chạy hàm này"); 31 | } 32 | 33 | @PreDestroy 34 | public void preDestroy(){ 35 | System.out.println("\t>> Đối tượng Girl trước khi bị destroy thì chạy hàm này"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-4-@Component-@Service-@Repository 13 | 14 | 15 | 16 | 17 | org.apache.commons 18 | commons-lang3 19 | 3.9 20 | 21 | 22 | org.apache.commons 23 | commons-lang3 24 | 3.9 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/src/main/java/me/loda/spring/componentservicerepository/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentservicerepository; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ApplicationContext; 6 | 7 | @SpringBootApplication 8 | public class App { 9 | 10 | public static void main(String[] args) { 11 | ApplicationContext context = SpringApplication.run(App.class, args); 12 | 13 | // Lấy ra bean GirlService 14 | GirlService girlService = context.getBean(GirlService.class); 15 | // Lấu ra random một cô gái từ tầng service 16 | Girl girl = girlService.getRandomGirl(); 17 | // In ra màn hình 18 | System.out.println(girl); 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/src/main/java/me/loda/spring/componentservicerepository/Girl.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentservicerepository; 2 | 3 | public class Girl { 4 | private String name; 5 | 6 | public Girl(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Girl(" + this.name + ")"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/src/main/java/me/loda/spring/componentservicerepository/GirlRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentservicerepository; 2 | 3 | public interface GirlRepository { 4 | /** 5 | * Tìm kiếm một cô gái trong database theo tên 6 | * @param name 7 | * @return 8 | */ 9 | Girl getGirlByName(String name); 10 | } 11 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/src/main/java/me/loda/spring/componentservicerepository/GirlRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentservicerepository; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | @Repository 6 | public class GirlRepositoryImpl implements GirlRepository { 7 | 8 | @Override 9 | public Girl getGirlByName(String name) { 10 | // Ở đây tôi ví dụ là database đã trả về 11 | // một cô gái với tên đúng như tham số 12 | // Để làm ví dụ. 13 | // Còn thực tế phải query trong csđl nhé. 14 | return new Girl(name); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/src/main/java/me/loda/spring/componentservicerepository/GirlService.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentservicerepository; 2 | 3 | import java.nio.charset.Charset; 4 | import java.util.Random; 5 | 6 | import org.apache.commons.lang3.RandomStringUtils; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | @Service 11 | public class GirlService { 12 | @Autowired 13 | private GirlRepository girlRepository; 14 | 15 | public Girl getRandomGirl(){ 16 | // Random 1 cái tên độ dài 10 17 | String name = randomGirlName(10); 18 | 19 | // Gọi xuông tầng repository để query lấy một cô gái tên là "name" trong database 20 | return girlRepository.getGirlByName(name); 21 | } 22 | 23 | public String randomGirlName(int length) { 24 | // Random một string có độ dài quy định 25 | // Sử dụng thư viện Apache Common Lang 26 | return RandomStringUtils.randomAlphanumeric(length).toLowerCase(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-4-@Component-@Service-@Repository/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework.web: DEBUG -------------------------------------------------------------------------------- /spring-boot-5-Component-Scan/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-4-Component-Scan-@Controller 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-5-Component-Scan/src/main/java/me/loda/spring/componentscan/Girl.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentscan; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 5/12/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | @Component 21 | public class Girl { 22 | 23 | @Override 24 | public String toString() { 25 | return "Girl.java"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-5-Component-Scan/src/main/java/me/loda/spring/componentscan/others/OtherGirl.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentscan.others; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.stereotype.Component; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 5/12/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | @Component 21 | public class OtherGirl { 22 | @Override 23 | public String toString() { 24 | return "OtherGirl.java"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-5-Component-Scan/src/main/java/me/loda/spring/componentscan/others2/OtherGirl2.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.componentscan.others2; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class OtherGirl2 { 7 | @Override 8 | public String toString() { 9 | return "OtherGirl2.java"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-6-@configuration-@Bean/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-6-@configuration-@Bean 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-6-@configuration-@Bean/src/main/java/me/loda/spring/configurationbean/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.configurationbean; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ApplicationContext; 6 | 7 | @SpringBootApplication 8 | public class App { 9 | public static void main(String[] args) { 10 | 11 | ApplicationContext context = SpringApplication.run(App.class, args); 12 | SimpleBean simpleBean = context.getBean(SimpleBean.class); 13 | 14 | System.out.println("Simple Bean Example: "+simpleBean); 15 | 16 | DatabaseConnector mysql = (DatabaseConnector) context.getBean("mysqlConnector"); 17 | mysql.connect(); 18 | 19 | DatabaseConnector mongodb = (DatabaseConnector) context.getBean("mongodbConnector"); 20 | mongodb.connect(); 21 | 22 | DatabaseConnector postgresql = (DatabaseConnector) context.getBean("postgresqlConnector"); 23 | postgresql.connect(); 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-6-@configuration-@Bean/src/main/java/me/loda/spring/configurationbean/DatabaseConnector.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.configurationbean; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/16/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public abstract class DatabaseConnector { 19 | private String url; 20 | /** 21 | * Hàm này có nhiệm vụ Connect tới một Database bất kỳ 22 | */ 23 | public abstract void connect(); 24 | 25 | public String getUrl() { 26 | return url; 27 | } 28 | 29 | public void setUrl(String url) { 30 | this.url = url; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot-6-@configuration-@Bean/src/main/java/me/loda/spring/configurationbean/MongoDbConnector.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.configurationbean; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/16/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class MongoDbConnector extends DatabaseConnector { 19 | @Override 20 | public void connect() { 21 | System.out.println("Đã kết nối tới Mongodb: " + getUrl()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-6-@configuration-@Bean/src/main/java/me/loda/spring/configurationbean/MySqlConnector.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.configurationbean; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/16/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class MySqlConnector extends DatabaseConnector { 19 | @Override 20 | public void connect() { 21 | System.out.println("Đã kết nối tới Mysql: " + getUrl()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-6-@configuration-@Bean/src/main/java/me/loda/spring/configurationbean/PostgreSqlConnector.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.configurationbean; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/16/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class PostgreSqlConnector extends DatabaseConnector{ 19 | @Override 20 | public void connect() { 21 | System.out.println("Đã kết nối tới Postgresql: " + getUrl()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-7-spring-application-properties-@Value/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-7-spring-application-properties-@Value 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-7-spring-application-properties-@Value/src/main/java/me/loda/spring/applicationproperties/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.applicationproperties; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.context.ApplicationContext; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/18/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | @SpringBootApplication 23 | public class App { 24 | public static void main(String[] args) { 25 | ApplicationContext context = SpringApplication.run(App.class, args); 26 | 27 | DatabaseConnector databaseConnector = context.getBean(DatabaseConnector.class); 28 | databaseConnector.connect(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-7-spring-application-properties-@Value/src/main/java/me/loda/spring/applicationproperties/AppConfig.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.applicationproperties; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | @Configuration 8 | public class AppConfig { 9 | 10 | // Lấy giá trị config từ file application.properties 11 | @Value("${loda.mysql.url}") 12 | String mysqlUrl; 13 | 14 | @Bean 15 | DatabaseConnector mysqlConfigure() { 16 | DatabaseConnector mySqlConnector = new MySqlConnector(); 17 | // Set Url 18 | System.out.println("Config Mysql Url: " + mysqlUrl); 19 | mySqlConnector.setUrl(mysqlUrl); 20 | // Set username, password, format, v.v... 21 | return mySqlConnector; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-7-spring-application-properties-@Value/src/main/java/me/loda/spring/applicationproperties/DatabaseConnector.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.applicationproperties; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/16/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public abstract class DatabaseConnector { 19 | private String url; 20 | /** 21 | * Hàm này có nhiệm vụ Connect tới một Database bất kỳ 22 | */ 23 | public abstract void connect(); 24 | 25 | public String getUrl() { 26 | return url; 27 | } 28 | 29 | public void setUrl(String url) { 30 | this.url = url; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot-7-spring-application-properties-@Value/src/main/java/me/loda/spring/applicationproperties/MySqlConnector.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.applicationproperties; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/16/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class MySqlConnector extends DatabaseConnector { 19 | @Override 20 | public void connect() { 21 | System.out.println("Đã kết nối tới Mysql: " + getUrl()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-boot-7-spring-application-properties-@Value/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 8081 2 | logging.level.root=INFO 3 | 4 | loda.mysql.url=jdbc:mysql://host1:33060/loda 5 | -------------------------------------------------------------------------------- /spring-boot-8-@Controller-web-helloworld/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-8-@Controller-web-helloworld 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-thymeleaf 19 | 20 | 21 | -------------------------------------------------------------------------------- /spring-boot-8-@Controller-web-helloworld/src/main/java/me/loda/spring/controllerweb/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.controllerweb; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/18/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @SpringBootApplication 22 | public class App { 23 | public static void main(String[] args) { 24 | SpringApplication.run(App.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-8-@Controller-web-helloworld/src/main/resources/templates/about.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello World 5 | 6 | 7 | 8 |

Loda

9 | 10 | Website 11 | Github 12 | 13 | 14 | -------------------------------------------------------------------------------- /spring-boot-8-@Controller-web-helloworld/src/main/resources/templates/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello World 5 | 6 | 7 | 8 |

9 | 10 | Trang chủ 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-boot-8-@Controller-web-helloworld/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello World 5 | 6 | 7 | 8 |

Đây là một trang web

9 | 10 | About 11 | 12 |
13 | 14 | 15 |
16 | 17 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-9-thymleaf 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-thymeleaf 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-devtools 27 | runtime 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/java/me/loda/spring/thymeleaf/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleaf; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | public static void main(String[] args) { 9 | SpringApplication.run(App.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/java/me/loda/spring/thymeleaf/Info.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleaf; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | /** 7 | * TẠo ra class này chỉ để lưu giữ thông tin 8 | */ 9 | @Data 10 | @AllArgsConstructor 11 | public class Info { 12 | String key; 13 | String value; 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/java/me/loda/spring/thymeleaf/WebController.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.thymeleaf; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.ui.Model; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | 10 | @Controller 11 | public class WebController { 12 | 13 | @GetMapping("/profile") 14 | public String profile(Model model){ 15 | // Tạo ra thông tin 16 | List profile = new ArrayList<>(); 17 | profile.add(new Info("fullname", "Nguyễn Hoàng Nam")); 18 | profile.add(new Info("nickname", "lốddaf")); 19 | profile.add(new Info("gmail", "loda.namnh@gmail.com")); 20 | profile.add(new Info("facebook", "https://www.facebook.com/nam.tehee")); 21 | profile.add(new Info("website", "https://loda.me")); 22 | 23 | // Đưa thông tin vào Model 24 | model.addAttribute("lodaProfile", profile); 25 | 26 | // TRả về template profile.html 27 | return "profile"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Chạy ứng dụng trên port 8085 2 | server.port=8085 3 | 4 | # Bỏ tính năng cache của thymeleaf để lập trình cho nhanh 5 | spring.thymeleaf.cache=false 6 | 7 | # Các message tĩnh sẽ được lưu tại thư mục i18n 8 | spring.messages.basename=i18n/messages 9 | 10 | 11 | # Bỏ properties này đi khi deploy 12 | # Nó có tác dụng cố định ngôn ngữ hiện tại chỉ là Tiếng Việt 13 | spring.mvc.locale-resolver=fixed 14 | 15 | # Mặc định ngôn ngữ là tiếng việt 16 | spring.mvc.locale=vi_VN 17 | # Đổi thành tiếng anh bằng cách bỏ comment ở dứoi 18 | #spring.mvc.locale=en_US 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/resources/i18n/messages.properties: -------------------------------------------------------------------------------- 1 | loda.hello=Xin chào tất cả các bạn tới với Loda Website -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/resources/i18n/messages_en.properties: -------------------------------------------------------------------------------- 1 | loda.hello=Welcome to Loda Website -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/resources/i18n/messages_vi.properties: -------------------------------------------------------------------------------- 1 | loda.hello=Xin chào tất cả các bạn tới với Loda Website -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

17 | 18 | Loda Profile 19 | 20 | 21 | -------------------------------------------------------------------------------- /spring-boot-9-thymeleaf/src/main/resources/templates/profile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Loda 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

17 |

Loda Profile

18 | 19 |
    20 | 21 |
  • 22 | 23 | : 24 |
  • 25 |
26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-@Conditional-2-custom-conditional 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/src/main/java/me/loda/spring/customconditional/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.customconditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.context.ApplicationContext; 14 | 15 | import me.loda.spring.customconditional.AppConfiguration.SomeBean; 16 | 17 | /** 18 | * Copyright 2019 {@author Loda} (https://loda.me). 19 | * This project is licensed under the MIT license. 20 | * 21 | * @since 6/3/2019 22 | * Github: https://github.com/loda-kun 23 | */ 24 | @SpringBootApplication 25 | public class App { 26 | public static void main(String[] args) { 27 | ApplicationContext context = SpringApplication.run(App.class, args); 28 | try { 29 | SomeBean someBean = context.getBean(SomeBean.class); 30 | System.out.println("SomeBean tồn tại!"); 31 | }catch (Exception e){ 32 | System.out.println("SomeBean chỉ được tạo nếu chạy trên Window"); 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/src/main/java/me/loda/spring/customconditional/AppConfiguration.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.customconditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Conditional; 13 | import org.springframework.context.annotation.Configuration; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 6/3/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | @Configuration 23 | public class AppConfiguration { 24 | public static class SomeBean{ 25 | } 26 | 27 | /* 28 | SomeBean chỉ được tạo ra khi 29 | thỏa mãn điều kiện 30 | */ 31 | // @Conditional(MacRequired.class) 32 | // @Conditional(WindowRequired.class) 33 | // @Conditional(WindowOrMacRequired.class) 34 | @ConditionalOnWindow 35 | @Bean 36 | SomeBean someBean(){ 37 | return new SomeBean(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/src/main/java/me/loda/spring/customconditional/ConditionalOnWindow.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.customconditional; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | import org.springframework.context.annotation.Conditional; 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 2019-06-04 16 | * Github: https://github.com/loda-kun 17 | */ 18 | @Target({ ElementType.TYPE, ElementType.METHOD }) 19 | @Retention(RetentionPolicy.RUNTIME) 20 | @Documented 21 | // Đánh dấu annotation này bởi @Conditional(WindowRequired.class) 22 | @Conditional(WindowRequired.class) 23 | public @interface ConditionalOnWindow { 24 | /* 25 | Trong trường hợp bạn muốn viết ngắn gọn, 26 | hay tạo ra 1 Annotation mới và gắn @Conditional(WindowRequired.class) 27 | trên nó 28 | 29 | Như vậy khi cần sử dụng chỉ cần gọi @ConditionalOnWindow là được 30 | */ 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/src/main/java/me/loda/spring/customconditional/MacRequired.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.customconditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.context.annotation.Condition; 12 | import org.springframework.context.annotation.ConditionContext; 13 | import org.springframework.core.type.AnnotatedTypeMetadata; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 6/3/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | public class MacRequired implements Condition { 23 | 24 | @Override 25 | public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { 26 | return System.getProperty("os.name").toLowerCase().contains("mac"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/src/main/java/me/loda/spring/customconditional/WindowOrMacRequired.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.customconditional; 2 | 3 | import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; 4 | import org.springframework.context.annotation.Conditional; 5 | 6 | /** 7 | * Copyright 2019 {@author Loda} (https://loda.me). 8 | * This project is licensed under the MIT license. 9 | * 10 | * @since 2019-06-04 11 | * Github: https://github.com/loda-kun 12 | */ 13 | 14 | /** 15 | * Class kế thừa AnyNestedCondition sẽ chấp nhận mọi 16 | * điều kiện @Conditional bên trong nó 17 | */ 18 | public class WindowOrMacRequired extends AnyNestedCondition{ 19 | 20 | public WindowOrMacRequired(){ 21 | super(ConfigurationPhase.REGISTER_BEAN); 22 | } 23 | 24 | /* 25 | Bạn phải định nghĩa các Điều kiện bên trong class 26 | kế thừa AnyNestedCondition 27 | */ 28 | @Conditional(WindowRequired.class) 29 | public class RunOnWindow{} 30 | 31 | /* 32 | Lúc này, cả 2 điều kiện Window và Mac sẽ được kết hợp vs 33 | nhau khi kiểm tra, nếu thoả mãn 1 trong 2 là đc 34 | */ 35 | @Conditional(MacRequired.class) 36 | public class RunOnMac{} 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-@Conditional-2-custom-conditional/src/main/java/me/loda/spring/customconditional/WindowRequired.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.customconditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.context.annotation.Condition; 12 | import org.springframework.context.annotation.ConditionContext; 13 | import org.springframework.core.type.AnnotatedTypeMetadata; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 6/3/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | /* 23 | Một điều kiện, phải kế thừa lớp Condition của Spring Boot cung cấp 24 | */ 25 | public class WindowRequired implements Condition{ 26 | 27 | @Override 28 | public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { 29 | // Nếu OS ra window trả ra true. 30 | return System.getProperty("os.name").toLowerCase().contains("win"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-@Conditional 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/ABeanWithCondition.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/29/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | 19 | public class ABeanWithCondition { 20 | } 21 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/ABeanWithCondition2.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/29/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class ABeanWithCondition2 { 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/ConditionalOnBeanExample.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/29/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | @Configuration 23 | public class ConditionalOnBeanExample { 24 | /* 25 | Đây là một Bean, bạn hãy chạy ứng dụng 26 | khi comment và chạy lại lần nữa nhưng bỏ dấu comment phía 27 | dưới để xem kết quả. 28 | */ 29 | // @Bean 30 | RandomBean randomBean() { 31 | return new RandomBean(); 32 | } 33 | 34 | /* 35 | ABeanWithCondition chỉ được tạo ra, khi RandomBean tồn tại trong Context. 36 | */ 37 | @Bean 38 | @ConditionalOnBean(RandomBean.class) 39 | ABeanWithCondition aBeanWithACondition() { 40 | return new ABeanWithCondition(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/ConditionalOnExpressionExample.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; 12 | import org.springframework.context.annotation.Configuration; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/29/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | /* 22 | Configuration ConditionalOnExpressionExample chỉ được tạo 23 | khi cả 2 điều kiện thỏa mãn 24 | */ 25 | @Configuration 26 | @ConditionalOnExpression( 27 | "${loda.expression1.enabled:true} and ${loda.expression2.enabled:true}" 28 | ) 29 | public class ConditionalOnExpressionExample { 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/ConditionalOnMissingBeanExample.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 12 | import org.springframework.context.annotation.Configuration; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/29/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Configuration 22 | public class ConditionalOnMissingBeanExample { 23 | 24 | public static class SomeMissingBean{ 25 | 26 | } 27 | 28 | /** 29 | * Nếu trong Context chưa tồn tại một SomeMissingBean nào 30 | * Thì Bean ở dưới đây mới được tạo 31 | * @return 32 | */ 33 | @ConditionalOnMissingBean 34 | SomeMissingBean someMissingBean(){ 35 | return new SomeMissingBean(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/ConditionalOnResourceExample.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnResource; 12 | import org.springframework.context.annotation.Configuration; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/29/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Configuration 22 | @ConditionalOnResource(resources = "/application.properties") 23 | public class ConditionalOnResourceExample { 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/java/me/loda/spring/conditional/RandomBean.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.conditional; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | /** 12 | * Copyright 2019 {@author Loda} (https://loda.me). 13 | * This project is licensed under the MIT license. 14 | * 15 | * @since 5/29/2019 16 | * Github: https://github.com/loda-kun 17 | */ 18 | public class RandomBean { 19 | } 20 | -------------------------------------------------------------------------------- /spring-boot-@Conditional/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TranDatk/spring-boot-learning/c8735c57074d7ab72614b5574d38eac77f72295c/spring-boot-@Conditional/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-boot-@EventListener-@Async/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-@EventListener-@Async 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-@EventListener-@Async/src/main/java/me/loda/springeventlistener/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.springeventlistener; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.context.annotation.Bean; 8 | 9 | /** 10 | * Copyright 2019 {@author Loda} (https://loda.me). 11 | * This project is licensed under the MIT license. 12 | * 13 | * @since 2019-05-31 14 | * Github: https://github.com/loda-kun 15 | */ 16 | @SpringBootApplication 17 | public class App { 18 | @Autowired 19 | MyHouse myHouse; 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(App.class, args); 23 | } 24 | 25 | @Bean 26 | CommandLineRunner run() { 27 | return args -> { 28 | System.out.println(Thread.currentThread().getName() + ": Loda đi tới cửa nhà !!!"); 29 | System.out.println(Thread.currentThread().getName() + ": => Loda bấm chuông và khai báo họ tên!"); 30 | // gõ cửa 31 | myHouse.rangDoorbellBy("Loda"); 32 | System.out.println(Thread.currentThread().getName() +": Loda quay lưng bỏ đi"); 33 | }; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-@EventListener-@Async/src/main/java/me/loda/springeventlistener/DoorBellEvent.java: -------------------------------------------------------------------------------- 1 | package me.loda.springeventlistener; 2 | 3 | import org.springframework.context.ApplicationEvent; 4 | 5 | /** 6 | * Copyright 2019 {@author Loda} (https://loda.me). 7 | * This project is licensed under the MIT license. 8 | * 9 | * @since 2019-05-31 10 | * Github: https://github.com/loda-kun 11 | */ 12 | /* 13 | DoorBellEvent phải kế thừa lớp ApplicationEvent của Spring 14 | Như vậy nó mới được coi là một sự kiện hợp lệ. 15 | */ 16 | public class DoorBellEvent extends ApplicationEvent { 17 | // Tên của ngừoi bấm chuông cửa. 18 | public String guestName; 19 | 20 | /* 21 | Mọi Class kế thừa ApplicationEvent sẽ 22 | phải gọi Constructor tới lớp cha. 23 | */ 24 | public DoorBellEvent(Object source, String guestName) { 25 | // Object source là object tham chiếu tới 26 | // nơi đã phát ra event này! 27 | super(source); 28 | this.guestName = guestName; 29 | } 30 | 31 | public String getGuestName() { 32 | return guestName; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /spring-boot-@EventListener-@Async/src/main/java/me/loda/springeventlistener/ListenerConfiguration.java: -------------------------------------------------------------------------------- 1 | package me.loda.springeventlistener; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.core.task.SimpleAsyncTaskExecutor; 6 | import org.springframework.core.task.TaskExecutor; 7 | import org.springframework.scheduling.annotation.EnableAsync; 8 | 9 | /** 10 | * Copyright 2019 {@author Loda} (https://loda.me). 11 | * This project is licensed under the MIT license. 12 | * 13 | * @since 2019-05-31 14 | * Github: https://github.com/loda-kun 15 | */ 16 | @Configuration 17 | @EnableAsync 18 | public class ListenerConfiguration { 19 | /** 20 | * Tạo ra Executor cho Async 21 | * @return 22 | */ 23 | @Bean 24 | TaskExecutor taskExecutor() { 25 | return new SimpleAsyncTaskExecutor(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-@EventListener-@Async/src/main/java/me/loda/springeventlistener/MyDog.java: -------------------------------------------------------------------------------- 1 | package me.loda.springeventlistener; 2 | 3 | import org.springframework.context.event.EventListener; 4 | import org.springframework.scheduling.annotation.Async; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * Copyright 2019 {@author Loda} (https://loda.me). 9 | * This project is licensed under the MIT license. 10 | * 11 | * @since 2019-05-31 12 | * Github: https://github.com/loda-kun 13 | */ 14 | @Component 15 | public class MyDog { 16 | 17 | /* 18 | @EventListener sẽ lắng nghe mọi sự kiện xảy ra 19 | Nếu có một sự kiện DoorBellEvent được bắn ra, nó sẽ đón lấy 20 | và đưa vào hàm để xử lý 21 | */ 22 | /* 23 | @Async là cách lắng nghe sự kiện ở một Thread khác, không ảnh hưởng tới 24 | luồng chính 25 | */ 26 | @Async 27 | @EventListener 28 | public void doorBellEventListener(DoorBellEvent doorBellEvent) throws InterruptedException { 29 | // Giả sử con chó đang ngủ, 1 giây sau mới dậy 30 | Thread.sleep(1000); 31 | // Sự kiện DoorBellEvent được lắng nghe và xử lý tại đây 32 | System.out.println(Thread.currentThread().getName() + ": Chó ngủ dậy!!!"); 33 | System.out.println(String.format("%s: Go go!! Có người tên là %s gõ cửa!!!", Thread.currentThread().getName(), doorBellEvent.getGuestName())); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-@EventListener-@Async/src/main/java/me/loda/springeventlistener/MyHouse.java: -------------------------------------------------------------------------------- 1 | package me.loda.springeventlistener; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.ApplicationEventPublisher; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * Copyright 2019 {@author Loda} (https://loda.me). 9 | * This project is licensed under the MIT license. 10 | * 11 | * @since 2019-05-31 12 | * Github: https://github.com/loda-kun 13 | */ 14 | @Component 15 | public class MyHouse { 16 | @Autowired 17 | ApplicationEventPublisher applicationEventPublisher; 18 | 19 | /** 20 | * Hành động bấm chuông cửa 21 | */ 22 | public void rangDoorbellBy(String guestName) { 23 | // Phát ra một sự kiện DoorBellEvent 24 | // source (Nguồn phát ra) chính là class này 25 | applicationEventPublisher.publishEvent(new DoorBellEvent(this, guestName)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-@Lazy-Anotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-9-@Lazy-Anotation 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-@Lazy-Anotation/src/main/java/me/loda/lazy/anotation/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package me.loda.lazy.anotation; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.Lazy; 6 | import org.springframework.stereotype.Component; 7 | 8 | 9 | @Configuration 10 | public class ApplicationConfig { 11 | 12 | 13 | @Bean 14 | public SecondBean secondBean() { 15 | return new SecondBean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-@Lazy-Anotation/src/main/java/me/loda/lazy/anotation/ExampleApplication.java: -------------------------------------------------------------------------------- 1 | package me.loda.lazy.anotation; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | @SpringBootApplication 7 | public class ExampleApplication { 8 | public static void main(String[] args) { 9 | AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ExampleApplication.class); 10 | System.out.println("ApplicationContext started!"); 11 | FirstBean firstBean = context.getBean(FirstBean.class); 12 | SecondBean secondBean = context.getBean(SecondBean.class); 13 | context.close(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-@Lazy-Anotation/src/main/java/me/loda/lazy/anotation/FirstBean.java: -------------------------------------------------------------------------------- 1 | package me.loda.lazy.anotation; 2 | 3 | import org.springframework.context.annotation.Lazy; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Lazy 7 | @Component 8 | public class FirstBean { 9 | public FirstBean() { 10 | System.out.println("Bean FirstBean đã được khởi tạo!"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-@Lazy-Anotation/src/main/java/me/loda/lazy/anotation/SecondBean.java: -------------------------------------------------------------------------------- 1 | package me.loda.lazy.anotation; 2 | 3 | public class SecondBean { 4 | public SecondBean() { 5 | System.out.println("Bean SecondBean đã được khởi tạo!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /spring-boot-@Lazy-Anotation/src/main/java/me/loda/lazy/anotation/ServiceBean.java: -------------------------------------------------------------------------------- 1 | package me.loda.lazy.anotation; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Lazy; 5 | 6 | public class ServiceBean { 7 | 8 | @Lazy 9 | @Autowired 10 | private FirstBean firstBean; 11 | 12 | public FirstBean getFirstBean() { 13 | return firstBean; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-application-context-events/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-application-context-events 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-boot-jpa-auditing/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-jpa-auditing 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-data-jpa 19 | 20 | 21 | mysql 22 | mysql-connector-java 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-boot-jpa-auditing/src/main/java/me/loda/spring/jpaauditing/AppParamsRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.jpaauditing; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | /** 6 | * Copyright 2019 {@author Loda} (https://loda.me). 7 | * This project is licensed under the MIT license. 8 | * 9 | * @since 2019-06-07 10 | * Github: https://github.com/loda-kun 11 | */ 12 | public interface AppParamsRepository extends JpaRepository { 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-jpa-auditing/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8081 2 | spring.datasource.url=jdbc:mysql://localhost:3306/lodadb?useSSL=false 3 | spring.datasource.username=root 4 | spring.datasource.password= 5 | 6 | 7 | ## Hibernate Properties 8 | # The SQL dialect makes Hibernate generate better SQL for the chosen database 9 | spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect 10 | # Hibernate ddl auto (create, create-drop, validate, update) 11 | spring.jpa.hibernate.ddl-auto = update 12 | 13 | logging.level.org.hibernate = ERROR -------------------------------------------------------------------------------- /spring-boot-jpa-auditing/src/main/resources/init.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE lodadb; 2 | use lodadb; 3 | 4 | CREATE TABLE `app_params` ( 5 | `id` int(11) NOT NULL AUTO_INCREMENT, 6 | `param_name` varchar(45) NOT NULL, 7 | `param_value` varchar(255) DEFAULT NULL, 8 | `created_at` datetime NOT NULL, 9 | `updated_at` datetime NOT NULL, 10 | PRIMARY KEY (`id`) 11 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -------------------------------------------------------------------------------- /spring-boot-swagger2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-swagger2 13 | 14 | 15 | 16 | io.springfox 17 | springfox-swagger2 18 | 2.9.2 19 | 20 | 21 | 22 | io.springfox 23 | springfox-swagger-ui 24 | 2.9.2 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | runtime 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-boot-swagger2/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | Vào link để xem chi tiết có hình ảnh minh họa: 3 | 4 | [Loda.me - RESTful API Document Tạo với Spring Boot + Swagger][loda-link] 5 | 6 | [loda-link]: https://loda.me/res-tful-api-document-tao-voi-spring-boot-swagger-loda1576222065972 7 | 8 | # Content without images 9 | 10 | ### Giới thiệu -------------------------------------------------------------------------------- /spring-boot-swagger2/src/main/java/me/loda/spring/swagger/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.swagger; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * Copyright 2019 {@author Loda} (https://loda.me). 8 | * This project is licensed under the MIT license. 9 | * 10 | * @since 2019-06-06 11 | * Github: https://github.com/loda-kun 12 | */ 13 | @SpringBootApplication 14 | public class App { 15 | public static void main(String[] args) { 16 | SpringApplication.run(App.class, args); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot-swagger2/src/main/java/me/loda/spring/swagger/model/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.swagger.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.GenerationType; 6 | import javax.persistence.Id; 7 | import javax.persistence.Table; 8 | 9 | import io.swagger.annotations.ApiModel; 10 | import io.swagger.annotations.ApiModelProperty; 11 | import lombok.AllArgsConstructor; 12 | import lombok.Data; 13 | import lombok.NoArgsConstructor; 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 2019-06-06 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Data 22 | @Entity 23 | @Table 24 | @NoArgsConstructor 25 | @AllArgsConstructor 26 | @ApiModel(value = "User model") 27 | public class User { 28 | @Id 29 | @GeneratedValue(strategy = GenerationType.IDENTITY) 30 | @ApiModelProperty(notes = "The database generated User ID") 31 | private Long id; 32 | 33 | private String firstName; 34 | private String lastName; 35 | private String email; 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-swagger2/src/main/java/me/loda/spring/swagger/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.swagger.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import me.loda.spring.swagger.model.User; 7 | /** 8 | * Copyright 2019 {@author Loda} (https://loda.me). 9 | * This project is licensed under the MIT license. 10 | * 11 | * @since 2019-06-06 12 | * Github: https://github.com/loda-kun 13 | */ 14 | @Repository 15 | public interface UserRepository extends JpaRepository { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-swagger3-openapi3/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | Vào link để xem chi tiết có hình ảnh minh họa: 3 | 4 | [Loda.me - RESTful API Document Tạo với Spring Boot + OpenApi 3.0][loda-link] 5 | 6 | [loda-link]: https://loda.me/res-tful-api-document-voi-spring-boot-open-api-3-0-loda1576741884390 7 | 8 | # Content without images 9 | 10 | ### Giới thiệu -------------------------------------------------------------------------------- /spring-boot-swagger3-openapi3/src/main/java/me.loda.spring.openapi3/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.openapi3; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | public static void main(String[] args) { 9 | // http://localhost:8080/v3/api-docs/ to show api document as a json 10 | // http://localhost:8080/swagger-ui/index.html to show api in the web-view 11 | SpringApplication.run(App.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-swagger3-openapi3/src/main/java/me.loda.spring.openapi3/model/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.openapi3.model; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import io.swagger.v3.oas.annotations.media.Schema; 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import javax.persistence.*; 10 | import javax.validation.constraints.NotBlank; 11 | import javax.validation.constraints.Size; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 2019-06-06 18 | * Github: https://github.com/loda-kun 19 | */ 20 | @Data 21 | @Entity 22 | @Table 23 | @NoArgsConstructor 24 | @AllArgsConstructor 25 | public class User { 26 | @Id 27 | @GeneratedValue(strategy = GenerationType.IDENTITY) 28 | @Schema(description = "User UUID in the database") 29 | @JsonProperty("id") 30 | private Long id; 31 | 32 | private @NotBlank @Size(min = 0, max = 20) String firstName; 33 | private String lastName; 34 | 35 | private @NotBlank @Size(min = 0, max = 50) String email; 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-swagger3-openapi3/src/main/java/me.loda.spring.openapi3/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.openapi3.repository; 2 | 3 | import me.loda.spring.openapi3.model.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | /** 8 | * Copyright 2019 {@author Loda} (https://loda.me). 9 | * This project is licensed under the MIT license. 10 | * 11 | * @since 2019-06-06 12 | * Github: https://github.com/loda-kun 13 | */ 14 | @Repository 15 | public interface UserRepository extends JpaRepository { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-webflux/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-boot-webflux 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-webflux 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-test 22 | test 23 | 24 | 25 | org.springframework 26 | spring-test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-webflux/src/main/java/loda/me/Employee.java: -------------------------------------------------------------------------------- 1 | package loda.me; 2 | 3 | public class Employee { 4 | private String id; 5 | private String name; 6 | 7 | public Employee(String id, String name) { 8 | this.id = id; 9 | this.name = name; 10 | } 11 | 12 | public String getId() { 13 | return id; 14 | } 15 | 16 | public void setId(String id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-webflux/src/main/java/loda/me/EmployeeApplication.java: -------------------------------------------------------------------------------- 1 | package loda.me; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.ComponentScan; 6 | 7 | @SpringBootApplication 8 | @ComponentScan("loda.me") 9 | public class EmployeeApplication { 10 | public static void main(String[] args) { 11 | SpringApplication.run(EmployeeApplication.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-webflux/src/main/java/loda/me/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package loda.me; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | import reactor.core.publisher.Flux; 7 | 8 | @RestController 9 | public class EmployeeController { 10 | @Autowired 11 | private EmployeeService employeeService; 12 | 13 | @GetMapping("/employees") 14 | private Flux getAllEmpployees() { 15 | return employeeService.findAll(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-webflux/src/main/java/loda/me/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package loda.me; 2 | 3 | import reactor.core.publisher.Flux; 4 | import reactor.core.publisher.Mono; 5 | 6 | public interface EmployeeService { 7 | Flux findAll(); 8 | } 9 | -------------------------------------------------------------------------------- /spring-boot-webflux/src/main/java/loda/me/EmployeeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package loda.me; 2 | 3 | import org.springframework.stereotype.Service; 4 | import reactor.core.publisher.Flux; 5 | 6 | @Service 7 | public class EmployeeServiceImpl implements EmployeeService { 8 | 9 | @Override 10 | public Flux findAll() { 11 | return Flux.just(new Employee("29750","AtomPtit") , new Employee("18273", "HungCD")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-webflux/src/test/java/loda/me/EmployeeControllerTest.java: -------------------------------------------------------------------------------- 1 | package loda.me; 2 | 3 | import org.junit.Test; 4 | import org.springframework.web.reactive.function.client.WebClient; 5 | public class EmployeeControllerTest { 6 | 7 | @Test 8 | public void TestGetAllEmpployees() { 9 | WebClient client = WebClient.builder().baseUrl("localhost:8080").build(); 10 | // Flux employees = 11 | client.get().uri("/employees") 12 | .exchange() 13 | .doOnSuccess(clientResponse -> { 14 | clientResponse.bodyToFlux(Employee.class).subscribe(employee -> System.out.println("$$$$$$$$$$$$$")); 15 | }) 16 | .doOnError(throwable -> throwable.printStackTrace()); 17 | // employees.subscribe(employee -> System.out.println("ok")); 18 | } 19 | } -------------------------------------------------------------------------------- /spring-cloud-config-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-config-client 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-config 18 | 2.1.0.RELEASE 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-webflux 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-cloud-config-client/src/main/java/me/loda/ClientService.java: -------------------------------------------------------------------------------- 1 | package me.loda; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ClientService { 8 | public static void main(String[] args) { 9 | SpringApplication.run(ClientService.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-cloud-config-client/src/main/java/me/loda/InfoRestController.java: -------------------------------------------------------------------------------- 1 | package me.loda; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | @RestController 11 | public class InfoRestController { 12 | @Value("${db.name:Data Null}") 13 | private String dbName; 14 | 15 | @Value("${db.username:Data Null}") 16 | private String dbUsername; 17 | 18 | @Value("${db.password:Data Null}") 19 | private String dbPassword; 20 | 21 | @Value("${message1:Data Null}") 22 | private String message1; 23 | 24 | @Value("${message2:Data Null}") 25 | private String message2; 26 | 27 | @RequestMapping("/info") 28 | Object getMessage() { 29 | Map data = new HashMap(); 30 | data.put("db.name", dbName); 31 | data.put("db.username", dbUsername); 32 | data.put("db.password", dbPassword); 33 | data.put("message1", message1); 34 | data.put("message2", message2); 35 | 36 | return data; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-cloud-config-client/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | #application name 2 | spring.application.name=ClientService 3 | #file name to get config 4 | spring.cloud.config.name=Service1,global 5 | # profile name 6 | spring.cloud.config.profile=default 7 | 8 | #Configuration server 9 | spring.cloud.config.uri=http://localhost:8000/ -------------------------------------------------------------------------------- /spring-cloud-config-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-cloud-config-server 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-config-server 18 | 2.0.4.RELEASE 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-cloud-config-server/src/main/java/me/loda/spring/springcloudconfig/ConfigServer.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springcloudconfig; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.config.server.EnableConfigServer; 6 | 7 | @EnableConfigServer 8 | @SpringBootApplication 9 | public class ConfigServer { 10 | public static void main(String[] args) { 11 | SpringApplication.run(ConfigServer.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-cloud-config-server/src/main/resources/bootstrap.properties: -------------------------------------------------------------------------------- 1 | #application name 2 | spring.application.name=vConfigServer 3 | #application port 4 | server.port=8000 5 | 6 | # make configuration files will be loaded from local 7 | spring.profiles.active=native 8 | 9 | # url contains config files on local 10 | spring.cloud.config.server.native.searchLocations=classpath:/config -------------------------------------------------------------------------------- /spring-cloud-config-server/src/main/resources/config/Service1.properties: -------------------------------------------------------------------------------- 1 | server.port=8100 2 | message1=File-Service1 -------------------------------------------------------------------------------- /spring-cloud-config-server/src/main/resources/config/Service2.properties: -------------------------------------------------------------------------------- 1 | server.port=8200 2 | message2=File-Service2 -------------------------------------------------------------------------------- /spring-cloud-config-server/src/main/resources/config/global.properties: -------------------------------------------------------------------------------- 1 | db.name=Test 2 | db.username=atom 3 | db.password=ptit 4 | -------------------------------------------------------------------------------- /spring-configuration-properties/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-configuration-properties 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-configuration-properties/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | loda: 2 | email: loda.namnh@gmail.com 3 | googleAnalyticsId: U-xxxxx 4 | authors: 5 | - loda 6 | - atom 7 | exampleMap: 8 | key1: hello 9 | key2: world -------------------------------------------------------------------------------- /spring-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-redis 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-data-jpa 18 | 19 | 20 | 21 | org.springframework.data 22 | spring-data-redis 23 | 24 | 25 | 26 | io.lettuce 27 | lettuce-core 28 | 5.1.3.RELEASE 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /spring-redis/src/main/java/me/loda/springredis/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.springredis; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/4/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @SpringBootApplication 22 | public class App { 23 | public static void main(String[] args) { 24 | SpringApplication.run(App.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-redis/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | redis.host=localhost 2 | redis.port=6379 3 | server.port=8000 -------------------------------------------------------------------------------- /spring-security-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-security-example 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-security 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-thymeleaf 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-security-example/src/main/java/me/loda/spring/springsecurity/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springsecurity; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 4/30/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @SpringBootApplication 22 | public class App { 23 | public static void main(String[] args) { 24 | SpringApplication.run(App.class, args); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-security-example/src/main/java/me/loda/spring/springsecurity/WebController.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springsecurity; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.web.bind.annotation.GetMapping; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 4/30/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Controller 22 | public class WebController { 23 | 24 | @GetMapping(value = {"/", "/home"}) 25 | public String homepage() { 26 | return "home"; 27 | } 28 | 29 | @GetMapping("/hello") 30 | public String hello() { 31 | return "hello"; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-security-example/src/main/resources/templates/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello 5 | 6 | 7 |

Chỉ những ai đăng nhập mới truy cập được trang này!

8 | 9 |
10 | 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /spring-security-example/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Example 5 | 6 | 7 |

Welcome!

8 | 9 |

login

10 |

Click here to see a loda homepage.

11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-security-hibernate-jwt/src/main/java/me/loda/springsecurityhibernatejwt/payload/LoginRequest.java: -------------------------------------------------------------------------------- 1 | package me.loda.springsecurityhibernatejwt.payload; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.validation.constraints.NotBlank; 12 | 13 | import lombok.Data; 14 | 15 | /** 16 | * Copyright 2019 {@author Loda} (https://loda.me). 17 | * This project is licensed under the MIT license. 18 | * 19 | * @since 5/1/2019 20 | * Github: https://github.com/loda-kun 21 | */ 22 | @Data 23 | public class LoginRequest { 24 | @NotBlank 25 | private String username; 26 | 27 | @NotBlank 28 | private String password; 29 | } 30 | -------------------------------------------------------------------------------- /spring-security-hibernate-jwt/src/main/java/me/loda/springsecurityhibernatejwt/payload/LoginResponse.java: -------------------------------------------------------------------------------- 1 | package me.loda.springsecurityhibernatejwt.payload; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import lombok.Data; 12 | 13 | /** 14 | * Copyright 2019 {@author Loda} (https://loda.me). 15 | * This project is licensed under the MIT license. 16 | * 17 | * @since 5/1/2019 18 | * Github: https://github.com/loda-kun 19 | */ 20 | @Data 21 | public class LoginResponse { 22 | private String accessToken; 23 | private String tokenType = "Bearer"; 24 | 25 | public LoginResponse(String accessToken) { 26 | this.accessToken = accessToken; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-security-hibernate-jwt/src/main/java/me/loda/springsecurityhibernatejwt/payload/RandomStuff.java: -------------------------------------------------------------------------------- 1 | package me.loda.springsecurityhibernatejwt.payload; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import lombok.AllArgsConstructor; 12 | import lombok.Data; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 5/1/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Data 22 | @AllArgsConstructor 23 | public class RandomStuff { 24 | private String message; 25 | } 26 | -------------------------------------------------------------------------------- /spring-security-hibernate-jwt/src/main/java/me/loda/springsecurityhibernatejwt/user/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.springsecurityhibernatejwt.user; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Column; 12 | import javax.persistence.Entity; 13 | import javax.persistence.GeneratedValue; 14 | import javax.persistence.Id; 15 | import javax.persistence.Table; 16 | 17 | import lombok.Data; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 4/30/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Entity 27 | @Table(name = "user") 28 | @Data 29 | public class User { 30 | @Id 31 | @GeneratedValue 32 | private Long id; 33 | 34 | @Column(nullable = false, unique = true) 35 | private String username; 36 | private String password; 37 | } 38 | -------------------------------------------------------------------------------- /spring-security-hibernate-jwt/src/main/java/me/loda/springsecurityhibernatejwt/user/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.springsecurityhibernatejwt.user; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | import org.springframework.stereotype.Repository; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 4/30/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Repository 22 | public interface UserRepository extends JpaRepository { 23 | User findByUsername(String username); 24 | } 25 | -------------------------------------------------------------------------------- /spring-security-hibernate/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-security-hibernate 13 | 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-security 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-thymeleaf 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-data-jpa 28 | 29 | 30 | 31 | com.h2database 32 | h2 33 | runtime 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-security-hibernate/src/main/java/me/loda/spring/springsecurityhibernate/WebController.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springsecurityhibernate; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.web.bind.annotation.GetMapping; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 4/30/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Controller 22 | public class WebController { 23 | 24 | @GetMapping(value = {"/", "/home"}) 25 | public String homepage() { 26 | return "home"; 27 | } 28 | 29 | @GetMapping("/hello") 30 | public String hello() { 31 | return "hello"; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-security-hibernate/src/main/java/me/loda/spring/springsecurityhibernate/user/User.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springsecurityhibernate.user; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import javax.persistence.Column; 12 | import javax.persistence.Entity; 13 | import javax.persistence.GeneratedValue; 14 | import javax.persistence.Id; 15 | import javax.persistence.Table; 16 | 17 | import lombok.Data; 18 | 19 | /** 20 | * Copyright 2019 {@author Loda} (https://loda.me). 21 | * This project is licensed under the MIT license. 22 | * 23 | * @since 4/30/2019 24 | * Github: https://github.com/loda-kun 25 | */ 26 | @Entity 27 | @Table(name = "user") 28 | @Data 29 | public class User { 30 | @Id 31 | @GeneratedValue 32 | private Long id; 33 | 34 | @Column(nullable = false, unique = true) 35 | private String username; 36 | private String password; 37 | } 38 | -------------------------------------------------------------------------------- /spring-security-hibernate/src/main/java/me/loda/spring/springsecurityhibernate/user/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.loda.spring.springsecurityhibernate.user; 2 | /******************************************************* 3 | * For Vietnamese readers: 4 | * Các bạn thân mến, mình rất vui nếu project này giúp 5 | * ích được cho các bạn trong việc học tập và công việc. Nếu 6 | * bạn sử dụng lại toàn bộ hoặc một phần source code xin để 7 | * lại dường dẫn tới github hoặc tên tác giá. 8 | * Xin cảm ơn! 9 | *******************************************************/ 10 | 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | import org.springframework.stereotype.Repository; 13 | 14 | /** 15 | * Copyright 2019 {@author Loda} (https://loda.me). 16 | * This project is licensed under the MIT license. 17 | * 18 | * @since 4/30/2019 19 | * Github: https://github.com/loda-kun 20 | */ 21 | @Repository 22 | public interface UserRepository extends JpaRepository { 23 | User findByUsername(String username); 24 | } 25 | -------------------------------------------------------------------------------- /spring-security-hibernate/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 7 | # Enabling H2 Console 8 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /spring-security-hibernate/src/main/resources/templates/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Hello 5 | 6 | 7 |

Chỉ những ai đăng nhập mới truy cập được trang này!

8 | 9 |
10 | 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /spring-security-hibernate/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring Security Example 5 | 6 | 7 |

Welcome!

8 | 9 |

login

10 |

Click here to see a loda homepage.

11 | 12 | 13 | -------------------------------------------------------------------------------- /test-mockito-basic/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-learning 7 | me.loda.spring 8 | 0.0.1-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | test-mockito-basic 13 | 14 | 15 | -------------------------------------------------------------------------------- /test-mockito-basic/readme.md: -------------------------------------------------------------------------------- 1 | # Source 2 | Vào link để xem chi tiết có hình ảnh minh họa: 3 | 4 | [Loda.me - [Test] Hướng dẫn toàn tập Mockito][loda-link] 5 | 6 | [loda-link]: https://loda.me/test-huong-dan-toan-tap-mockito-loda1576641016810 7 | 8 | # Content without images 9 | 10 | ### Giới thiệu -------------------------------------------------------------------------------- /test-mockito-basic/src/main/java/me/loda/springtest/App.java: -------------------------------------------------------------------------------- 1 | package me.loda.springtest; 2 | 3 | public class App { 4 | } 5 | -------------------------------------------------------------------------------- /test-mockito-basic/src/test/java/me/loda/springtest/CaptorTest.java: -------------------------------------------------------------------------------- 1 | package me.loda.springtest; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.ArgumentCaptor; 10 | import org.mockito.Captor; 11 | import org.mockito.Mock; 12 | import org.mockito.Mockito; 13 | import org.mockito.junit.MockitoJUnitRunner; 14 | 15 | @RunWith(MockitoJUnitRunner.class) 16 | public class CaptorTest { 17 | @Mock 18 | List list; 19 | 20 | @Captor 21 | ArgumentCaptor captor; 22 | 23 | @Test 24 | public void testCaptor1() { 25 | list.add(1); 26 | // Capture 2 lần gọi add có giá trị như thế nào 27 | Mockito.verify(list).add(captor.capture()); 28 | 29 | System.out.println(captor.getAllValues()); 30 | 31 | Assert.assertEquals(1, captor.getValue()); 32 | } 33 | 34 | @Test 35 | public void testCaptor2() { 36 | list.add(1); 37 | list.add("String"); 38 | // Capture 2 lần gọi add có giá trị như thế nào 39 | Mockito.verify(list, Mockito.times(2)).add(captor.capture()); 40 | 41 | System.out.println(captor.getAllValues()); 42 | 43 | Assert.assertEquals(Arrays.asList(1, "String"), captor.getAllValues()); 44 | Assert.assertEquals("String", captor.getValue()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /test-mockito-basic/src/test/java/me/loda/springtest/SpyTest.java: -------------------------------------------------------------------------------- 1 | package me.loda.springtest; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.Mockito; 10 | import org.mockito.Spy; 11 | import org.mockito.junit.MockitoJUnitRunner; 12 | 13 | @RunWith(MockitoJUnitRunner.class) 14 | public class SpyTest { 15 | @Spy 16 | List list = new ArrayList<>(); 17 | 18 | @Test 19 | public void testSpy() { 20 | list.add("one"); 21 | list.add("two"); 22 | // show the list items 23 | System.out.println(list); 24 | 25 | Mockito.verify(list).add("one"); 26 | Mockito.verify(list).add("two"); 27 | 28 | // @Spy thực sự gọi hàm .add của List nên nó có size là 2 mà không cần giả lập 29 | Assert.assertEquals(2, list.size()); 30 | 31 | // Vẫn có thể làm giả thông tin gọi hàm với @Spy 32 | Mockito.when(list.size()).thenReturn(100); 33 | 34 | Assert.assertEquals(100, list.size()); 35 | } 36 | } 37 | --------------------------------------------------------------------------------