├── .gitignore ├── README.md ├── chapter1-1 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter1 │ │ │ ├── Chapter1Application.java │ │ │ └── controller │ │ │ └── HelloController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter1 │ └── Chapter1ApplicationTests.java ├── chapter2-1 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter21 │ │ │ ├── Chapter21Application.java │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ └── service │ │ │ └── BlogService.java │ └── resources │ │ ├── application-dev.properties │ │ ├── application-online.properties │ │ ├── application-test.properties │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter21 │ └── Chapter21ApplicationTests.java ├── chapter3-1 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter31 │ │ │ ├── Chapter31Application.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ └── domain │ │ │ └── User.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter31 │ └── Chapter31ApplicationTests.java ├── chapter3-2 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter32 │ │ │ ├── Chapter32Application.java │ │ │ └── controller │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── index.html │ └── test │ └── java │ └── com │ └── frank │ └── chapter32 │ └── Chapter32ApplicationTests.java ├── chapter3-3 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter33 │ │ │ ├── Chapter33Application.java │ │ │ └── controller │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── index.ftl │ │ └── login.ftl │ └── test │ └── java │ └── com │ └── frank │ └── chapter33 │ └── Chapter33ApplicationTests.java ├── chapter3-4 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter34 │ │ │ ├── Chapter34Application.java │ │ │ └── controller │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── index.vm │ └── test │ └── java │ └── com │ └── frank │ └── chapter34 │ └── Chapter34ApplicationTests.java ├── chapter3-5 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter35 │ │ │ ├── Chapter35Application.java │ │ │ ├── config │ │ │ └── SwaggerConfig.java │ │ │ └── controller │ │ │ └── HelloController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter35 │ └── Chapter35ApplicationTests.java ├── chapter3-6 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter36 │ │ │ ├── Chapter36Application.java │ │ │ ├── controller │ │ │ └── HelloController.java │ │ │ ├── dto │ │ │ └── ErrorInfo.java │ │ │ └── exception │ │ │ ├── GlobalExceptionHandler.java │ │ │ └── MyException.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── error.html │ │ └── index.html │ └── test │ └── java │ └── com │ └── frank │ └── chapter36 │ └── Chapter36ApplicationTests.java ├── chapter3-7 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── README.md ├── front-vue │ ├── .babelrc │ ├── .editorconfig │ ├── .eslintignore │ ├── .eslintrc.js │ ├── .gitignore │ ├── .postcssrc.js │ ├── README.md │ ├── config │ │ ├── dev.env.js │ │ ├── index.js │ │ └── prod.env.js │ ├── index.html │ ├── package.json │ ├── src │ │ ├── App.vue │ │ ├── assets │ │ │ └── logo.png │ │ ├── components │ │ │ └── HelloWorld.vue │ │ ├── main.js │ │ └── router │ │ │ └── index.js │ ├── static │ │ └── .gitkeep │ └── yarn.lock ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter37 │ │ │ ├── Chapter37Application.java │ │ │ ├── config │ │ │ └── WebMvcConfig.java │ │ │ └── controller │ │ │ └── IndexController.java │ └── resources │ │ ├── application.properties │ │ ├── static │ │ ├── css │ │ │ └── app.739f1ad65f043fab3efe7a450cd54da9.css │ │ └── js │ │ │ ├── app.de27c634d7ad9dab3885.js │ │ │ ├── app.de27c634d7ad9dab3885.js.map │ │ │ ├── manifest.ff985a587e3fe0e8abca.js │ │ │ ├── manifest.ff985a587e3fe0e8abca.js.map │ │ │ ├── vendor.dea99b46850552e56485.js │ │ │ └── vendor.dea99b46850552e56485.js.map │ │ └── templates │ │ └── index.ftl │ └── test │ └── java │ └── com │ └── frank │ └── chapter37 │ └── Chapter37ApplicationTests.java ├── chapter4-1 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter41 │ │ │ ├── Chapter41Application.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter41 │ └── Chapter41ApplicationTests.java ├── chapter4-10 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── data │ └── elasticsearch │ │ └── nodes │ │ └── 0 │ │ ├── _state │ │ └── global-0.st │ │ └── node.lock ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter410 │ │ │ ├── Chapter410Application.java │ │ │ ├── config │ │ │ └── ElasticSearchConfig.java │ │ │ ├── domain │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter410 │ └── Chapter410ApplicationTests.java ├── chapter4-11 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter411 │ │ │ ├── Chapter411Application.java │ │ │ ├── domain │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter411 │ └── Chapter411ApplicationTests.java ├── chapter4-12 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter412 │ │ │ ├── Chapter412Application.java │ │ │ ├── codegen │ │ │ ├── GenerationCodeUtil.java │ │ │ ├── GenerationRepo.java │ │ │ ├── GenerationService.java │ │ │ ├── SimpleConfGenerator.java │ │ │ └── SimpleJavaGenerator.java │ │ │ ├── config │ │ │ ├── BdUseDbConfig.java │ │ │ ├── DadaDbConfig.java │ │ │ ├── JOOQConfig.java │ │ │ ├── SpringTransaction.java │ │ │ └── SpringTransactionProvider.java │ │ │ ├── constant │ │ │ └── Constants.java │ │ │ ├── entity │ │ │ └── test │ │ │ │ ├── DefaultCatalog.java │ │ │ │ ├── Keys.java │ │ │ │ ├── Tables.java │ │ │ │ ├── Test.java │ │ │ │ └── tables │ │ │ │ ├── User.java │ │ │ │ ├── pojos │ │ │ │ └── User.java │ │ │ │ └── records │ │ │ │ └── UserRecord.java │ │ │ ├── repository │ │ │ ├── BaseRepository.java │ │ │ ├── TestBaseRepository.java │ │ │ └── test │ │ │ │ └── UserRepository.java │ │ │ ├── service │ │ │ └── test │ │ │ │ └── UserService.java │ │ │ └── utils │ │ │ └── CamelUnderlineTransformUtils.java │ └── resources │ │ ├── application.properties │ │ └── test_2017-11-06.sql │ └── test │ └── java │ └── com │ └── frank │ └── chapter412 │ └── Chapter412ApplicationTests.java ├── chapter4-13 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter413 │ │ │ ├── Chapter413Application.java │ │ │ ├── controller │ │ │ └── CityController.java │ │ │ ├── domain │ │ │ └── City.java │ │ │ └── mapper │ │ │ └── CityMapper.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter413 │ └── Chapter413ApplicationTests.java ├── chapter4-2 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter42 │ │ │ ├── Chapter42Application.java │ │ │ ├── dao │ │ │ └── BdChargeInfoRepository.java │ │ │ └── domain │ │ │ └── BdChargeInfo.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter42 │ └── Chapter42ApplicationTests.java ├── chapter4-3 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter43 │ │ │ ├── Chapter43Application.java │ │ │ └── config │ │ │ └── DataSourceConfig.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter43 │ └── Chapter43ApplicationTests.java ├── chapter4-4 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter44 │ │ │ ├── Chapter44Application.java │ │ │ ├── DataSourceConfig.java │ │ │ ├── PrimaryConfig.java │ │ │ ├── SecondaryConfig.java │ │ │ └── domain │ │ │ ├── p │ │ │ ├── User.java │ │ │ └── UserRepository.java │ │ │ └── s │ │ │ ├── Message.java │ │ │ └── MessageRepository.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter44 │ └── Chapter44ApplicationTests.java ├── chapter4-5 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter45 │ │ │ ├── Chapter45Application.java │ │ │ ├── RedisConfig.java │ │ │ ├── RedisObjectSerializer.java │ │ │ └── domain │ │ │ └── User.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter45 │ └── Chapter45ApplicationTests.java ├── chapter4-6 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter46 │ │ │ ├── Chapter46Application.java │ │ │ └── domain │ │ │ ├── User.java │ │ │ └── UserRepository.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter46 │ └── Chapter46ApplicationTests.java ├── chapter4-7 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter47 │ │ │ ├── Chapter47Application.java │ │ │ └── domain │ │ │ ├── User.java │ │ │ └── UserMapper.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter47 │ └── Chapter47ApplicationTests.java ├── chapter4-8 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter48 │ │ │ ├── Chapter48Application.java │ │ │ └── domain │ │ │ ├── User.java │ │ │ └── UserMapper.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter48 │ └── Chapter48ApplicationTests.java ├── chapter4-9 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter49 │ │ │ ├── Chapter49Application.java │ │ │ ├── domain │ │ │ ├── User.java │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ └── UserService.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter49 │ └── Chapter49ApplicationTests.java ├── chapter5-1 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter51 │ │ │ ├── Chapter51Application.java │ │ │ └── task │ │ │ └── ScheduledTasks.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter51 │ └── Chapter51ApplicationTests.java ├── chapter5-2 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter52 │ │ │ ├── Chapter52Application.java │ │ │ └── task │ │ │ └── Task.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter52 │ └── Chapter52ApplicationTests.java ├── chapter5-3 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── logs │ ├── all.log │ ├── error.log │ └── my.log ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter53 │ │ │ └── Chapter53Application.java │ └── resources │ │ ├── application-dev.properties │ │ ├── application-prod.properties │ │ ├── application-test.properties │ │ ├── application.properties │ │ └── log4j.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter53 │ └── Chapter53ApplicationTests.java ├── chapter5-4 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter54 │ │ │ ├── Chapter54Application.java │ │ │ ├── aspect │ │ │ └── WebLogAspect.java │ │ │ ├── log │ │ │ └── MongoAppender.java │ │ │ └── web │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── log4j.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter54 │ └── Chapter54ApplicationTests.java ├── chapter5-5 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── logs │ ├── all.log │ ├── error.log │ └── my.log ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter55 │ │ │ ├── Chapter55Application.java │ │ │ ├── aspect │ │ │ └── WebLogAspect.java │ │ │ └── web │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── log4j.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter55 │ └── Chapter55ApplicationTests.java ├── chapter5-6 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter56 │ │ │ ├── Chapter56Application.java │ │ │ ├── config │ │ │ └── WebSecurityConfig.java │ │ │ └── web │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── hello.html │ │ ├── index.html │ │ └── login.html │ └── test │ └── java │ └── com │ └── frank │ └── chapter56 │ └── Chapter56ApplicationTests.java ├── chapter6-1 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── man.jpg ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter61 │ │ │ └── Chapter61Application.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── template.vm │ └── test │ └── java │ └── com │ └── frank │ └── chapter61 │ └── Chapter61ApplicationTests.java ├── chapter6-2 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter62 │ │ │ ├── Chapter62Application.java │ │ │ └── rabbit │ │ │ ├── RabbitConfig.java │ │ │ ├── Receiver.java │ │ │ └── Sender.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter62 │ └── Chapter62ApplicationTests.java ├── chapter6-3 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter63 │ │ │ ├── Chapter63Application.java │ │ │ ├── EventConfig.java │ │ │ ├── Events.java │ │ │ ├── StateMachineConfig.java │ │ │ └── States.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter63 │ └── Chapter63ApplicationTests.java ├── chapter6-4 ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── frank │ │ │ └── chapter64 │ │ │ ├── Chapter64Application.java │ │ │ ├── config │ │ │ ├── DataflowJobConfig.java │ │ │ ├── EmbedZookeeperServer.java │ │ │ ├── JobEventConfig.java │ │ │ ├── SimpleJobConfig.java │ │ │ └── ZookeeperRegistryCenterConfig.java │ │ │ ├── domain │ │ │ └── Foo.java │ │ │ ├── job │ │ │ ├── dataflow │ │ │ │ └── SpringDataflowJob.java │ │ │ └── simple │ │ │ │ └── SpringSimpleJob.java │ │ │ └── repository │ │ │ └── FooRepository.java │ └── resources │ │ ├── application-dev.properties │ │ └── application.properties │ └── test │ └── java │ └── com │ └── frank │ └── chapter64 │ └── Chapter64ApplicationTests.java ├── chapter7-1-1 ├── .gitignore ├── .idea │ └── workspace.xml ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── william │ │ └── chapter711 │ │ └── Chapter711Application.java │ └── resources │ └── application.properties └── chapter7-1 ├── .gitignore ├── pom.xml └── src └── main ├── java └── com │ └── william │ └── chapter71 │ ├── autoconfigure │ ├── GreeterAutoConfiguration.java │ └── GreeterProperties.java │ └── domain │ ├── Greeter.java │ └── GreeterConfig.java └── resources └── META-INF └── spring.factories /.gitignore: -------------------------------------------------------------------------------- 1 | .mvn/* 2 | mvnw 3 | mvnw.cmd 4 | *.iml 5 | target/* 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpringBootDemo 2 | SpringBoot教程系列,用于实战SprinBoot常用到的技术点 3 | -------------------------------------------------------------------------------- /chapter1-1/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter1-1/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter1-1/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter1-1/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter1-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.frank 7 | chapter1 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | chapter1 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter1-1/src/main/java/com/frank/chapter1/Chapter1Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter1; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter1Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter1Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter1-1/src/main/java/com/frank/chapter1/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter1.controller; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestMethod; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/9. 9 | */ 10 | @RestController 11 | public class HelloController { 12 | @RequestMapping(value = "/hello",method = RequestMethod.GET) 13 | public String index(){ 14 | return "Hello World!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter1-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter1-1/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter1-1/src/test/java/com/frank/chapter1/Chapter1ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter1; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter1ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter2-1/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter2-1/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter2-1/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter2-1/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter2-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.frank 7 | chapter2-1 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | chapter2-1 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter2-1/src/main/java/com/frank/chapter21/Chapter21Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter21; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter21Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter21Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter2-1/src/main/java/com/frank/chapter21/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter21.controller; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestMethod; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/9. 9 | */ 10 | @RestController 11 | public class HelloController { 12 | @RequestMapping(value = "/hello",method= RequestMethod.GET) 13 | public String index(){ 14 | return "Hello World!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter2-1/src/main/java/com/frank/chapter21/service/BlogService.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter21.service; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * Created by jianweilin on 2017/10/9. 8 | */ 9 | @Component 10 | public class BlogService { 11 | 12 | @Value("${com.frank.blog.name}") 13 | private String name; 14 | @Value("${com.frank.blog.title}") 15 | private String title; 16 | @Value("${com.frank.blog.desc}") 17 | private String desc; 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public String getTitle() { 28 | return title; 29 | } 30 | 31 | public void setTitle(String title) { 32 | this.title = title; 33 | } 34 | 35 | public String getDesc() { 36 | return desc; 37 | } 38 | 39 | public void setDesc(String desc) { 40 | this.desc = desc; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /chapter2-1/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | server.port=1111 -------------------------------------------------------------------------------- /chapter2-1/src/main/resources/application-online.properties: -------------------------------------------------------------------------------- 1 | server.port=2222 -------------------------------------------------------------------------------- /chapter2-1/src/main/resources/application-test.properties: -------------------------------------------------------------------------------- 1 | server.port=3333 -------------------------------------------------------------------------------- /chapter2-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | com.frank.blog.name=dayday up 2 | com.frank.blog.title=SpringBoot 3 | com.frank.blog.desc=${com.frank.blog.name},writing ${com.frank.blog.title} 4 | spring.profiles.active=dev -------------------------------------------------------------------------------- /chapter2-1/src/test/java/com/frank/chapter21/Chapter21ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter21; 2 | 3 | import com.frank.chapter21.service.BlogService; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class Chapter21ApplicationTests { 15 | private static final Logger logger = LoggerFactory.getLogger(Chapter21ApplicationTests.class); 16 | 17 | @Autowired 18 | private BlogService blogService; 19 | 20 | @Test 21 | public void test_001() { 22 | logger.info("name: " + blogService.getName()); 23 | logger.info("title: " + blogService.getTitle()); 24 | logger.info("desc: " + blogService.getDesc()); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /chapter3-1/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-1/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-1/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-1/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-1/src/main/java/com/frank/chapter31/Chapter31Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter31; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter31Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter31Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-1/src/main/java/com/frank/chapter31/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter31.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.frank.chapter31.domain.User; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * Created by jianweilin on 2017/10/11. 13 | */ 14 | @RestController 15 | public class UserController { 16 | private static final Logger logger = LoggerFactory.getLogger(UserController.class); 17 | 18 | @RequestMapping(value = "/list",method = RequestMethod.GET) 19 | public List getUserList(){ 20 | return User.initUsers(); 21 | } 22 | 23 | @RequestMapping(value = "/",method = RequestMethod.POST) 24 | public String postUser(@ModelAttribute User user){ 25 | logger.info("user => " + JSON.toJSONString(user)); 26 | return "add success"; 27 | } 28 | 29 | @RequestMapping(value = "/{id}",method = RequestMethod.GET) 30 | public User getUser(@PathVariable Long id) { 31 | logger.info("userId => " + id); 32 | return new User(id,"init",0); 33 | } 34 | 35 | @RequestMapping(value = "/{id}",method = RequestMethod.PUT) 36 | public User putUser(@PathVariable Long id,@ModelAttribute User user) { 37 | logger.info("userId => " + id); 38 | return new User(id,user.getName(),user.getAge()); 39 | } 40 | 41 | @RequestMapping(value = "/{id}",method = RequestMethod.DELETE) 42 | public String deleteUser(@PathVariable Long id) { 43 | logger.info("delte userId => " + id); 44 | return "delete success"; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /chapter3-1/src/main/java/com/frank/chapter31/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter31.domain; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by jianweilin on 2017/10/11. 8 | */ 9 | public class User { 10 | private Long id; 11 | private String name; 12 | private Integer age; 13 | 14 | public static List initUsers(){ 15 | return Arrays.asList(new User(1L, "张三", 21), new User(2L, "李四", 22)); 16 | } 17 | 18 | public User() { 19 | } 20 | 21 | public User(Long id, String name, Integer age) { 22 | this.id = id; 23 | this.name = name; 24 | this.age = age; 25 | } 26 | 27 | public Long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Long id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public Integer getAge() { 44 | return age; 45 | } 46 | 47 | public void setAge(Integer age) { 48 | this.age = age; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /chapter3-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-1/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter3-1/src/test/java/com/frank/chapter31/Chapter31ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter31; 2 | 3 | import com.frank.chapter31.controller.UserController; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | import org.springframework.test.web.servlet.MockMvc; 10 | import org.springframework.test.web.servlet.RequestBuilder; 11 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 12 | 13 | import static org.hamcrest.core.IsEqual.equalTo; 14 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 15 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 16 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 17 | 18 | 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest 21 | public class Chapter31ApplicationTests { 22 | 23 | private MockMvc mockMvc; 24 | 25 | @Before 26 | public void setUp(){ 27 | mockMvc = MockMvcBuilders.standaloneSetup(new UserController()).build(); 28 | } 29 | 30 | @Test 31 | public void test_001() throws Exception { 32 | // 测试UserController 33 | RequestBuilder request = null; 34 | 35 | request = post("/") 36 | .param("id","1") 37 | .param("name","测试大师") 38 | .param("age","11"); 39 | mockMvc.perform(request).andExpect(status().isOk()) 40 | .andExpect(content().string(equalTo("add success"))); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter3-2/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-2/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-2/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-2/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-2/src/main/java/com/frank/chapter32/Chapter32Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter32; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter32Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter32Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-2/src/main/java/com/frank/chapter32/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter32.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestMethod; 7 | import org.springframework.web.bind.annotation.ResponseBody; 8 | 9 | /** 10 | * Created by jianweilin on 2017/10/11. 11 | */ 12 | @Controller 13 | public class HelloController { 14 | @ResponseBody 15 | @RequestMapping(value = "/hello",method = RequestMethod.GET) 16 | public String hello() { 17 | return "Hello World!"; 18 | } 19 | 20 | @RequestMapping("/") 21 | public String index(ModelMap map){ 22 | map.addAttribute("host","http://www.jianwl.com"); 23 | return "index"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /chapter3-2/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-2/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter3-2/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 |

Hello World

9 | 10 | -------------------------------------------------------------------------------- /chapter3-2/src/test/java/com/frank/chapter32/Chapter32ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter32; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter32ApplicationTests { 11 | @Test 12 | public void test_001(){ 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter3-3/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-3/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-3/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-3/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-3/src/main/java/com/frank/chapter33/Chapter33Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter33; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter33Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter33Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-3/src/main/java/com/frank/chapter33/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter33.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestMethod; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/11. 10 | */ 11 | @Controller 12 | public class HelloController { 13 | 14 | @RequestMapping(value = "/index",method = RequestMethod.GET) 15 | public String index(ModelMap map) { 16 | map.addAttribute("host","http://www.jianwl.com"); 17 | return "index"; 18 | } 19 | 20 | @RequestMapping(value = "/",method = RequestMethod.GET) 21 | public String home() { 22 | return "login"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /chapter3-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=9000 -------------------------------------------------------------------------------- /chapter3-3/src/main/resources/templates/index.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | FreeMarker模板引擎 9 |

${host}

10 | 11 | -------------------------------------------------------------------------------- /chapter3-3/src/main/resources/templates/login.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 小工具平台 10 | 11 | 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /chapter3-3/src/test/java/com/frank/chapter33/Chapter33ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter33; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter33ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter3-4/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-4/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-4/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-4/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-4/src/main/java/com/frank/chapter34/Chapter34Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter34; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter34Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter34Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-4/src/main/java/com/frank/chapter34/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter34.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestMethod; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/11. 10 | */ 11 | @Controller 12 | public class HelloController { 13 | @RequestMapping(value = "/",method = RequestMethod.GET) 14 | public String index(ModelMap map) { 15 | map.addAttribute("host","http://www.jianwl.com"); 16 | return "index"; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /chapter3-4/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-4/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter3-4/src/main/resources/templates/index.vm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | Velocity模板 9 |

${host}

10 | 11 | -------------------------------------------------------------------------------- /chapter3-4/src/test/java/com/frank/chapter34/Chapter34ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter34; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter34ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter3-5/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-5/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-5/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-5/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-5/src/main/java/com/frank/chapter35/Chapter35Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter35; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter35Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter35Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-5/src/main/java/com/frank/chapter35/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter35.controller; 2 | 3 | import io.swagger.annotations.*; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | /** 10 | * 11 | * Created by jianweilin on 2017/10/11. 12 | */ 13 | @Api(value = "Hello接口模块") 14 | @RestController 15 | public class HelloController { 16 | 17 | // @ApiImplicitParams( 18 | // value = { 19 | // @ApiImplicitParam(paramType = "header",name="User-Id",required = true,dataType = "string"), 20 | // @ApiImplicitParam(paramType = "header",name="User-Token",required = true,dataType = "string") 21 | // } 22 | // ) 23 | @ApiOperation(value = "hello world接口",notes = "swagger文档,请访问http://localhost:8080/swagger-ui.html") 24 | @RequestMapping(value = "/hello",method = RequestMethod.GET) 25 | public String index(@ApiParam(value = "名字",required = true)@RequestParam String name){ 26 | return "Hello World: " + name ; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /chapter3-5/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-5/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter3-5/src/test/java/com/frank/chapter35/Chapter35ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter35; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter35ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter3-6/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-6/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-6/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-6/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-6/src/main/java/com/frank/chapter36/Chapter36Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter36; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter36Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter36Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-6/src/main/java/com/frank/chapter36/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter36.controller; 2 | 3 | import com.frank.chapter36.exception.MyException; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.ModelMap; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * Created by jianweilin on 2017/10/11. 11 | */ 12 | @Controller 13 | public class HelloController { 14 | 15 | @RequestMapping(value = "/hello",method = RequestMethod.GET) 16 | public String hello() throws Exception { 17 | throw new Exception("发生错误"); 18 | } 19 | 20 | @RequestMapping("/json") 21 | public String json() throws MyException { 22 | throw new MyException("发生错误2"); 23 | } 24 | 25 | @RequestMapping("/") 26 | public String index(ModelMap map){ 27 | map.addAttribute("host","http://jianwl.com"); 28 | return "index"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter3-6/src/main/java/com/frank/chapter36/dto/ErrorInfo.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter36.dto; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/11. 5 | */ 6 | public class ErrorInfo { 7 | public static final Integer OK = 0; 8 | public static final Integer ERROR = 100; 9 | 10 | private Integer code; 11 | private String message; 12 | private String url; 13 | private T data; 14 | 15 | public static Integer getOK() { 16 | return OK; 17 | } 18 | 19 | public static Integer getERROR() { 20 | return ERROR; 21 | } 22 | 23 | public Integer getCode() { 24 | return code; 25 | } 26 | 27 | public void setCode(Integer code) { 28 | this.code = code; 29 | } 30 | 31 | public String getMessage() { 32 | return message; 33 | } 34 | 35 | public void setMessage(String message) { 36 | this.message = message; 37 | } 38 | 39 | public String getUrl() { 40 | return url; 41 | } 42 | 43 | public void setUrl(String url) { 44 | this.url = url; 45 | } 46 | 47 | public T getData() { 48 | return data; 49 | } 50 | 51 | public void setData(T data) { 52 | this.data = data; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /chapter3-6/src/main/java/com/frank/chapter36/exception/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter36.exception; 2 | 3 | import com.frank.chapter36.dto.ErrorInfo; 4 | import org.springframework.web.bind.annotation.ControllerAdvice; 5 | import org.springframework.web.bind.annotation.ExceptionHandler; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | import org.springframework.web.servlet.ModelAndView; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | 11 | /** 12 | * Created by jianweilin on 2017/10/11. 13 | */ 14 | @ControllerAdvice 15 | public class GlobalExceptionHandler { 16 | @ExceptionHandler(value = Exception.class) 17 | public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception{ 18 | ModelAndView mav = new ModelAndView(); 19 | mav.addObject("exception",e); 20 | mav.addObject("url",req.getRequestURL()); 21 | mav.setViewName("error"); 22 | return mav; 23 | } 24 | 25 | @ExceptionHandler(value = MyException.class) 26 | @ResponseBody 27 | public ErrorInfo jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception { 28 | ErrorInfo r = new ErrorInfo<>(); 29 | r.setCode(ErrorInfo.ERROR); 30 | r.setData("Some Data"); 31 | r.setMessage(e.getMessage()); 32 | r.setUrl(req.getRequestURL().toString()); 33 | return r; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /chapter3-6/src/main/java/com/frank/chapter36/exception/MyException.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter36.exception; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/11. 5 | */ 6 | public class MyException extends Exception{ 7 | public MyException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /chapter3-6/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-6/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter3-6/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 统一异常处理 6 | 7 | 8 |

Error Handler

9 |
10 |
11 | 12 | -------------------------------------------------------------------------------- /chapter3-6/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 |

Hello World!

9 | 10 | -------------------------------------------------------------------------------- /chapter3-6/src/test/java/com/frank/chapter36/Chapter36ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter36; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter36ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter3-7/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter3-7/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-7/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter3-7/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip 2 | -------------------------------------------------------------------------------- /chapter3-7/README.md: -------------------------------------------------------------------------------- 1 | ## Running Project 2 | 3 | ``` bash 4 | # use npm install vue-cli 5 | npm install -g vue-cli 6 | 7 | # init front-vue 8 | vue init webpack front-vue 9 | 10 | # cd front-vue project & run 11 | cd front-vue 12 | npm run dev 13 | 14 | # compile 15 | npm run build 16 | 17 | # copy index.html to springboot resource templates folder,rename index.ftl & copy js & css resource to springboot resources 18 | 19 | # run springboot 20 | ``` 21 | 22 | For a detailed explanation on how things work, check out the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader). 23 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "modules": false, 5 | "targets": { 6 | "browsers": ["> 1%", "last 2 versions", "not ie <= 8"] 7 | } 8 | }], 9 | "stage-2" 10 | ], 11 | "plugins": ["transform-runtime"], 12 | "env": { 13 | "test": { 14 | "presets": ["env", "stage-2"], 15 | "plugins": ["istanbul"] 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/.eslintignore: -------------------------------------------------------------------------------- 1 | build/*.js 2 | config/*.js 3 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/.eslintrc.js: -------------------------------------------------------------------------------- 1 | // https://eslint.org/docs/user-guide/configuring 2 | 3 | module.exports = { 4 | root: true, 5 | parser: 'babel-eslint', 6 | parserOptions: { 7 | sourceType: 'module' 8 | }, 9 | env: { 10 | browser: true, 11 | }, 12 | // https://github.com/standard/standard/blob/master/docs/RULES-en.md 13 | extends: 'standard', 14 | // required to lint *.vue files 15 | plugins: [ 16 | 'html' 17 | ], 18 | // add your custom rules here 19 | 'rules': { 20 | // allow paren-less arrow functions 21 | 'arrow-parens': 0, 22 | // allow async-await 23 | 'generator-star-spacing': 0, 24 | // allow debugger during development 25 | 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | dist/ 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Editor directories and files 9 | .idea 10 | .vscode 11 | *.suo 12 | *.ntvs* 13 | *.njsproj 14 | *.sln 15 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/.postcssrc.js: -------------------------------------------------------------------------------- 1 | // https://github.com/michael-ciniawsky/postcss-load-config 2 | 3 | module.exports = { 4 | "plugins": { 5 | // to edit target browsers: use "browserslist" field in package.json 6 | "autoprefixer": {} 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/README.md: -------------------------------------------------------------------------------- 1 | # front-vue 2 | 3 | > A Vue.js project 4 | 5 | ## Build Setup 6 | 7 | ``` bash 8 | # install dependencies 9 | npm install 10 | 11 | # serve with hot reload at localhost:8080 12 | npm run dev 13 | 14 | # build for production with minification 15 | npm run build 16 | 17 | # build for production and view the bundle analyzer report 18 | npm run build --report 19 | ``` 20 | 21 | For a detailed explanation on how things work, check out the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader). 22 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/config/dev.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const merge = require('webpack-merge') 3 | const prodEnv = require('./prod.env') 4 | 5 | module.exports = merge(prodEnv, { 6 | NODE_ENV: '"development"' 7 | }) 8 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/config/index.js: -------------------------------------------------------------------------------- 1 | 2 | 'use strict' 3 | // Template version: 1.1.3 4 | // see http://vuejs-templates.github.io/webpack for documentation. 5 | 6 | const path = require('path') 7 | 8 | module.exports = { 9 | build: { 10 | env: require('./prod.env'), 11 | index: path.resolve(__dirname, '../dist/index.ftl'), 12 | assetsRoot: path.resolve(__dirname, '../dist'), 13 | assetsSubDirectory: 'static', 14 | assetsPublicPath: '/', 15 | productionSourceMap: true, 16 | // Gzip off by default as many popular static hosts such as 17 | // Surge or Netlify already gzip all static assets for you. 18 | // Before setting to `true`, make sure to: 19 | // npm install --save-dev compression-webpack-plugin 20 | productionGzip: false, 21 | productionGzipExtensions: ['js', 'css'], 22 | // Run the build command with an extra argument to 23 | // View the bundle analyzer report after build finishes: 24 | // `npm run build --report` 25 | // Set to `true` or `false` to always turn it on or off 26 | bundleAnalyzerReport: process.env.npm_config_report 27 | }, 28 | dev: { 29 | env: require('./dev.env'), 30 | port: process.env.PORT || 8080, 31 | autoOpenBrowser: true, 32 | assetsSubDirectory: 'static', 33 | assetsPublicPath: '/', 34 | proxyTable: {}, 35 | // CSS Sourcemaps off by default because relative paths are "buggy" 36 | // with this option, according to the CSS-Loader README 37 | // (https://github.com/webpack/css-loader#sourcemaps) 38 | // In our experience, they generally work as expected, 39 | // just be aware of this issue when enabling this option. 40 | cssSourceMap: false 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/config/prod.env.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | module.exports = { 3 | NODE_ENV: '"production"' 4 | } 5 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | front-vue 6 | 7 | 8 |
9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/src/App.vue: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 | 14 | 24 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-7/front-vue/src/assets/logo.png -------------------------------------------------------------------------------- /chapter3-7/front-vue/src/components/HelloWorld.vue: -------------------------------------------------------------------------------- 1 | 22 | 23 | 33 | 34 | 35 | 54 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/src/main.js: -------------------------------------------------------------------------------- 1 | // The Vue build version to load with the `import` command 2 | // (runtime-only or standalone) has been set in webpack.base.conf with an alias. 3 | import Vue from 'vue' 4 | import App from './App' 5 | import router from './router' 6 | 7 | Vue.config.productionTip = false 8 | 9 | /* eslint-disable no-new */ 10 | new Vue({ 11 | el: '#app', 12 | router, 13 | template: '', 14 | components: { App } 15 | }) 16 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/src/router/index.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import Router from 'vue-router' 3 | import HelloWorld from '@/components/HelloWorld' 4 | 5 | Vue.use(Router) 6 | 7 | export default new Router({ 8 | routes: [ 9 | { 10 | path: '/', 11 | name: 'Hello', 12 | component: HelloWorld 13 | } 14 | ] 15 | }) 16 | -------------------------------------------------------------------------------- /chapter3-7/front-vue/static/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter3-7/front-vue/static/.gitkeep -------------------------------------------------------------------------------- /chapter3-7/src/main/java/com/frank/chapter37/Chapter37Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter37; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter37Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter37Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter3-7/src/main/java/com/frank/chapter37/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter37.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | /** 8 | * @author jianweilin 9 | * @date 2017/11/12 10 | */ 11 | @Configuration 12 | public class WebMvcConfig extends WebMvcConfigurerAdapter { 13 | @Override 14 | public void addResourceHandlers(ResourceHandlerRegistry registry) { 15 | registry.addResourceHandler("/static/**") 16 | .addResourceLocations("classpath:/static/"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /chapter3-7/src/main/java/com/frank/chapter37/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter37.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | 7 | /** 8 | * @author jianweilin 9 | * @date 2017/11/12 10 | */ 11 | @Controller 12 | public class IndexController { 13 | @RequestMapping(value = "/index",method = RequestMethod.GET) 14 | public String index(){ 15 | return "index"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /chapter3-7/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8001 -------------------------------------------------------------------------------- /chapter3-7/src/main/resources/static/css/app.739f1ad65f043fab3efe7a450cd54da9.css: -------------------------------------------------------------------------------- 1 | #app{font-family:Avenir,Helvetica,Arial,sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-align:center;color:#2c3e50;margin-top:60px}h1[data-v-40211da4],h2[data-v-40211da4]{font-weight:400}ul[data-v-40211da4]{list-style-type:none;padding:0}li[data-v-40211da4]{display:inline-block;margin:0 10px}a[data-v-40211da4]{color:#42b983} -------------------------------------------------------------------------------- /chapter3-7/src/main/resources/static/js/manifest.ff985a587e3fe0e8abca.js: -------------------------------------------------------------------------------- 1 | !function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;window.webpackJsonp=function(t,a,c){for(var i,u,f,s=0,l=[];sfront-vue
-------------------------------------------------------------------------------- /chapter3-7/src/test/java/com/frank/chapter37/Chapter37ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter37; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter37ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter4-1/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-1/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-1/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-1/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-1/src/main/java/com/frank/chapter41/Chapter41Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter41; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter41Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter41Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-1/src/main/java/com/frank/chapter41/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter41.service; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/11. 5 | */ 6 | public interface UserService { 7 | 8 | void create(String name, Integer age); 9 | 10 | void deleteByName(String name); 11 | 12 | Integer getAllUsers(); 13 | 14 | void deleteAllUsers(); 15 | } 16 | -------------------------------------------------------------------------------- /chapter4-1/src/main/java/com/frank/chapter41/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter41.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.jdbc.core.JdbcTemplate; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/11. 9 | */ 10 | @Service 11 | public class UserServiceImpl implements UserService { 12 | 13 | @Autowired 14 | private JdbcTemplate jdbcTemplate; 15 | 16 | @Override 17 | public void create(String name, Integer age) { 18 | jdbcTemplate.update("insert into bd_charge_info(name,age) values(?,?)",name,age); 19 | } 20 | 21 | @Override 22 | public void deleteByName(String name) { 23 | jdbcTemplate.update("DELETE FROM bd_charge_info where name = ?",name); 24 | } 25 | 26 | @Override 27 | public Integer getAllUsers() { 28 | return jdbcTemplate.queryForObject("select count(1) from bd_charge_info",Integer.class); 29 | } 30 | 31 | @Override 32 | public void deleteAllUsers() { 33 | jdbcTemplate.update("delete from bd_charge_info"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /chapter4-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/test 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /chapter4-1/src/test/java/com/frank/chapter41/Chapter41ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter41; 2 | 3 | import com.frank.chapter41.service.UserService; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.test.context.junit4.SpringRunner; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest 15 | public class Chapter41ApplicationTests { 16 | private static final Logger logger = LoggerFactory.getLogger(Chapter41ApplicationTests.class); 17 | @Autowired 18 | private UserService userService; 19 | 20 | @Before 21 | public void setUp(){ 22 | userService.deleteAllUsers(); 23 | } 24 | 25 | @Test 26 | public void test_001(){ 27 | userService.create("a",11); 28 | userService.create("b",21); 29 | userService.create("c",31); 30 | 31 | logger.info("user size => " + userService.getAllUsers()); 32 | userService.deleteByName("a"); 33 | logger.info("user size => " + userService.getAllUsers()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /chapter4-10/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-10/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-10/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-10/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-10/data/elasticsearch/nodes/0/_state/global-0.st: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-10/data/elasticsearch/nodes/0/_state/global-0.st -------------------------------------------------------------------------------- /chapter4-10/data/elasticsearch/nodes/0/node.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-10/data/elasticsearch/nodes/0/node.lock -------------------------------------------------------------------------------- /chapter4-10/src/main/java/com/frank/chapter410/Chapter410Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter410; 2 | 3 | import com.frank.chapter410.domain.User; 4 | import com.frank.chapter410.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | @SpringBootApplication 11 | public class Chapter410Application implements CommandLineRunner { 12 | 13 | 14 | @Autowired 15 | private UserService userService; 16 | 17 | @Override 18 | public void run(String... args) throws Exception { 19 | // System.out.println("<<<<<< Elasticsearch 参数配置开始 >>>>>>>"); 20 | // Map parameters = elasticsearchOperations.getClient().settings().getAsMap(); 21 | // parameters.entrySet().forEach(entry -> { 22 | // System.out.println(entry.getKey() + " = " + entry.getValue()); 23 | // }); 24 | // System.out.println(">>>>>> Elasticsearch 参数配置结束 <<<<<<<<\n\n"); 25 | 26 | System.out.println("<<<<<< Elasticsearch 初始化数据开始 >>>>>>>"); 27 | userService.save(new User(1L,"张三","18817394122")); 28 | userService.save(new User(2L,"李四","18817394132")); 29 | userService.save(new User(3L,"王二麻子","18817394142")); 30 | userService.save(new User(4L,"赵六子","18817394152")); 31 | System.out.println("<<<<<< Elasticsearch 初始化数据结束 >>>>>>>\n\n"); 32 | } 33 | 34 | public static void main(String[] args) { 35 | SpringApplication.run(Chapter410Application.class, args); 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /chapter4-10/src/main/java/com/frank/chapter410/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter410.domain; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.elasticsearch.annotations.Document; 5 | 6 | /** 7 | * @author jianweilin 8 | * @date 2017/10/24 9 | */ 10 | @Document(indexName = "frank", type = "user") 11 | public class User { 12 | @Id 13 | private Long id; 14 | 15 | private String userName; 16 | 17 | private String userPhone; 18 | 19 | public User() { 20 | } 21 | 22 | public User(Long id, String userName, String userPhone) { 23 | this.id = id; 24 | this.userName = userName; 25 | this.userPhone = userPhone; 26 | } 27 | 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Long id) { 33 | this.id = id; 34 | } 35 | 36 | public String getUserName() { 37 | return userName; 38 | } 39 | 40 | public void setUserName(String userName) { 41 | this.userName = userName; 42 | } 43 | 44 | public String getUserPhone() { 45 | return userPhone; 46 | } 47 | 48 | public void setUserPhone(String userPhone) { 49 | this.userPhone = userPhone; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "User{" + 55 | "id=" + id + 56 | ", userName='" + userName + '\'' + 57 | ", userPhone='" + userPhone + '\'' + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /chapter4-10/src/main/java/com/frank/chapter410/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter410.repository; 2 | 3 | 4 | import com.frank.chapter410.domain.User; 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author jianweilin 13 | * @date 2017/10/24 14 | */ 15 | public interface UserRepository extends ElasticsearchRepository { 16 | /** 17 | * 根据用户名,获取用户分页列表 18 | * @param userName 用户名 19 | * @param pageable 分页模型 20 | * @return 分页用户列表 21 | */ 22 | Page findByUserName(String userName, Pageable pageable); 23 | 24 | /** 25 | * 根据用户手机号,获取用户列表 26 | * @param userPhone 用户手机 27 | * @return 用户列表 28 | */ 29 | List findByUserPhone(String userPhone); 30 | } 31 | -------------------------------------------------------------------------------- /chapter4-10/src/main/java/com/frank/chapter410/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter410.service; 2 | 3 | import com.frank.chapter410.domain.User; 4 | import org.springframework.data.domain.Page; 5 | import org.springframework.data.domain.PageRequest; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author jianweilin 11 | * @date 2017/10/24 12 | */ 13 | public interface UserService { 14 | User save(User user); 15 | 16 | void deleteByUserId(Long userId); 17 | 18 | User findByUserId(Long userId); 19 | 20 | Iterable findAll(); 21 | 22 | Page findByUserName(String userName, PageRequest pageRequest); 23 | 24 | List findByUserPhone(String userPhone); 25 | } 26 | -------------------------------------------------------------------------------- /chapter4-10/src/main/java/com/frank/chapter410/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter410.service; 2 | 3 | import com.frank.chapter410.domain.User; 4 | import com.frank.chapter410.repository.UserRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.domain.Page; 7 | import org.springframework.data.domain.PageRequest; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @author jianweilin 14 | * @date 2017/10/24 15 | */ 16 | @Service 17 | public class UserServiceImpl implements UserService { 18 | 19 | @Autowired 20 | private UserRepository userRepository; 21 | 22 | @Override 23 | public User save(User user) { 24 | return userRepository.save(user); 25 | } 26 | 27 | @Override 28 | public void deleteByUserId(Long userId) { 29 | userRepository.delete(userId); 30 | } 31 | 32 | @Override 33 | public User findByUserId(Long userId) { 34 | return userRepository.findOne(userId); 35 | } 36 | 37 | @Override 38 | public Iterable findAll() { 39 | return userRepository.findAll(); 40 | } 41 | 42 | @Override 43 | public Page findByUserName(String userName, PageRequest pageRequest) { 44 | return userRepository.findByUserName(userName,pageRequest); 45 | } 46 | 47 | @Override 48 | public List findByUserPhone(String userPhone) { 49 | return userRepository.findByUserPhone(userPhone); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter4-10/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | elasticsearch.clustername=frank_cluster 2 | elasticsearch.host=localhost 3 | # 9100 - 9200 Http -> Http传输; 9300 - 9400 Node -> Node传输 4 | elasticsearch.port=9300 -------------------------------------------------------------------------------- /chapter4-11/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-11/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-11/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-11/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-11/src/main/java/com/frank/chapter411/Chapter411Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter411; 2 | 3 | import com.frank.chapter411.domain.User; 4 | import com.frank.chapter411.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | @SpringBootApplication 11 | public class Chapter411Application implements CommandLineRunner { 12 | @Autowired 13 | private UserService userService; 14 | 15 | @Override 16 | public void run(String... args) throws Exception { 17 | System.out.println("<<<<<< Elasticsearch 初始化数据开始 >>>>>>>"); 18 | userService.save(new User(1L,"张三","18817394122")); 19 | userService.save(new User(2L,"李四","18817394132")); 20 | userService.save(new User(3L,"王二麻子","18817394142")); 21 | userService.save(new User(4L,"赵六子","18817394152")); 22 | System.out.println("<<<<<< Elasticsearch 初始化数据结束 >>>>>>>\n\n"); 23 | } 24 | 25 | public static void main(String[] args) { 26 | SpringApplication.run(Chapter411Application.class, args); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /chapter4-11/src/main/java/com/frank/chapter411/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter411.domain; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.elasticsearch.annotations.Document; 5 | 6 | /** 7 | * @author jianweilin 8 | * @date 2017/10/24 9 | */ 10 | @Document(indexName = "frank", type = "user") 11 | public class User { 12 | @Id 13 | private Long id; 14 | 15 | private String userName; 16 | 17 | private String userPhone; 18 | 19 | public User() { 20 | } 21 | 22 | public User(Long id, String userName, String userPhone) { 23 | this.id = id; 24 | this.userName = userName; 25 | this.userPhone = userPhone; 26 | } 27 | 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Long id) { 33 | this.id = id; 34 | } 35 | 36 | public String getUserName() { 37 | return userName; 38 | } 39 | 40 | public void setUserName(String userName) { 41 | this.userName = userName; 42 | } 43 | 44 | public String getUserPhone() { 45 | return userPhone; 46 | } 47 | 48 | public void setUserPhone(String userPhone) { 49 | this.userPhone = userPhone; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "User{" + 55 | "id=" + id + 56 | ", userName='" + userName + '\'' + 57 | ", userPhone='" + userPhone + '\'' + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /chapter4-11/src/main/java/com/frank/chapter411/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter411.repository; 2 | 3 | 4 | import com.frank.chapter411.domain.User; 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author jianweilin 13 | * @date 2017/10/24 14 | */ 15 | public interface UserRepository extends ElasticsearchRepository { 16 | /** 17 | * 根据用户名,获取用户分页列表 18 | * @param userName 用户名 19 | * @param pageable 分页模型 20 | * @return 分页用户列表 21 | */ 22 | Page findByUserName(String userName, Pageable pageable); 23 | 24 | /** 25 | * 根据用户手机号,获取用户列表 26 | * @param userPhone 用户手机 27 | * @return 用户列表 28 | */ 29 | List findByUserPhone(String userPhone); 30 | } 31 | -------------------------------------------------------------------------------- /chapter4-11/src/main/java/com/frank/chapter411/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter411.service; 2 | 3 | import com.frank.chapter411.domain.User; 4 | import org.springframework.data.domain.Page; 5 | import org.springframework.data.domain.PageRequest; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author jianweilin 11 | * @date 2017/10/24 12 | */ 13 | public interface UserService { 14 | User save(User user); 15 | 16 | void deleteByUserId(Long userId); 17 | 18 | User findByUserId(Long userId); 19 | 20 | Iterable findAll(); 21 | 22 | Page findByUserName(String userName, PageRequest pageRequest); 23 | 24 | List findByUserPhone(String userPhone); 25 | } 26 | -------------------------------------------------------------------------------- /chapter4-11/src/main/java/com/frank/chapter411/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter411.service; 2 | 3 | import com.frank.chapter411.domain.User; 4 | import com.frank.chapter411.repository.UserRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.domain.Page; 7 | import org.springframework.data.domain.PageRequest; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @author jianweilin 14 | * @date 2017/10/24 15 | */ 16 | @Service 17 | public class UserServiceImpl implements UserService { 18 | 19 | @Autowired 20 | private UserRepository userRepository; 21 | 22 | @Override 23 | public User save(User user) { 24 | return userRepository.save(user); 25 | } 26 | 27 | @Override 28 | public void deleteByUserId(Long userId) { 29 | userRepository.delete(userId); 30 | } 31 | 32 | @Override 33 | public User findByUserId(Long userId) { 34 | return userRepository.findOne(userId); 35 | } 36 | 37 | @Override 38 | public Iterable findAll() { 39 | return userRepository.findAll(); 40 | } 41 | 42 | @Override 43 | public Page findByUserName(String userName, PageRequest pageRequest) { 44 | return userRepository.findByUserName(userName,pageRequest); 45 | } 46 | 47 | @Override 48 | public List findByUserPhone(String userPhone) { 49 | return userRepository.findByUserPhone(userPhone); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /chapter4-11/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.elasticsearch.cluster-name=frank_cluster 2 | spring.data.elasticsearch.cluster-nodes=localhost:9300 -------------------------------------------------------------------------------- /chapter4-12/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-12/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-12/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-12/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/Chapter412Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter412Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter412Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/codegen/SimpleConfGenerator.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412.codegen; 2 | 3 | import org.jooq.util.jaxb.Generator; 4 | 5 | /** 6 | * Created by HYL on 2016/11/11. 7 | */ 8 | public class SimpleConfGenerator extends Generator { 9 | 10 | @Override 11 | public String getName() { 12 | return "com.frank.chapter412.codegen.SimpleJavaGenerator"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/config/SpringTransaction.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412.config; 2 | 3 | import org.jooq.Transaction; 4 | import org.springframework.transaction.TransactionStatus; 5 | 6 | /** 7 | * Adapts a Spring transaction for JOOQ. 8 | * 9 | * @author Lukas Eder 10 | * @author Andreas Ahlenstorf 11 | * @author Phillip Webb 12 | */ 13 | public class SpringTransaction implements Transaction { 14 | 15 | // Based on the jOOQ-spring-example from https://github.com/jOOQ/jOOQ 16 | 17 | private final TransactionStatus transactionStatus; 18 | 19 | SpringTransaction(TransactionStatus transactionStatus) { 20 | this.transactionStatus = transactionStatus; 21 | } 22 | 23 | public TransactionStatus getTxStatus() { 24 | return this.transactionStatus; 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/config/SpringTransactionProvider.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412.config; 2 | 3 | import org.jooq.TransactionContext; 4 | import org.jooq.TransactionProvider; 5 | import org.springframework.transaction.PlatformTransactionManager; 6 | import org.springframework.transaction.TransactionDefinition; 7 | import org.springframework.transaction.TransactionStatus; 8 | import org.springframework.transaction.support.DefaultTransactionDefinition; 9 | 10 | public class SpringTransactionProvider implements TransactionProvider { 11 | 12 | // Based on the jOOQ-spring-example from https://github.com/jOOQ/jOOQ 13 | 14 | private final PlatformTransactionManager transactionManager; 15 | 16 | public SpringTransactionProvider(PlatformTransactionManager transactionManager) { 17 | this.transactionManager = transactionManager; 18 | } 19 | 20 | @Override 21 | public void begin(TransactionContext context) { 22 | TransactionDefinition definition = new DefaultTransactionDefinition( 23 | TransactionDefinition.PROPAGATION_NESTED); 24 | TransactionStatus status = this.transactionManager.getTransaction(definition); 25 | context.transaction(new SpringTransaction(status)); 26 | } 27 | 28 | @Override 29 | public void commit(TransactionContext ctx) { 30 | this.transactionManager.commit(getTransactionStatus(ctx)); 31 | } 32 | 33 | @Override 34 | public void rollback(TransactionContext ctx) { 35 | this.transactionManager.rollback(getTransactionStatus(ctx)); 36 | } 37 | 38 | private TransactionStatus getTransactionStatus(TransactionContext ctx) { 39 | SpringTransaction transaction = (SpringTransaction) ctx.transaction(); 40 | return transaction.getTxStatus(); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/constant/Constants.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412.constant; 2 | 3 | /** 4 | * @author jianweilin 5 | * @date 2017/11/6 6 | */ 7 | public class Constants { 8 | public static final String BD_USE_READ_DB = "dslContextBdUseReadDb"; 9 | public static final String BD_USE_WRITE_DB = "dslContextBdUseWriteDb"; 10 | 11 | public static final String DADA_READ_DB = "dslContextDadaReadDb"; 12 | public static final String DADA_WRITE_DB = "dslContextDadaWriteDb"; 13 | } 14 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/entity/test/DefaultCatalog.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by jOOQ 3 | */ 4 | package com.frank.chapter412.entity.test; 5 | 6 | 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | import java.util.List; 10 | 11 | import javax.annotation.Generated; 12 | 13 | import org.jooq.Schema; 14 | import org.jooq.impl.CatalogImpl; 15 | 16 | 17 | /** 18 | * This class is generated by jOOQ. 19 | */ 20 | @Generated( 21 | value = { 22 | "http://www.jooq.org", 23 | "jOOQ version:3.8.4" 24 | }, 25 | comments = "This class is generated by jOOQ" 26 | ) 27 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 28 | public class DefaultCatalog extends CatalogImpl { 29 | 30 | private static final long serialVersionUID = 307308603; 31 | 32 | /** 33 | * The reference instance of 34 | */ 35 | public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog(); 36 | 37 | /** 38 | * The schema test. 39 | */ 40 | public final Test TEST = com.frank.chapter412.entity.test.Test.TEST; 41 | 42 | /** 43 | * No further instances allowed 44 | */ 45 | private DefaultCatalog() { 46 | super(""); 47 | } 48 | 49 | @Override 50 | public final List getSchemas() { 51 | List result = new ArrayList(); 52 | result.addAll(getSchemas0()); 53 | return result; 54 | } 55 | 56 | private final List getSchemas0() { 57 | return Arrays.asList( 58 | Test.TEST); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/entity/test/Tables.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by jOOQ 3 | */ 4 | package com.frank.chapter412.entity.test; 5 | 6 | 7 | import com.frank.chapter412.entity.test.tables.User; 8 | 9 | import javax.annotation.Generated; 10 | 11 | 12 | /** 13 | * Convenience access to all tables in test 14 | */ 15 | @Generated( 16 | value = { 17 | "http://www.jooq.org", 18 | "jOOQ version:3.8.4" 19 | }, 20 | comments = "This class is generated by jOOQ" 21 | ) 22 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 23 | public class Tables { 24 | 25 | /** 26 | * The table test.user. 27 | */ 28 | public static final User USER = com.frank.chapter412.entity.test.tables.User.USER; 29 | } 30 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/entity/test/Test.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by jOOQ 3 | */ 4 | package com.frank.chapter412.entity.test; 5 | 6 | 7 | import com.frank.chapter412.entity.test.tables.User; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | import javax.annotation.Generated; 14 | 15 | import org.jooq.Catalog; 16 | import org.jooq.Table; 17 | import org.jooq.impl.SchemaImpl; 18 | 19 | 20 | /** 21 | * This class is generated by jOOQ. 22 | */ 23 | @Generated( 24 | value = { 25 | "http://www.jooq.org", 26 | "jOOQ version:3.8.4" 27 | }, 28 | comments = "This class is generated by jOOQ" 29 | ) 30 | @SuppressWarnings({ "all", "unchecked", "rawtypes" }) 31 | public class Test extends SchemaImpl { 32 | 33 | private static final long serialVersionUID = -1583381313; 34 | 35 | /** 36 | * The reference instance of test 37 | */ 38 | public static final Test TEST = new Test(); 39 | 40 | /** 41 | * The table test.user. 42 | */ 43 | public final User USER = com.frank.chapter412.entity.test.tables.User.USER; 44 | 45 | /** 46 | * No further instances allowed 47 | */ 48 | private Test() { 49 | super("test", null); 50 | } 51 | 52 | 53 | /** 54 | * {@inheritDoc} 55 | */ 56 | @Override 57 | public Catalog getCatalog() { 58 | return DefaultCatalog.DEFAULT_CATALOG; 59 | } 60 | 61 | @Override 62 | public final List> getTables() { 63 | List result = new ArrayList(); 64 | result.addAll(getTables0()); 65 | return result; 66 | } 67 | 68 | private final List> getTables0() { 69 | return Arrays.>asList( 70 | User.USER); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/repository/TestBaseRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by GenerationCodeUtil. 3 | */ 4 | package com.frank.chapter412.repository; 5 | 6 | import org.apache.commons.lang3.tuple.Pair; 7 | import org.jooq.DSLContext; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.beans.factory.annotation.Qualifier; 10 | 11 | import java.io.Serializable; 12 | 13 | import static com.frank.chapter412.constant.Constants.*; 14 | 15 | 16 | public abstract class TestBaseRepository extends BaseRepository { 17 | 18 | @Autowired 19 | @Qualifier(BD_USE_READ_DB) 20 | protected DSLContext dslRead; 21 | 22 | @Autowired 23 | @Qualifier(BD_USE_WRITE_DB) 24 | protected DSLContext dslWrite; 25 | 26 | @Override 27 | protected Pair dslSelect() { 28 | return Pair.of(dslRead, dslWrite); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/repository/test/UserRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by GenerationCodeUtil. 3 | */ 4 | package com.frank.chapter412.repository.test; 5 | 6 | import org.apache.commons.lang3.tuple.Pair; 7 | import org.jooq.Table; 8 | import org.jooq.UpdatableRecord; 9 | import org.springframework.stereotype.Repository; 10 | 11 | 12 | import com.frank.chapter412.entity.test.tables.records.UserRecord; 13 | import com.frank.chapter412.repository.TestBaseRepository; 14 | import static com.frank.chapter412.entity.test.Tables.USER; 15 | 16 | @Repository 17 | public class UserRepository extends TestBaseRepository { 18 | 19 | @Override 20 | protected Pair, Table> mapping() { 21 | return Pair.of(UserRecord.class, USER); 22 | } 23 | 24 | public void AddUser(UserRecord userRecord){ 25 | dslWrite.executeInsert(userRecord); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/service/test/UserService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This class is generated by GenerationCodeUtil. 3 | */ 4 | 5 | package com.frank.chapter412.service.test; 6 | import org.springframework.stereotype.Service; 7 | 8 | 9 | @Service 10 | public class UserService { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-12/src/main/java/com/frank/chapter412/utils/CamelUnderlineTransformUtils.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412.utils; 2 | 3 | import java.util.regex.Matcher; 4 | 5 | import static java.util.regex.Pattern.compile; 6 | 7 | public class CamelUnderlineTransformUtils { 8 | 9 | private static final char UNDERLINE = '_'; 10 | 11 | /** 12 | * 驼峰转下划线 13 | * @param str 14 | * @return 15 | */ 16 | public static String camelToUnderline(String str) { 17 | if (str == null || "".equals(str.trim())) { 18 | return ""; 19 | } 20 | int len = str.length(); 21 | StringBuilder sb = new StringBuilder(len); 22 | for (int i = 0; i < len; i++) { 23 | char c = str.charAt(i); 24 | if (Character.isUpperCase(c)) { 25 | sb.append(UNDERLINE); 26 | sb.append(Character.toLowerCase(c)); 27 | } else { 28 | sb.append(c); 29 | } 30 | } 31 | return sb.toString(); 32 | } 33 | 34 | /** 35 | * 下划线转驼峰 36 | * @param str 37 | * @return 38 | */ 39 | public static String underlineToCamel(String str) { 40 | if (str == null || "".equals(str.trim())) return ""; 41 | StringBuilder sb = new StringBuilder(str); 42 | Matcher mc = compile("_").matcher(str); 43 | int i = 0; 44 | while (mc.find()) { 45 | int position = mc.end() - (i++); 46 | sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase()); 47 | } 48 | return sb.toString(); 49 | } 50 | } -------------------------------------------------------------------------------- /chapter4-12/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # bd_use 读库 2 | db.bduse.read.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true 3 | db.bduse.read.username=root 4 | db.bduse.read.password=root 5 | db.bduse.read.initialSize=1 6 | db.bduse.read.minIdle=1 7 | db.bduse.read.maxActive=100 8 | db.bduse.read.filters=stat 9 | 10 | # bd_use 写库 11 | db.bduse.write.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true 12 | db.bduse.write.username=root 13 | db.bduse.write.password=root 14 | db.bduse.write.initialSize=1 15 | db.bduse.write.minIdle=1 16 | db.bduse.write.maxActive=100 17 | db.bduse.write.filters=stat 18 | 19 | # dada 读库 20 | db.dada.read.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true 21 | db.dada.read.username=root 22 | db.dada.read.password=root 23 | db.dada.read.initialSize=1 24 | db.dada.read.minIdle=1 25 | db.dada.read.maxActive=100 26 | db.dada.read.filters=stat 27 | 28 | # dada 写库 29 | db.dada.write.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true 30 | db.dada.write.username=root 31 | db.dada.write.password=root 32 | db.dada.write.initialSize=1 33 | db.dada.write.minIdle=1 34 | db.dada.write.maxActive=100 35 | db.dada.write.filters=stat -------------------------------------------------------------------------------- /chapter4-12/src/main/resources/test_2017-11-06.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-12/src/main/resources/test_2017-11-06.sql -------------------------------------------------------------------------------- /chapter4-12/src/test/java/com/frank/chapter412/Chapter412ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter412; 2 | 3 | import com.frank.chapter412.entity.test.tables.records.UserRecord; 4 | import com.frank.chapter412.repository.test.UserRepository; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class Chapter412ApplicationTests { 14 | 15 | @Autowired 16 | private UserRepository userRepository; 17 | 18 | @Test 19 | public void test_001(){ 20 | UserRecord userRecord = new UserRecord(); 21 | userRecord.setName("张三"); 22 | userRecord.setAge(23); 23 | userRepository.AddUser(userRecord); 24 | System.out.println("新增用户成功!"); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /chapter4-13/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /chapter4-13/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-13/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-13/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-13/src/main/java/com/frank/chapter413/Chapter413Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter413; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.mybatis.spring.annotation.MapperScan; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | @MapperScan("com.frank.chapter413") 9 | @SpringBootApplication 10 | @Slf4j 11 | public class Chapter413Application{ 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(Chapter413Application.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /chapter4-13/src/main/java/com/frank/chapter413/controller/CityController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter413.controller; 2 | 3 | import com.frank.chapter413.domain.City; 4 | import com.frank.chapter413.mapper.CityMapper; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author jianweilin 13 | * @date 2018/9/12 14 | */ 15 | @RestController 16 | public class CityController { 17 | 18 | @Autowired 19 | private CityMapper cityMapper; 20 | 21 | @RequestMapping("/getCitys") 22 | public List getCitys(){ 23 | return cityMapper.getAll(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /chapter4-13/src/main/java/com/frank/chapter413/domain/City.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter413.domain; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @author jianweilin 7 | * @date 2018/9/12 8 | */ 9 | @Data 10 | public class City { 11 | private Long id; 12 | private String cityName; 13 | private String state; 14 | private String country; 15 | } 16 | -------------------------------------------------------------------------------- /chapter4-13/src/main/java/com/frank/chapter413/mapper/CityMapper.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter413.mapper; 2 | 3 | import com.frank.chapter413.domain.City; 4 | import org.apache.ibatis.annotations.*; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author jianweilin 10 | * @date 2018/9/12 11 | */ 12 | public interface CityMapper { 13 | @Select("SELECT city_name, country, state FROM city") 14 | @Results({@Result(property = "cityName", column = "city_name")}) 15 | List getAll(); 16 | 17 | @Select("SELECT * FROM city where id = #{id}") 18 | @Results({@Result(property = "cityName", column = "city_name"), 19 | @Result(property = "country", column = "country")}) 20 | City getOne(Long id); 21 | 22 | @Insert("INSERT INTO city(city_name,state,country) VALUES(#{cityName}, #{state}, #{country})") 23 | void insert(City city); 24 | 25 | @Update("UPDATE city set city_name=${cityName} where id #{id}") 26 | void update(City city); 27 | 28 | @Delete("DELETE FROM city where id = #{id}") 29 | void delete(Long id); 30 | } 31 | -------------------------------------------------------------------------------- /chapter4-13/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | mybatis.type-aliases-package=com.frank.chapter413 2 | 3 | spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 4 | spring.datasource.username=root 5 | spring.datasource.password=root -------------------------------------------------------------------------------- /chapter4-13/src/test/java/com/frank/chapter413/Chapter413ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter413; 2 | 3 | import org.junit.ClassRule; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.boot.test.rule.OutputCapture; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | @RunWith(SpringRunner.class) 11 | @SpringBootTest 12 | public class Chapter413ApplicationTests { 13 | @ClassRule 14 | public static OutputCapture out = new OutputCapture(); 15 | 16 | @Test 17 | public void test_001(){ 18 | String output = out.toString(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /chapter4-2/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-2/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-2/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-2/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-2/src/main/java/com/frank/chapter42/Chapter42Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter42; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter42Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter42Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-2/src/main/java/com/frank/chapter42/dao/BdChargeInfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter42.dao; 2 | 3 | import com.frank.chapter42.domain.BdChargeInfo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.data.repository.query.Param; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/11. 10 | */ 11 | public interface BdChargeInfoRepository extends JpaRepository { 12 | BdChargeInfo findByName(String name); 13 | 14 | BdChargeInfo findByNameAndAge(String name,Integer age); 15 | 16 | @Query("select b from BdChargeInfo b where b.name=:name") 17 | BdChargeInfo findUser(@Param("name") String name); 18 | } 19 | -------------------------------------------------------------------------------- /chapter4-2/src/main/java/com/frank/chapter42/domain/BdChargeInfo.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter42.domain; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/11. 10 | */ 11 | @Entity 12 | public class BdChargeInfo{ 13 | 14 | @Id 15 | @GeneratedValue 16 | private Integer id; 17 | 18 | @Column(nullable = false) 19 | private String name; 20 | 21 | @Column(nullable = false) 22 | private Integer age; 23 | 24 | public BdChargeInfo(String name, Integer age) { 25 | this.name = name; 26 | this.age = age; 27 | } 28 | 29 | public BdChargeInfo() { 30 | } 31 | 32 | public Integer getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Integer id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public Integer getAge() { 49 | return age; 50 | } 51 | 52 | public void setAge(Integer age) { 53 | this.age = age; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /chapter4-2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/test 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 5 | 6 | # 自动创建 & 移除表 7 | spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop -------------------------------------------------------------------------------- /chapter4-2/src/test/java/com/frank/chapter42/Chapter42ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter42; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.frank.chapter42.dao.BdChargeInfoRepository; 5 | import com.frank.chapter42.domain.BdChargeInfo; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | 14 | @RunWith(SpringRunner.class) 15 | @SpringBootTest 16 | public class Chapter42ApplicationTests { 17 | private static final Logger logger = LoggerFactory.getLogger(Chapter42ApplicationTests.class); 18 | @Autowired 19 | private BdChargeInfoRepository bdChargeInfoRepository; 20 | 21 | @Test 22 | public void test_001(){ 23 | bdChargeInfoRepository.save(new BdChargeInfo("AA",10)); 24 | bdChargeInfoRepository.save(new BdChargeInfo("BB",20)); 25 | 26 | logger.info("all size => " + bdChargeInfoRepository.findAll().size()); 27 | logger.info("findbyName => " + JSON.toJSONString(bdChargeInfoRepository.findByName("AA"))); 28 | logger.info("findByNameAndAge => " + JSON.toJSONString(bdChargeInfoRepository.findByNameAndAge("BB",20))); 29 | logger.info("findUser => " + JSON.toJSONString(bdChargeInfoRepository.findUser("AA"))); 30 | 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter4-3/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-3/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-3/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-3/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-3/src/main/java/com/frank/chapter43/Chapter43Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter43; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter43Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter43Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-3/src/main/java/com/frank/chapter43/config/DataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter43.config; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.Primary; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | 11 | import javax.sql.DataSource; 12 | 13 | /** 14 | * Created by jianweilin on 2017/10/12. 15 | */ 16 | @Configuration 17 | public class DataSourceConfig { 18 | @Bean(name = "primaryDataSource") 19 | @Qualifier("primaryDataSource") 20 | @ConfigurationProperties(prefix = "spring.datasource.primary") 21 | public DataSource primaryDataSource(){ 22 | return DataSourceBuilder.create().build(); 23 | } 24 | 25 | @Bean(name = "secondaryDataSource") 26 | @Qualifier("secondaryDataSource") 27 | @Primary 28 | @ConfigurationProperties(prefix = "spring.datasource.secondary") 29 | public DataSource secondaryDataSource(){ 30 | return DataSourceBuilder.create().build(); 31 | } 32 | 33 | @Bean(name = "primaryJdbcTemplate") 34 | public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource){ 35 | return new JdbcTemplate(dataSource); 36 | } 37 | 38 | @Bean(name = "secondaryJdbcTemplate") 39 | public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource){ 40 | return new JdbcTemplate(dataSource); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter4-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1 2 | spring.datasource.primary.username=root 3 | spring.datasource.primary.password=root 4 | spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver 5 | 6 | spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2 7 | spring.datasource.secondary.username=root 8 | spring.datasource.secondary.password=root 9 | spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /chapter4-3/src/test/java/com/frank/chapter43/Chapter43ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter43; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class Chapter43ApplicationTests { 15 | 16 | @Autowired 17 | @Qualifier("primaryJdbcTemplate") 18 | private JdbcTemplate jdbcTemplate1; 19 | 20 | @Autowired 21 | @Qualifier("secondaryJdbcTemplate") 22 | private JdbcTemplate jdbcTemplate2; 23 | 24 | @Before 25 | public void setUp(){ 26 | jdbcTemplate1.update("DELETE FROM USER "); 27 | jdbcTemplate2.update("DELETE FROM USER "); 28 | } 29 | 30 | @Test 31 | public void test() throws Exception { 32 | jdbcTemplate1.update("INSERT INTO user(name,age) values(?,?)","AA",23); 33 | jdbcTemplate1.update("INSERT INTO user(name,age) values(?,?)","BB",24); 34 | jdbcTemplate2.update("INSERT INTO user(name,age) values(?,?)","CC",25); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /chapter4-4/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-4/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-4/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-4/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-4/src/main/java/com/frank/chapter44/Chapter44Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter44Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter44Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-4/src/main/java/com/frank/chapter44/DataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.Primary; 9 | 10 | import javax.sql.DataSource; 11 | 12 | /** 13 | * Created by jianweilin on 2017/10/12. 14 | */ 15 | @Configuration 16 | public class DataSourceConfig { 17 | 18 | @Bean(name = "primaryDataSource") 19 | @Qualifier("primaryDataSource") 20 | @ConfigurationProperties(prefix = "spring.datasource.primary") 21 | public DataSource primaryDataSource(){ 22 | return DataSourceBuilder.create().build(); 23 | } 24 | 25 | @Bean(name = "secondaryDataSource") 26 | @Qualifier("secondaryDataSource") 27 | @Primary 28 | @ConfigurationProperties(prefix = "spring.datasource.secondary") 29 | public DataSource secondaryDataSource(){ 30 | return DataSourceBuilder.create().build(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /chapter4-4/src/main/java/com/frank/chapter44/domain/p/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44.domain.p; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/12. 10 | */ 11 | @Entity 12 | public class User { 13 | @Id 14 | @GeneratedValue 15 | private Long id; 16 | 17 | @Column(nullable = false) 18 | private String name; 19 | 20 | @Column(nullable = false) 21 | private Integer age; 22 | 23 | public User(String name, Integer age) { 24 | this.name = name; 25 | this.age = age; 26 | } 27 | 28 | public User() { 29 | } 30 | 31 | public Long getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Long id) { 36 | this.id = id; 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | public void setName(String name) { 44 | this.name = name; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /chapter4-4/src/main/java/com/frank/chapter44/domain/p/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44.domain.p; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | /** 6 | * Created by jianweilin on 2017/10/12. 7 | */ 8 | public interface UserRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /chapter4-4/src/main/java/com/frank/chapter44/domain/s/Message.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44.domain.s; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/12. 10 | */ 11 | @Entity 12 | public class Message { 13 | 14 | @Id 15 | @GeneratedValue 16 | private Long id; 17 | 18 | @Column(nullable = false) 19 | private String name; 20 | 21 | @Column(nullable = false) 22 | private String content; 23 | 24 | public Message() { 25 | } 26 | 27 | public Message(String name, String content) { 28 | this.name = name; 29 | this.content = content; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Long id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public String getContent() { 49 | return content; 50 | } 51 | 52 | public void setContent(String content) { 53 | this.content = content; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /chapter4-4/src/main/java/com/frank/chapter44/domain/s/MessageRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44.domain.s; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | /** 6 | * Created by jianweilin on 2017/10/12. 7 | */ 8 | public interface MessageRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /chapter4-4/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1 2 | spring.datasource.primary.username=root 3 | spring.datasource.primary.password=root 4 | spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver 5 | 6 | spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2 7 | spring.datasource.secondary.username=root 8 | spring.datasource.secondary.password=root 9 | spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver 10 | 11 | spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop -------------------------------------------------------------------------------- /chapter4-4/src/test/java/com/frank/chapter44/Chapter44ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter44; 2 | 3 | import com.frank.chapter44.domain.p.User; 4 | import com.frank.chapter44.domain.p.UserRepository; 5 | import com.frank.chapter44.domain.s.Message; 6 | import com.frank.chapter44.domain.s.MessageRepository; 7 | import org.junit.Assert; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.boot.test.context.SpringBootTest; 13 | import org.springframework.test.context.junit4.SpringRunner; 14 | 15 | @RunWith(SpringRunner.class) 16 | @SpringBootTest 17 | public class Chapter44ApplicationTests { 18 | 19 | 20 | @Autowired 21 | private UserRepository userRepository; 22 | @Autowired 23 | private MessageRepository messageRepository; 24 | 25 | @Before 26 | public void setUp() { 27 | } 28 | 29 | @Test 30 | public void test() throws Exception { 31 | 32 | userRepository.save(new User("aaa", 10)); 33 | userRepository.save(new User("bbb", 20)); 34 | userRepository.save(new User("ccc", 30)); 35 | userRepository.save(new User("ddd", 40)); 36 | userRepository.save(new User("eee", 50)); 37 | 38 | Assert.assertEquals(5, userRepository.findAll().size()); 39 | 40 | messageRepository.save(new Message("o1", "aaaaaaaaaa")); 41 | messageRepository.save(new Message("o2", "bbbbbbbbbb")); 42 | messageRepository.save(new Message("o3", "cccccccccc")); 43 | 44 | Assert.assertEquals(3, messageRepository.findAll().size()); 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /chapter4-5/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-5/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-5/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-5/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-5/src/main/java/com/frank/chapter45/Chapter45Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter45; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter45Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter45Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-5/src/main/java/com/frank/chapter45/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter45; 2 | 3 | import com.frank.chapter45.domain.User; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.data.redis.connection.RedisConnectionFactory; 7 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 8 | import org.springframework.data.redis.core.RedisTemplate; 9 | import org.springframework.data.redis.serializer.StringRedisSerializer; 10 | 11 | /** 12 | * Created by jianweilin on 2017/10/14. 13 | */ 14 | @Configuration 15 | public class RedisConfig { 16 | @Bean 17 | JedisConnectionFactory jedisConnectionFactory(){ 18 | return new JedisConnectionFactory(); 19 | } 20 | 21 | @Bean 22 | public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { 23 | RedisTemplate template = new RedisTemplate(); 24 | template.setConnectionFactory(jedisConnectionFactory()); 25 | template.setKeySerializer(new StringRedisSerializer()); 26 | template.setValueSerializer(new RedisObjectSerializer()); 27 | return template; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /chapter4-5/src/main/java/com/frank/chapter45/RedisObjectSerializer.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter45; 2 | 3 | //import com.sun.tools.javac.util.Convert; 4 | 5 | import org.springframework.core.convert.converter.Converter; 6 | import org.springframework.core.serializer.support.DeserializingConverter; 7 | import org.springframework.core.serializer.support.SerializingConverter; 8 | import org.springframework.data.redis.serializer.RedisSerializer; 9 | import org.springframework.data.redis.serializer.SerializationException; 10 | 11 | 12 | /** 13 | * Created by jianweilin on 2017/10/14. 14 | */ 15 | public class RedisObjectSerializer implements RedisSerializer{ 16 | private Converter serilizer = new SerializingConverter(); 17 | private Converter deserializer= new DeserializingConverter(); 18 | 19 | static final byte[] EMPTY_ARRAY = new byte[0]; 20 | 21 | @Override 22 | public byte[] serialize(Object object) throws SerializationException { 23 | if(object == null) { 24 | return EMPTY_ARRAY; 25 | } 26 | 27 | try { 28 | return serilizer.convert(object); 29 | }catch (Exception ex){ 30 | return EMPTY_ARRAY; 31 | } 32 | } 33 | 34 | @Override 35 | public Object deserialize(byte[] bytes) throws SerializationException { 36 | if(isEmpty(bytes)) { 37 | return null; 38 | } 39 | 40 | try { 41 | return deserializer.convert(bytes); 42 | }catch (Exception ex){ 43 | throw new SerializationException("Can not deserialize",ex); 44 | } 45 | } 46 | 47 | private boolean isEmpty(byte[] data){ 48 | return (data == null || data.length == 0); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /chapter4-5/src/main/java/com/frank/chapter45/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter45.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by jianweilin on 2017/10/14. 7 | */ 8 | public class User implements Serializable{ 9 | private static final long serialVersionUID = -5720806027779615048L; 10 | private String userName; 11 | private Integer age; 12 | 13 | public User(String userName, Integer age) { 14 | this.userName = userName; 15 | this.age = age; 16 | } 17 | 18 | public User() { 19 | } 20 | 21 | public String getUserName() { 22 | return userName; 23 | } 24 | 25 | public void setUserName(String userName) { 26 | this.userName = userName; 27 | } 28 | 29 | public Integer getAge() { 30 | return age; 31 | } 32 | 33 | public void setAge(Integer age) { 34 | this.age = age; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /chapter4-5/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # REDIS (RedisProperties) 2 | # Redis数据库索引(默认为0) 3 | spring.redis.database=0 4 | # Redis服务器地址 5 | spring.redis.host=localhost 6 | # Redis服务器连接端口 7 | spring.redis.port=6379 8 | # Redis服务器连接密码(默认为空) 9 | spring.redis.password= 10 | # 连接池最大连接数(使用负值表示没有限制) 11 | spring.redis.pool.max-active=8 12 | # 连接池最大阻塞等待时间(使用负值表示没有限制) 13 | spring.redis.pool.max-wait=-1 14 | # 连接池中的最大空闲连接 15 | spring.redis.pool.max-idle=8 16 | # 连接池中的最小空闲连接 17 | spring.redis.pool.min-idle=0 18 | # 连接超时时间(毫秒) 19 | spring.redis.timeout=0 -------------------------------------------------------------------------------- /chapter4-5/src/test/java/com/frank/chapter45/Chapter45ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter45; 2 | 3 | import com.frank.chapter45.domain.User; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.data.redis.core.RedisTemplate; 10 | import org.springframework.data.redis.core.StringRedisTemplate; 11 | import org.springframework.test.context.junit4.SpringRunner; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest 15 | public class Chapter45ApplicationTests { 16 | 17 | @Autowired 18 | private StringRedisTemplate stringRedisTemplate; 19 | 20 | @Autowired 21 | private RedisTemplate redisTemplate; 22 | 23 | @Test 24 | public void test_001() { 25 | // 保存字符串 26 | stringRedisTemplate.opsForValue().set("aaaa","111"); 27 | Assert.assertEquals("111",stringRedisTemplate.opsForValue().get("aaaa")); 28 | 29 | // 保存对象 30 | User user = new User("超人",20); 31 | redisTemplate.opsForValue().set(user.getUserName(),user); 32 | 33 | user = new User("蝙蝠侠",30); 34 | redisTemplate.opsForValue().set(user.getUserName(),user); 35 | 36 | user = new User("蜘蛛侠",40); 37 | redisTemplate.opsForValue().set(user.getUserName(),user); 38 | 39 | Assert.assertEquals(20,redisTemplate.opsForValue().get("超人").getAge().longValue()); 40 | Assert.assertEquals(30,redisTemplate.opsForValue().get("蝙蝠侠").getAge().longValue()); 41 | Assert.assertEquals(40,redisTemplate.opsForValue().get("蜘蛛侠").getAge().longValue()); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /chapter4-6/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-6/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-6/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-6/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-6/src/main/java/com/frank/chapter46/Chapter46Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter46; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter46Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter46Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-6/src/main/java/com/frank/chapter46/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter46.domain; 2 | 3 | import org.springframework.data.annotation.Id; 4 | 5 | /** 6 | * Created by jianweilin on 2017/10/14. 7 | */ 8 | public class User { 9 | 10 | @Id 11 | private Long id; 12 | 13 | private String username; 14 | private Integer age; 15 | 16 | public User(Long id, String username, Integer age) { 17 | this.id = id; 18 | this.username = username; 19 | this.age = age; 20 | } 21 | 22 | public User() { 23 | } 24 | 25 | public Long getId() { 26 | return id; 27 | } 28 | 29 | public void setId(Long id) { 30 | this.id = id; 31 | } 32 | 33 | public String getUsername() { 34 | return username; 35 | } 36 | 37 | public void setUsername(String username) { 38 | this.username = username; 39 | } 40 | 41 | public Integer getAge() { 42 | return age; 43 | } 44 | 45 | public void setAge(Integer age) { 46 | this.age = age; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /chapter4-6/src/main/java/com/frank/chapter46/domain/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter46.domain; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | /** 6 | * Created by jianweilin on 2017/10/14. 7 | */ 8 | public interface UserRepository extends MongoRepository { 9 | User findByUsername(String username); 10 | } 11 | -------------------------------------------------------------------------------- /chapter4-6/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.uri=mongodb://localhost:27017/bd_use -------------------------------------------------------------------------------- /chapter4-6/src/test/java/com/frank/chapter46/Chapter46ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter46; 2 | 3 | import com.frank.chapter46.domain.User; 4 | import com.frank.chapter46.domain.UserRepository; 5 | import org.junit.Assert; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.test.context.junit4.SpringRunner; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest 15 | public class Chapter46ApplicationTests { 16 | 17 | @Autowired 18 | private UserRepository userRepository; 19 | 20 | @Before 21 | public void setUp(){ 22 | userRepository.deleteAll(); 23 | } 24 | 25 | 26 | @Test 27 | public void test_001(){ 28 | // 创建三个User,并验证User总数 29 | userRepository.save(new User(1L,"张三",30)); 30 | userRepository.save(new User(2L,"王武",40)); 31 | userRepository.save(new User(3L,"招商",50)); 32 | Assert.assertEquals(3,userRepository.findAll().size()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /chapter4-7/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-7/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-7/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-7/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-7/src/main/java/com/frank/chapter47/Chapter47Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter47; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter47Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter47Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-7/src/main/java/com/frank/chapter47/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter47.domain; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/14. 5 | */ 6 | public class User { 7 | private Long id; 8 | private String name; 9 | private Integer age; 10 | 11 | public User() { 12 | } 13 | 14 | public User(Long id, String name, Integer age) { 15 | this.id = id; 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public Long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public Integer getAge() { 37 | return age; 38 | } 39 | 40 | public void setAge(Integer age) { 41 | this.age = age; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /chapter4-7/src/main/java/com/frank/chapter47/domain/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter47.domain; 2 | 3 | import org.apache.ibatis.annotations.*; 4 | 5 | /** 6 | * Created by jianweilin on 2017/10/14. 7 | */ 8 | @Mapper 9 | public interface UserMapper { 10 | @Select("Select * FROM User where name = #{name}") 11 | User findByName(@Param("name") String name); 12 | 13 | @Insert("INSERT INTO user(name,age) VALUES(#{name},#{age})") 14 | Integer insert(@Param("name")String name,@Param("age") Integer age); 15 | 16 | @Update("UPDATE user set name=#{name} WHERE id=#{id}") 17 | Integer update(@Param("name")String name,@Param("id") Integer id); 18 | 19 | @Delete("delete from user where id=#{id}") 20 | Integer deleteById(@Param("id")Integer id); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /chapter4-7/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/test 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /chapter4-7/src/test/java/com/frank/chapter47/Chapter47ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter47; 2 | 3 | import com.frank.chapter47.domain.User; 4 | import com.frank.chapter47.domain.UserMapper; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class Chapter47ApplicationTests { 15 | 16 | @Autowired 17 | private UserMapper userMapper; 18 | 19 | @Test 20 | // @Rollback 21 | public void test_001(){ 22 | userMapper.insert("AAA",20); 23 | User u = userMapper.findByName("AAA"); 24 | Assert.assertEquals(20,u.getAge().intValue()); 25 | } 26 | 27 | @Test 28 | public void test_002(){ 29 | userMapper.update("BBBB",1); 30 | } 31 | 32 | @Test 33 | public void test_003(){ 34 | userMapper.deleteById(1); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /chapter4-8/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-8/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-8/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-8/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-8/src/main/java/com/frank/chapter48/Chapter48Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter48; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter48Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter48Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-8/src/main/java/com/frank/chapter48/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter48.domain; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/14. 5 | */ 6 | public class User { 7 | private Long id; 8 | private String name; 9 | private Integer age; 10 | 11 | public User() { 12 | } 13 | 14 | public User(Long id, String name, Integer age) { 15 | this.id = id; 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public Long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public Integer getAge() { 37 | return age; 38 | } 39 | 40 | public void setAge(Integer age) { 41 | this.age = age; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /chapter4-8/src/main/java/com/frank/chapter48/domain/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter48.domain; 2 | 3 | import org.apache.ibatis.annotations.*; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/14. 10 | */ 11 | @Mapper 12 | public interface UserMapper { 13 | @Select("SELECT * FROM user where name= #{name}") 14 | User findByName(@Param("name") String name); 15 | 16 | @Results({ 17 | @Result(property = "name",column = "name"), 18 | @Result(property = "age",column = "age") 19 | }) 20 | @Select("SELECT name,age from user") 21 | List findAll(); 22 | 23 | @Insert("INSERT INTO user(name,age) VALUES(#{name},#{age})") 24 | int insert(@Param("name")String name,@Param("age") Integer age); 25 | 26 | @Update("UPDATE user set age = #{age} where name=#{name}") 27 | void update(User user); 28 | 29 | @Delete("DELETE FROM user where id = #{id}") 30 | void delete(Long id); 31 | 32 | @Insert("INSERT INTO user(name,age) VALUES(#{name},#{age})") 33 | int insertByMap(Map map); 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /chapter4-8/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/test 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /chapter4-8/src/test/java/com/frank/chapter48/Chapter48ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter48; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.frank.chapter48.domain.User; 5 | import com.frank.chapter48.domain.UserMapper; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.context.SpringBootTest; 11 | import org.springframework.test.annotation.Rollback; 12 | import org.springframework.test.context.junit4.SpringRunner; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | import java.util.HashMap; 16 | import java.util.List; 17 | import java.util.Map; 18 | 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest 21 | public class Chapter48ApplicationTests { 22 | 23 | @Autowired 24 | private UserMapper userMapper; 25 | 26 | @Test 27 | public void test_001(){ 28 | // 插入一条数据 29 | userMapper.insert("AAA",20); 30 | User u = userMapper.findByName("AAA"); 31 | Assert.assertEquals(20,u.getAge().intValue()); 32 | 33 | // 更新数据 34 | u.setAge(30); 35 | userMapper.update(u); 36 | u = userMapper.findByName("AAA"); 37 | Assert.assertEquals(30,u.getAge().intValue()); 38 | 39 | // 删除数据 40 | userMapper.delete(u.getId()); 41 | u = userMapper.findByName("AAA"); 42 | Assert.assertEquals(null,u); 43 | 44 | // Map插入数据 45 | Map map = new HashMap<>(); 46 | map.put("name","CCC"); 47 | map.put("age",23); 48 | userMapper.insertByMap(map); 49 | Assert.assertEquals(23,userMapper.findByName("CCC").getAge().intValue()); 50 | 51 | List userList = userMapper.findAll(); 52 | System.out.println("userList => " + JSON.toJSONString(userList)); 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /chapter4-9/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter4-9/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter4-9/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter4-9/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter4-9/src/main/java/com/frank/chapter49/Chapter49Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter49; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter49Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter49Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter4-9/src/main/java/com/frank/chapter49/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter49.domain; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/14. 10 | */ 11 | @Entity 12 | public class User { 13 | @Id 14 | @GeneratedValue 15 | private Long id; 16 | 17 | @Column(nullable = false,length = 5) 18 | private String name; 19 | 20 | @Column(nullable = false) 21 | private Integer age; 22 | 23 | public User(String name, Integer age) { 24 | this.name = name; 25 | this.age = age; 26 | } 27 | 28 | public User() { 29 | } 30 | 31 | public Long getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Long id) { 36 | this.id = id; 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | public void setName(String name) { 44 | this.name = name; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /chapter4-9/src/main/java/com/frank/chapter49/domain/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter49.domain; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Query; 5 | import org.springframework.data.repository.query.Param; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/14. 9 | */ 10 | public interface UserRepository extends JpaRepository { 11 | User findByName(String name); 12 | 13 | User findByNameAndAge(String name,Integer age); 14 | 15 | @Query("from User u where u.name=:name") 16 | User findUser(@Param("name") String name); 17 | } 18 | -------------------------------------------------------------------------------- /chapter4-9/src/main/java/com/frank/chapter49/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter49.service; 2 | 3 | import com.frank.chapter49.domain.User; 4 | import org.springframework.transaction.annotation.Isolation; 5 | import org.springframework.transaction.annotation.Propagation; 6 | import org.springframework.transaction.annotation.Transactional; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/14. 10 | */ 11 | public interface UserService { 12 | @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.NOT_SUPPORTED) 13 | User login(String name,String password); 14 | } 15 | -------------------------------------------------------------------------------- /chapter4-9/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/test 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 5 | 6 | spring.jpa.properties.hibernate.hbm2ddl.auto=create -------------------------------------------------------------------------------- /chapter5-1/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter5-1/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-1/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter5-1/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter5-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.frank 7 | chapter5-1 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | chapter5-1 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /chapter5-1/src/main/java/com/frank/chapter51/Chapter51Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter51; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.scheduling.annotation.EnableScheduling; 6 | 7 | @SpringBootApplication 8 | @EnableScheduling 9 | public class Chapter51Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Chapter51Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chapter5-1/src/main/java/com/frank/chapter51/task/ScheduledTasks.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter51.task; 2 | 3 | import org.springframework.scheduling.annotation.Scheduled; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.text.SimpleDateFormat; 7 | import java.util.Date; 8 | 9 | /** 10 | * Created by jianweilin on 2017/10/14. 11 | */ 12 | @Component 13 | public class ScheduledTasks { 14 | private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 15 | 16 | @Scheduled(fixedRate = 5000) 17 | public void reportCurrentTime(){ 18 | System.out.println("当前时间: " + dateFormat.format(new Date())); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /chapter5-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-1/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter5-1/src/test/java/com/frank/chapter51/Chapter51ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter51; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter51ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter5-2/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter5-2/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-2/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter5-2/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter5-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.frank 7 | chapter5-2 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | chapter5-2 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter5-2/src/main/java/com/frank/chapter52/Chapter52Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter52; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.scheduling.annotation.EnableAsync; 6 | 7 | @SpringBootApplication 8 | @EnableAsync 9 | public class Chapter52Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Chapter52Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chapter5-2/src/main/java/com/frank/chapter52/task/Task.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter52.task; 2 | 3 | import org.springframework.scheduling.annotation.Async; 4 | import org.springframework.scheduling.annotation.AsyncResult; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.Random; 8 | import java.util.concurrent.Future; 9 | 10 | /** 11 | * Created by jianweilin on 2017/10/14. 12 | */ 13 | @Component 14 | public class Task { 15 | public static Random random = new Random(); 16 | 17 | @Async 18 | public Future doTaskOne() throws InterruptedException { 19 | System.out.println("开始做任务一"); 20 | long start = System.currentTimeMillis(); 21 | Thread.sleep(random.nextInt(10000)); 22 | long end = System.currentTimeMillis(); 23 | System.out.println("完成任务一,耗时:" + (end - start) + "毫秒"); 24 | return new AsyncResult<>("任务一完成"); 25 | } 26 | 27 | @Async 28 | public Future doTaskSecond() throws InterruptedException { 29 | System.out.println("开始做任务二"); 30 | long start = System.currentTimeMillis(); 31 | Thread.sleep(random.nextInt(10000)); 32 | long end = System.currentTimeMillis(); 33 | System.out.println("完成任务二,耗时:" + (end - start) + "毫秒"); 34 | return new AsyncResult<>("任务二完成"); 35 | } 36 | 37 | @Async 38 | public Future doTaskThird() throws InterruptedException { 39 | System.out.println("开始做任务三"); 40 | long start = System.currentTimeMillis(); 41 | Thread.sleep(random.nextInt(10000)); 42 | long end = System.currentTimeMillis(); 43 | System.out.println("完成任务三,耗时:" + (end - start) + "毫秒"); 44 | return new AsyncResult<>("任务三完成"); 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /chapter5-2/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-2/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter5-2/src/test/java/com/frank/chapter52/Chapter52ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter52; 2 | 3 | import com.frank.chapter52.task.Task; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | import java.util.concurrent.Future; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class Chapter52ApplicationTests { 15 | 16 | @Autowired 17 | private Task task; 18 | 19 | @Test 20 | public void test_001() throws InterruptedException { 21 | long start = System.currentTimeMillis(); 22 | Future task1 = task.doTaskOne(); 23 | Future task2 = task.doTaskSecond(); 24 | Future task3 = task.doTaskThird(); 25 | 26 | while (true) { 27 | if(task1.isDone() && task2.isDone() && task3.isDone()) { 28 | // 三个任务都调用完成,退出循环 29 | break; 30 | } 31 | Thread.sleep(1000); 32 | } 33 | long end = System.currentTimeMillis(); 34 | System.out.println("任务全部完成,总耗时: " + (end - start) + "毫秒"); 35 | 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /chapter5-3/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter5-3/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-3/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter5-3/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter5-3/logs/error.log: -------------------------------------------------------------------------------- 1 | 2017-10-14 22:18:11,556 ERROR Chapter53ApplicationTests:17 - 输出error 2 | -------------------------------------------------------------------------------- /chapter5-3/logs/my.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-3/logs/my.log -------------------------------------------------------------------------------- /chapter5-3/src/main/java/com/frank/chapter53/Chapter53Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter53; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter53Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter53Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter5-3/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | logging.level.com.frank=INFO -------------------------------------------------------------------------------- /chapter5-3/src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | logging.level.com.frank=INFO -------------------------------------------------------------------------------- /chapter5-3/src/main/resources/application-test.properties: -------------------------------------------------------------------------------- 1 | logging.level.com.frank=DEBUG -------------------------------------------------------------------------------- /chapter5-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.profiles.active=dev -------------------------------------------------------------------------------- /chapter5-3/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # LOG4J配置 2 | log4j.rootCategory=INFO, stdout, file, errorfile 3 | log4j.category.com.frankspace=${logging.level.com.frank}, frankfile 4 | log4j.logger.error=errorfile 5 | 6 | # 控制台输出 7 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 8 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 9 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 10 | 11 | # root日志输出 12 | log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 13 | log4j.appender.file.file=logs/all.log 14 | log4j.appender.file.DatePattern='.'yyyy-MM-dd 15 | log4j.appender.file.layout=org.apache.log4j.PatternLayout 16 | log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 17 | 18 | # error日志输出 19 | log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender 20 | log4j.appender.errorfile.file=logs/error.log 21 | log4j.appender.errorfile.DatePattern='.'yyyy-MM-dd 22 | log4j.appender.errorfile.Threshold = ERROR 23 | log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout 24 | log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 25 | 26 | # com.frank下的日志输出 27 | log4j.appender.frankfile=org.apache.log4j.DailyRollingFileAppender 28 | log4j.appender.frankfile.file=logs/my.log 29 | log4j.appender.frankfile.DatePattern='.'yyyy-MM-dd 30 | log4j.appender.frankfile.layout=org.apache.log4j.PatternLayout 31 | log4j.appender.frankfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L ---- %m%n 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter5-3/src/test/java/com/frank/chapter53/Chapter53ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter53; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | 9 | @RunWith(SpringRunner.class) 10 | @SpringBootTest 11 | public class Chapter53ApplicationTests { 12 | private Logger logger = Logger.getLogger(getClass()); 13 | @Test 14 | public void test_001(){ 15 | logger.info("输出info"); 16 | logger.debug("输出debug"); 17 | logger.error("输出error"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /chapter5-4/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter5-4/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-4/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter5-4/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter5-4/src/main/java/com/frank/chapter54/Chapter54Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter54; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter54Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter54Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter5-4/src/main/java/com/frank/chapter54/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter54.web; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestMethod; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/14. 10 | */ 11 | @RestController 12 | public class HelloController { 13 | 14 | @RequestMapping(value = "/hello",method = RequestMethod.GET) 15 | public String hello(@RequestParam String name){ 16 | return "Hello" + name; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /chapter5-4/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-4/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter5-4/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # LOG4J配置 2 | log4j.rootCategory=INFO, stdout 3 | log4j.logger.mongodb=INFO, mongodb 4 | 5 | # 控制台输出 6 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 7 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 9 | 10 | # mongodb输出 11 | log4j.appender.mongodb=com.didispace.log.MongoAppender 12 | log4j.appender.mongodb.connectionUrl=mongodb://localhost:27017 13 | log4j.appender.mongodb.databaseName=logs 14 | log4j.appender.mongodb.collectionName=logs_request 15 | -------------------------------------------------------------------------------- /chapter5-4/src/test/java/com/frank/chapter54/Chapter54ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter54; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter54ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter5-5/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter5-5/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-5/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter5-5/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter5-5/logs/error.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-5/logs/error.log -------------------------------------------------------------------------------- /chapter5-5/logs/my.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-5/logs/my.log -------------------------------------------------------------------------------- /chapter5-5/src/main/java/com/frank/chapter55/Chapter55Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter55; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter55Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter55Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter5-5/src/main/java/com/frank/chapter55/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter55.web; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RequestMethod; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | /** 9 | * Created by jianweilin on 2017/10/15. 10 | */ 11 | @RestController 12 | public class HelloController { 13 | 14 | @RequestMapping(value = "/hello",method = RequestMethod.GET) 15 | public String index(@RequestParam String name){ 16 | return "Hello: " + name; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /chapter5-5/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chapter5-5/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # LOG4J配置 2 | log4j.rootCategory=INFO, stdout, file, errorfile 3 | log4j.category.com.frankspace=DEBUG, frankfile 4 | log4j.logger.error=errorfile 5 | 6 | # 控制台输出 7 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 8 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 9 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 10 | 11 | # root日志输出 12 | log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 13 | log4j.appender.file.file=logs/all.log 14 | log4j.appender.file.DatePattern='.'yyyy-MM-dd 15 | log4j.appender.file.layout=org.apache.log4j.PatternLayout 16 | log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 17 | 18 | # error日志输出 19 | log4j.appender.errorfile=org.apache.log4j.DailyRollingFileAppender 20 | log4j.appender.errorfile.file=logs/error.log 21 | log4j.appender.errorfile.DatePattern='.'yyyy-MM-dd 22 | log4j.appender.errorfile.Threshold = ERROR 23 | log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout 24 | log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 25 | 26 | # com.didispace下的日志输出 27 | log4j.appender.frankfile=org.apache.log4j.DailyRollingFileAppender 28 | log4j.appender.frankfile.file=logs/my.log 29 | log4j.appender.frankfile.DatePattern='.'yyyy-MM-dd 30 | log4j.appender.frankfile.layout=org.apache.log4j.PatternLayout 31 | log4j.appender.frankfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L ---- %m%n 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter5-5/src/test/java/com/frank/chapter55/Chapter55ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter55; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter55ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter5-6/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter5-6/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-6/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter5-6/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter5-6/src/main/java/com/frank/chapter56/Chapter56Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter56; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter56Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter56Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter5-6/src/main/java/com/frank/chapter56/config/WebSecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter56.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 | 10 | /** 11 | * Created by jianweilin on 2017/10/15. 12 | */ 13 | @Configuration 14 | @EnableWebSecurity 15 | public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 16 | 17 | @Override 18 | protected void configure(HttpSecurity http) throws Exception { 19 | http.authorizeRequests() 20 | .antMatchers("/","/home").permitAll() 21 | .anyRequest().authenticated() 22 | .and() 23 | .formLogin() 24 | .loginPage("/login") 25 | .permitAll() 26 | .and() 27 | .logout() 28 | .permitAll(); 29 | } 30 | 31 | @Autowired 32 | public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 33 | auth.inMemoryAuthentication() 34 | .withUser("user") 35 | .password("password") 36 | .roles("USER"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /chapter5-6/src/main/java/com/frank/chapter56/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter56.web; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/15. 9 | */ 10 | @Controller 11 | public class HelloController { 12 | @RequestMapping("/") 13 | public String index(){ 14 | return "index"; 15 | } 16 | 17 | @RequestMapping("/hello") 18 | public String hello(){ 19 | return "hello"; 20 | } 21 | 22 | @RequestMapping(value = "/login",method = RequestMethod.GET) 23 | public String login(){ 24 | return "login"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /chapter5-6/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter5-6/src/main/resources/application.properties -------------------------------------------------------------------------------- /chapter5-6/src/main/resources/templates/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Hello World! 6 | 7 | 8 |

Hello [[${#httpServletRequest.remoteUser}]]!

9 |
10 | 11 |
12 | 13 | -------------------------------------------------------------------------------- /chapter5-6/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | Spring Security入门 6 | 7 | 8 |

欢迎使用Spring Security!

9 | 10 |

点击 这里 打个招呼吧

11 | 12 | -------------------------------------------------------------------------------- /chapter5-6/src/main/resources/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Spring Security Example 7 | 8 | 9 |
10 | 用户名或密码错 11 |
12 |
13 | 您已注销成功 14 |
15 |
16 |
17 |
18 |
19 |
20 | 21 | -------------------------------------------------------------------------------- /chapter5-6/src/test/java/com/frank/chapter56/Chapter56ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter56; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | import org.springframework.test.context.web.WebAppConfiguration; 8 | 9 | @RunWith(SpringRunner.class) 10 | @SpringBootTest 11 | @WebAppConfiguration 12 | public class Chapter56ApplicationTests { 13 | @Test 14 | public void test_001(){} 15 | } 16 | -------------------------------------------------------------------------------- /chapter6-1/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter6-1/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter6-1/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter6-1/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter6-1/man.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter6-1/man.jpg -------------------------------------------------------------------------------- /chapter6-1/src/main/java/com/frank/chapter61/Chapter61Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter61; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter61Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter61Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter6-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # 用qq邮箱测试,有权限问题 2 | spring.mail.host=smtp.163.com 3 | spring.mail.username=username@163.com 4 | spring.mail.password=password 5 | spring.mail.properties.mail.smtp.auth=true 6 | spring.mail.properties.mail.smtp.starttls.enable=true 7 | spring.mail.properties.mail.smtp.starttls.required=true -------------------------------------------------------------------------------- /chapter6-1/src/main/resources/templates/template.vm: -------------------------------------------------------------------------------- 1 | 2 | 3 |

你好, ${username}, 这是一封模板邮件!

4 | 5 | -------------------------------------------------------------------------------- /chapter6-2/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter6-2/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter6-2/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter6-2/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter6-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.frank 7 | chapter6-2 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | chapter6-2 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-amqp 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter6-2/src/main/java/com/frank/chapter62/Chapter62Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter62; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter62Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter62Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter6-2/src/main/java/com/frank/chapter62/rabbit/RabbitConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter62.rabbit; 2 | 3 | import org.springframework.amqp.core.Queue; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/15. 9 | */ 10 | @Configuration 11 | public class RabbitConfig { 12 | @Bean 13 | public Queue helloQueue(){ 14 | return new Queue("hello"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /chapter6-2/src/main/java/com/frank/chapter62/rabbit/Receiver.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter62.rabbit; 2 | 3 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 4 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | /** 8 | * Created by jianweilin on 2017/10/15. 9 | */ 10 | @Component 11 | @RabbitListener(queues = "hello") 12 | public class Receiver { 13 | 14 | @RabbitHandler 15 | public void process(String hello) { 16 | System.out.println("Receiver: " + hello); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /chapter6-2/src/main/java/com/frank/chapter62/rabbit/Sender.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter62.rabbit; 2 | 3 | import org.springframework.amqp.core.AmqpTemplate; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.Date; 8 | 9 | /** 10 | * Created by jianweilin on 2017/10/15. 11 | */ 12 | @Component 13 | public class Sender { 14 | @Autowired 15 | private AmqpTemplate amqpTemplate; 16 | 17 | public void send(){ 18 | String context = "hello " + new Date(); 19 | System.out.println("Sender : " + context); 20 | this.amqpTemplate.convertAndSend("hello", context); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter6-2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=rabbitmq-hello 2 | 3 | spring.rabbitmq.host=localhost 4 | spring.rabbitmq.port=5672 5 | spring.rabbitmq.username=springcloud 6 | spring.rabbitmq.password=123456 -------------------------------------------------------------------------------- /chapter6-2/src/test/java/com/frank/chapter62/Chapter62ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter62; 2 | 3 | import com.frank.chapter62.rabbit.Sender; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.test.context.junit4.SpringRunner; 9 | 10 | @RunWith(SpringRunner.class) 11 | @SpringBootTest 12 | public class Chapter62ApplicationTests { 13 | 14 | @Autowired 15 | private Sender sender; 16 | @Test 17 | public void test_001(){ 18 | sender.send(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /chapter6-3/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter6-3/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter6-3/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter6-3/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip 2 | -------------------------------------------------------------------------------- /chapter6-3/src/main/java/com/frank/chapter63/Chapter63Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter63; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Chapter63Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Chapter63Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /chapter6-3/src/main/java/com/frank/chapter63/EventConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter63; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.statemachine.annotation.OnTransition; 6 | import org.springframework.statemachine.annotation.OnTransitionEnd; 7 | import org.springframework.statemachine.annotation.OnTransitionStart; 8 | import org.springframework.statemachine.annotation.WithStateMachine; 9 | 10 | /** 11 | * Created by jianweilin on 2017/10/15. 12 | */ 13 | @WithStateMachine 14 | public class EventConfig { 15 | private Logger logger = LoggerFactory.getLogger(getClass()); 16 | 17 | 18 | @OnTransition(target = "UNPAID") 19 | public void create() { 20 | logger.info("订单创建,待支付"); 21 | } 22 | 23 | @OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE") 24 | public void pay() { 25 | logger.info("用户完成支付,待收货"); 26 | } 27 | 28 | @OnTransitionStart(source = "UNPAID", target = "WAITING_FOR_RECEIVE") 29 | public void payStart() { 30 | logger.info("用户完成支付,待收货: start"); 31 | } 32 | 33 | @OnTransitionEnd(source = "UNPAID", target = "WAITING_FOR_RECEIVE") 34 | public void payEnd() { 35 | logger.info("用户完成支付,待收货: end"); 36 | } 37 | 38 | @OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE") 39 | public void receive() { 40 | logger.info("用户已收货,订单完成"); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter6-3/src/main/java/com/frank/chapter63/Events.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter63; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/15. 5 | */ 6 | public enum Events { 7 | PAY, // 支付 8 | RECEIVE; // 收获 9 | } 10 | -------------------------------------------------------------------------------- /chapter6-3/src/main/java/com/frank/chapter63/StateMachineConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter63; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.statemachine.config.EnableStateMachine; 7 | import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; 8 | import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; 9 | import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; 10 | 11 | import java.util.EnumSet; 12 | 13 | /** 14 | * Created by jianweilin on 2017/10/15. 15 | */ 16 | @Configuration 17 | @EnableStateMachine 18 | public class StateMachineConfig extends EnumStateMachineConfigurerAdapter { 19 | private Logger logger = LoggerFactory.getLogger(getClass()); 20 | 21 | @Override 22 | public void configure(StateMachineStateConfigurer states) 23 | throws Exception { 24 | states 25 | .withStates() 26 | .initial(States.UNPAID) 27 | .states(EnumSet.allOf(States.class)); 28 | } 29 | 30 | @Override 31 | public void configure(StateMachineTransitionConfigurer transitions) 32 | throws Exception { 33 | transitions 34 | .withExternal() 35 | .source(States.UNPAID).target(States.WAIT_FOR_RECEIVE) 36 | .event(Events.PAY) 37 | .and() 38 | .withExternal() 39 | .source(States.WAIT_FOR_RECEIVE).target(States.DONE) 40 | .event(Events.RECEIVE); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /chapter6-3/src/main/java/com/frank/chapter63/States.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter63; 2 | 3 | /** 4 | * Created by jianweilin on 2017/10/15. 5 | */ 6 | public enum States { 7 | UNPAID, // 待支付 8 | WAIT_FOR_RECEIVE, // 待收货 9 | DONE; // 结束 10 | } 11 | -------------------------------------------------------------------------------- /chapter6-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=statemachine -------------------------------------------------------------------------------- /chapter6-3/src/test/java/com/frank/chapter63/Chapter63ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter63; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter63ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter6-4/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /chapter6-4/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teruiV/SpringBootDemo/f66a8891ae762be918416e67ba1c0511766fbe1e/chapter6-4/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /chapter6-4/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip 2 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/Chapter64Application.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64; 2 | 3 | import com.frank.chapter64.config.EmbedZookeeperServer; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | public class Chapter64Application { 9 | 10 | public static void main(String[] args) { 11 | EmbedZookeeperServer.start(6181); 12 | SpringApplication.run(Chapter64Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/config/EmbedZookeeperServer.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.config; 2 | 3 | 4 | import org.apache.curator.test.TestingServer; 5 | 6 | import java.io.File; 7 | import java.io.IOException; 8 | 9 | /** 10 | * @author jianweilin 11 | * @date 2017/10/31 12 | */ 13 | public class EmbedZookeeperServer { 14 | private static TestingServer testingServer; 15 | 16 | public static void start(final int port){ 17 | try{ 18 | testingServer = new TestingServer(port,new File(String.format("target/test_zk_data/%s/",System.nanoTime()))); 19 | } catch (Exception e) { 20 | e.printStackTrace(); 21 | }finally { 22 | Runtime.getRuntime().addShutdownHook(new Thread(() -> { 23 | try { 24 | Thread.sleep(1000L); 25 | testingServer.close(); 26 | } catch (InterruptedException | IOException e) { 27 | e.printStackTrace(); 28 | } 29 | })); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/config/JobEventConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.config; 2 | 3 | import com.dangdang.ddframe.job.event.JobEventConfiguration; 4 | import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import javax.annotation.Resource; 9 | import javax.sql.DataSource; 10 | 11 | /** 12 | * @author jianweilin 13 | * @date 2017/10/31 14 | */ 15 | @Configuration 16 | public class JobEventConfig { 17 | 18 | @Resource 19 | private DataSource dataSource; 20 | 21 | @Bean 22 | public JobEventConfiguration jobEventConfiguration() { 23 | return new JobEventRdbConfiguration(dataSource); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/config/ZookeeperRegistryCenterConfig.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.config; 2 | 3 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration; 4 | import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | /** 11 | * @author jianweilin 12 | * @date 2017/10/31 13 | */ 14 | @Configuration 15 | @ConditionalOnExpression("'${regCenter.serverList}'.length()>0") 16 | public class ZookeeperRegistryCenterConfig { 17 | @Bean(initMethod = "init") 18 | public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList, 19 | @Value("${regCenter.namespace}") final String namespace){ 20 | return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList,namespace)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/domain/Foo.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author jianweilin 7 | * @date 2017/10/31 8 | */ 9 | public class Foo implements Serializable{ 10 | private final long id; 11 | private final String location; 12 | private Status status; 13 | 14 | public Foo(long id, String location, Status status) { 15 | this.id = id; 16 | this.location = location; 17 | this.status = status; 18 | } 19 | 20 | public long getId() { 21 | return id; 22 | } 23 | 24 | public String getLocation() { 25 | return location; 26 | } 27 | 28 | public Status getStatus() { 29 | return status; 30 | } 31 | 32 | public void setStatus(Status status) { 33 | this.status = status; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Foo{" + 39 | "id=" + id + 40 | ", location='" + location + '\'' + 41 | ", status=" + status + 42 | '}'; 43 | } 44 | 45 | public enum Status{ 46 | TODO, 47 | COMPLETED 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/job/dataflow/SpringDataflowJob.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.job.dataflow; 2 | 3 | import com.dangdang.ddframe.job.api.ShardingContext; 4 | import com.dangdang.ddframe.job.api.dataflow.DataflowJob; 5 | import com.frank.chapter64.domain.Foo; 6 | import com.frank.chapter64.repository.FooRepository; 7 | 8 | import javax.annotation.Resource; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | /** 14 | * @author jianweilin 15 | * @date 2017/10/31 16 | */ 17 | public class SpringDataflowJob implements DataflowJob{ 18 | @Resource 19 | private FooRepository fooRepository; 20 | 21 | @Override 22 | public List fetchData(final ShardingContext shardingContext) { 23 | System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s", 24 | shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW FETCH")); 25 | return fooRepository.findTodoData(shardingContext.getShardingParameter(), 10); 26 | } 27 | 28 | @Override 29 | public void processData(final ShardingContext shardingContext, final List data) { 30 | System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s", 31 | shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW PROCESS")); 32 | for (Foo each : data) { 33 | fooRepository.setCompleted(each.getId()); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/job/simple/SpringSimpleJob.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.job.simple; 2 | 3 | import com.dangdang.ddframe.job.api.ShardingContext; 4 | import com.dangdang.ddframe.job.api.simple.SimpleJob; 5 | import com.frank.chapter64.domain.Foo; 6 | import com.frank.chapter64.repository.FooRepository; 7 | 8 | import javax.annotation.Resource; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | /** 14 | * @author jianweilin 15 | * @date 2017/10/31 16 | */ 17 | public class SpringSimpleJob implements SimpleJob{ 18 | 19 | @Resource 20 | private FooRepository fooRepository; 21 | 22 | @Override 23 | public void execute(ShardingContext shardingContext) { 24 | System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s", 25 | shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE")); 26 | List data = fooRepository.findTodoData(shardingContext.getShardingParameter(), 10); 27 | for (Foo each : data) { 28 | fooRepository.setCompleted(each.getId()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /chapter6-4/src/main/java/com/frank/chapter64/repository/FooRepository.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64.repository; 2 | 3 | import com.frank.chapter64.domain.Foo; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | /** 12 | * @author jianweilin 13 | * @date 2017/10/31 14 | */ 15 | @Repository 16 | public class FooRepository { 17 | private Map data = new ConcurrentHashMap<>(300,1); 18 | 19 | public FooRepository(){init();} 20 | 21 | private void init(){ 22 | addData(0L, 100L, "Beijing"); 23 | addData(100L, 200L, "Shanghai"); 24 | addData(200L, 300L, "Guangzhou"); 25 | } 26 | 27 | private void addData(final Long idFrom,final Long idTo,final String location){ 28 | for(long i=idFrom; i < idTo; i++) { 29 | data.put(i,new Foo(i,location,Foo.Status.TODO)); 30 | } 31 | } 32 | 33 | public List findTodoData(final String location, final int limit) { 34 | List result = new ArrayList<>(limit); 35 | int count = 0; 36 | for (Map.Entry each : data.entrySet()) { 37 | Foo foo = each.getValue(); 38 | if (foo.getLocation().equals(location) && foo.getStatus() == Foo.Status.TODO) { 39 | result.add(foo); 40 | count++; 41 | if (count == limit) { 42 | break; 43 | } 44 | } 45 | } 46 | return result; 47 | } 48 | 49 | public void setCompleted(final long id) { 50 | data.get(id).setStatus(Foo.Status.COMPLETED); 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /chapter6-4/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:job_event_storage 2 | spring.datasource.driver-class-name=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= -------------------------------------------------------------------------------- /chapter6-4/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | regCenter.serverList=localhost:6181 2 | regCenter.namespace=elastic-job-lite-springboot 3 | 4 | 5 | simpleJob.cron=0/2 * * * * ? 6 | simpleJob.shardingTotalCount=3 7 | simpleJob.shardingItemParameters=0=Beijing,1=Shanghai,2=Guangzhou 8 | 9 | dataflowJob.cron=0/5 * * * * ? 10 | dataflowJob.shardingTotalCount=3 11 | dataflowJob.shardingItemParameters=0=Beijing,1=Shanghai,2=Guangzhou 12 | 13 | spring.profiles.active=dev -------------------------------------------------------------------------------- /chapter6-4/src/test/java/com/frank/chapter64/Chapter64ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.frank.chapter64; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class Chapter64ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /chapter7-1-1/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /chapter7-1-1/src/main/java/com/william/chapter711/Chapter711Application.java: -------------------------------------------------------------------------------- 1 | package com.william.chapter711; 2 | 3 | import com.william.chapter71.domain.Greeter; 4 | import lombok.extern.slf4j.Slf4j; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | @Slf4j 11 | @SpringBootApplication 12 | public class Chapter711Application implements CommandLineRunner{ 13 | 14 | @Autowired 15 | private Greeter greeter; 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(Chapter711Application.class, args); 19 | } 20 | 21 | @Override 22 | public void run(String... args) throws Exception { 23 | log.info("greeter info = {}", greeter.greet()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /chapter7-1-1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | com.william.greeter.user-name=william 2 | com.william.greeter.morning-msg=hi all, good morning, nice to see you. 3 | com.william.greeter.afternoon-msg=please, wake up eat lunch 4 | com.william.greeter.evening-msg=hi all, custom define evening 5 | com.william.greeter.night-msg=hi all, custom define night -------------------------------------------------------------------------------- /chapter7-1/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /chapter7-1/src/main/java/com/william/chapter71/autoconfigure/GreeterProperties.java: -------------------------------------------------------------------------------- 1 | package com.william.chapter71.autoconfigure; 2 | 3 | import lombok.Data; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | /** 7 | * @author jianweilin 8 | * @date 2018/9/29 9 | */ 10 | @ConfigurationProperties(prefix = "com.william.greeter") 11 | @Data 12 | public class GreeterProperties { 13 | private String userName; 14 | private String morningMsg; 15 | private String afternoonMsg; 16 | private String eveningMsg; 17 | private String nightMsg; 18 | } 19 | -------------------------------------------------------------------------------- /chapter7-1/src/main/java/com/william/chapter71/domain/Greeter.java: -------------------------------------------------------------------------------- 1 | package com.william.chapter71.domain; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | /** 6 | * @author jianweilin 7 | * @date 2018/9/29 8 | */ 9 | public class Greeter { 10 | public static final String USER_NAME = "user.name"; 11 | public static final String MORNING_MSG = "morning.msg"; 12 | public static final String AFTERNOON_MSG = "afternoon.msg"; 13 | public static final String EVENING_MSG = "evening.msg"; 14 | public static final String NIGHT_MSG = "night.msg"; 15 | 16 | private GreeterConfig greeterConfig; 17 | 18 | public Greeter(GreeterConfig greeterConfig) { 19 | this.greeterConfig = greeterConfig; 20 | } 21 | 22 | public String greet(LocalDateTime localDateTime) { 23 | String name = greeterConfig.getProperty(USER_NAME); 24 | int hourOfDay = localDateTime.getHour(); 25 | 26 | if (hourOfDay >= 5 && hourOfDay < 12) { 27 | return String.format("Hello %s, %s", name, greeterConfig.get(MORNING_MSG)); 28 | } else if (hourOfDay >= 12 && hourOfDay < 17) { 29 | return String.format("Hello %s, %s", name, greeterConfig.get(AFTERNOON_MSG)); 30 | } else if (hourOfDay >= 17 && hourOfDay < 20) { 31 | return String.format("Hello %s, %s", name, greeterConfig.get(EVENING_MSG)); 32 | } else { 33 | return String.format("Hello %s, %s", name, greeterConfig.get(NIGHT_MSG)); 34 | } 35 | } 36 | 37 | public String greet(){ 38 | return greet(LocalDateTime.now()); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /chapter7-1/src/main/java/com/william/chapter71/domain/GreeterConfig.java: -------------------------------------------------------------------------------- 1 | package com.william.chapter71.domain; 2 | 3 | import java.util.Properties; 4 | 5 | /** 6 | * @author jianweilin 7 | * @date 2018/9/29 8 | */ 9 | public class GreeterConfig extends Properties { 10 | } 11 | -------------------------------------------------------------------------------- /chapter7-1/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.william.chapter71.autoconfigure.GreeterAutoConfiguration --------------------------------------------------------------------------------