├── .editorconfig ├── .gitignore ├── .springjavaformatconfig ├── README.md ├── db ├── 1schema.sql └── 2init.sql ├── pom.xml ├── spring-boot-01-basic ├── pom.xml ├── spring-boot-banner │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootbanner │ │ │ └── SpringBootBannerApplication.java │ │ └── resources │ │ ├── application.properties │ │ └── banner.txt ├── spring-boot-helloWorld │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springboothelloWorld │ │ │ │ └── SpringBootHelloWorldApplication.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springboothelloWorld │ │ └── SpringBootHelloWorldApplicationTests.java ├── spring-boot-properties │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootproperties │ │ │ │ ├── SpringBootPropertiesApplication.java │ │ │ │ └── entity │ │ │ │ ├── Properties1.java │ │ │ │ └── Properties2.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootproperties │ │ └── SpringBootPropertiesApplicationTests.java └── spring-boot-yaml │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootyaml │ │ │ ├── SpringBootYamlApplication.java │ │ │ └── entity │ │ │ ├── People.java │ │ │ └── Pet.java │ └── resources │ │ └── application.yml │ └── test │ └── java │ └── com │ └── louis │ └── springbootyaml │ └── SpringBootYamlApplicationTests.java ├── spring-boot-02-templateEngines ├── pom.xml ├── spring-boot-freemarker │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootfreemarker │ │ │ │ ├── SpringBootFreemarkerApplication.java │ │ │ │ └── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ └── resources │ │ │ ├── application.yml │ │ │ └── templates │ │ │ └── index.ftl │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootfreemark │ │ └── SpringBootFreemarkApplicationTests.java └── spring-boot-thymeleaf │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootthymeleaf │ │ │ ├── SpringBootThymeleafApplication.java │ │ │ └── entity │ │ │ ├── People.java │ │ │ └── Pet.java │ └── resources │ │ ├── application.yml │ │ └── templates │ │ └── index.html │ └── test │ └── java │ └── com │ └── louis │ └── springbootthymeleaf │ └── SpringBootThymeleafApplicationTests.java ├── spring-boot-03-sql ├── pom.xml ├── spring-boot-jpa │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootjpa │ │ │ │ ├── SpringBootJpaApplication.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ └── repository │ │ │ │ ├── PeopleRepository.java │ │ │ │ └── PetRepository.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootjpa │ │ └── SpringBootJpaApplicationTests.java ├── spring-boot-mybatis-annotation │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootmybatisannotation │ │ │ │ ├── SpringBootMybatisAnnotationApplication.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ └── mapper │ │ │ │ └── PeopleMapper.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootmybatisannotation │ │ └── SpringBootMybatisAnnotationApplicationTests.java ├── spring-boot-mybatis-flex │ ├── mybatis-flex.config │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootmybatisflex │ │ │ │ ├── SpringBootMybatisFlexApplication.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ └── mapper │ │ │ │ └── PeopleMapper.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootmybatisflex │ │ └── SpringBootMybatisFlexApplicationTests.java ├── spring-boot-mybatis-plus │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootmybatisplus │ │ │ │ ├── SpringBootMybatisPlusApplication.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ └── mapper │ │ │ │ └── PeopleMapper.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootmybatisplus │ │ └── SpringBootMybatisPlusApplicationTests.java └── spring-boot-mybatis │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootmybatis │ │ │ ├── SpringBootMybatisApplication.java │ │ │ ├── entity │ │ │ ├── People.java │ │ │ └── Pet.java │ │ │ └── mapper │ │ │ └── PeopleMapper.java │ └── resources │ │ ├── application.yml │ │ └── mapper │ │ └── PeopleMapper.xml │ └── test │ └── java │ └── com │ └── louis │ └── springbootmybatis │ └── SpringBootMybatisApplicationTests.java ├── spring-boot-04-nosql ├── pom.xml ├── spring-boot-ehcache │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootehcache │ │ │ ├── SpringBootEhcacheApplication.java │ │ │ ├── controller │ │ │ └── PeopleController.java │ │ │ ├── entity │ │ │ ├── People.java │ │ │ └── Pet.java │ │ │ └── service │ │ │ └── PeopleService.java │ │ └── resources │ │ ├── application.properties │ │ └── ehcache.xml ├── spring-boot-mongodb │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootmongodb │ │ │ │ ├── SpringBootMongodbApplication.java │ │ │ │ ├── controller │ │ │ │ ├── PeopleController.java │ │ │ │ └── PetController.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ ├── repository │ │ │ │ ├── PeopleRepository.java │ │ │ │ └── PetRepository.java │ │ │ │ └── service │ │ │ │ ├── PeopleService.java │ │ │ │ └── PetService.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootmongodb │ │ └── SpringBootMongodbApplicationTests.java ├── spring-boot-redis-annotation │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootredisannotation │ │ │ │ ├── SpringBootRedisAnnotationApplication.java │ │ │ │ ├── conf │ │ │ │ └── RedisConfig.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ └── service │ │ │ │ └── PeopleService.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootredisannotation │ │ └── SpringBootRedisAnnotationApplicationTests.java ├── spring-boot-redis │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── louis │ │ │ │ └── springbootredis │ │ │ │ ├── SpringBootRedisApplication.java │ │ │ │ ├── conf │ │ │ │ └── RedisConfig.java │ │ │ │ ├── entity │ │ │ │ ├── People.java │ │ │ │ └── Pet.java │ │ │ │ └── util │ │ │ │ └── RedisTemplateUtil.java │ │ └── resources │ │ │ └── application.yml │ │ └── test │ │ └── java │ │ └── com │ │ └── louis │ │ └── springbootredis │ │ └── SpringBootRedisApplicationTests.java └── spring-boot-redisson │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── louis │ │ └── springbootredisson │ │ ├── SpringBootRedissonApplication.java │ │ ├── config │ │ └── RedissonConfig.java │ │ ├── controller │ │ └── LockController.java │ │ └── service │ │ └── DistributedLockService.java │ └── resources │ └── application.yml ├── spring-boot-05-batch ├── pom.xml ├── spring-boot-quartz │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootquartz │ │ │ ├── SpringBootQuartzApplication.java │ │ │ ├── config │ │ │ └── QuartzConfig.java │ │ │ └── job │ │ │ └── SimpleJob.java │ │ └── resources │ │ └── application.properties ├── spring-boot-springbatch │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootspringbatch │ │ │ ├── SpringBootSpringbatchApplication.java │ │ │ ├── config │ │ │ └── BatchConfig.java │ │ │ ├── model │ │ │ └── Trade.java │ │ │ └── repository │ │ │ └── TradeRepository.java │ │ └── resources │ │ ├── application.properties │ │ └── trades.csv ├── spring-boot-task │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springboottask │ │ │ ├── SpringBootTaskApplication.java │ │ │ ├── config │ │ │ └── SchedulerConfig.java │ │ │ └── task │ │ │ └── ScheduledTask.java │ │ └── resources │ │ └── application.properties └── spring-boot-xxljob │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── louis │ │ └── springbootxxljob │ │ ├── SpringBootXxljobApplication.java │ │ ├── config │ │ └── XxlJobConfig.java │ │ └── job │ │ └── SampleJobHandler.java │ └── resources │ └── application.properties ├── spring-boot-06-security ├── README.md └── pom.xml ├── spring-boot-07-messaging ├── pom.xml ├── spring-boot-activemq │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootactivemq │ │ │ ├── SpringBootActivemqApplication.java │ │ │ ├── component │ │ │ ├── MessageConsumer.java │ │ │ └── MessageProducer.java │ │ │ └── controller │ │ │ └── DemoController.java │ │ └── resources │ │ └── application.yml ├── spring-boot-rabbitmq │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootrabbitmq │ │ │ ├── SpringBootRabbitmqApplication.java │ │ │ ├── config │ │ │ └── RabbitMQConfig.java │ │ │ ├── consumer │ │ │ └── MessageConsumer.java │ │ │ ├── controller │ │ │ └── TestController.java │ │ │ ├── entity │ │ │ ├── People.java │ │ │ └── Pet.java │ │ │ └── producer │ │ │ └── MessageProducer.java │ │ └── resources │ │ └── application.yml ├── spring-boot-rocketmq │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootrocketmq │ │ │ ├── SpringBootRocketmqApplication.java │ │ │ ├── config │ │ │ └── MessageConverterConfig.java │ │ │ ├── consumer │ │ │ ├── DemoMessageConsumer.java │ │ │ ├── OrderMessageConsumer.java │ │ │ └── TagAMessageConsumer.java │ │ │ ├── controller │ │ │ └── TestController.java │ │ │ ├── dto │ │ │ └── Order.java │ │ │ ├── producer │ │ │ └── MessageProducer.java │ │ │ └── service │ │ │ └── FileStorageService.java │ │ └── resources │ │ └── application.yml └── spring-boot-websocket │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── louis │ │ └── springbootwebsocket │ │ ├── SpringBootWebsocketApplication.java │ │ ├── config │ │ └── WebSocketConfig.java │ │ └── controller │ │ └── WebSocketController.java │ └── resources │ └── application.yml ├── spring-boot-08-search ├── pom.xml └── spring-boot-elasticsearch │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── louis │ │ └── springbootelasticsearch │ │ ├── SpringBootElasticsearchApplication.java │ │ ├── controller │ │ └── PeopleController.java │ │ ├── entity │ │ ├── People.java │ │ └── Pet.java │ │ └── repository │ │ └── PeopleRepository.java │ └── resources │ └── application.yml ├── spring-boot-09-ai ├── pom.xml ├── spring-boot-deepseek │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootdeepseek │ │ │ ├── SpringBootDeepseekApplication.java │ │ │ ├── config │ │ │ └── DeepSeekConfig.java │ │ │ ├── controller │ │ │ └── DeepSeekController.java │ │ │ ├── model │ │ │ ├── Choice.java │ │ │ ├── DeepSeekRequest.java │ │ │ ├── DeepSeekResponse.java │ │ │ ├── Message.java │ │ │ └── Usage.java │ │ │ └── service │ │ │ └── DeepSeekService.java │ │ └── resources │ │ └── application.yml ├── spring-boot-ollama │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootollama │ │ │ ├── SpringBootOllamaApplication.java │ │ │ ├── controller │ │ │ └── OllamaController.java │ │ │ └── service │ │ │ └── OllamaService.java │ │ └── resources │ │ └── application.yml ├── spring-boot-openai │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── louis │ │ │ └── springbootopenai │ │ │ ├── SpringBootOpenaiApplication.java │ │ │ ├── controller │ │ │ └── OpenAIController.java │ │ │ └── service │ │ │ └── OpenAIService.java │ │ └── resources │ │ └── application.yml └── spring-boot-qwen │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── com │ │ └── louis │ │ └── springbootqwen │ │ ├── SpringBootQwenApplication.java │ │ ├── controller │ │ └── AIController.java │ │ └── service │ │ └── QwenAIService.java │ └── resources │ └── application.yml └── spring-boot-reference.pdf /.editorconfig: -------------------------------------------------------------------------------- 1 | # 开发组IDE 编辑器标准 V2.1.0 2 | root = true 3 | 4 | [*.{adoc, bat, groovy, html, java, js, jsp, kt, kts, md, properties, py, rb, sh, sql, svg, txt, xml, xsd}] 5 | charset = utf-8 6 | 7 | [*.{groovy, java, kt, kts, xml, xsd}] 8 | indent_style = tab 9 | indent_size = 4 10 | continuation_indent_size = 8 11 | end_of_line = lf 12 | 13 | [*.{js, html}] 14 | indent_style = space 15 | indent_size = 2 16 | end_of_line = lf 17 | insert_final_newline = true 18 | trim_trailing_whitespace = true 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # 忽略匹配下列规则的Git 提交 V2.1.0 2 | ### gradle ### 3 | .gradle 4 | /build/ 5 | !gradle/wrapper/gradle-wrapper.jar 6 | 7 | ### STS ### 8 | .settings/ 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | bin/ 16 | 17 | ### IntelliJ IDEA ### 18 | .idea 19 | *.iws 20 | *.iml 21 | *.ipr 22 | *.lock 23 | rebel.xml 24 | 25 | ### NetBeans ### 26 | nbproject/private/ 27 | build/ 28 | nbbuild/ 29 | nbdist/ 30 | .nb-gradle/ 31 | 32 | ### maven ### 33 | target/ 34 | *.war 35 | *.ear 36 | *.zip 37 | *.tar 38 | *.tar.gz 39 | 40 | ### logs #### 41 | /logs/ 42 | *.log 43 | 44 | ### temp ignore ### 45 | *.cache 46 | *.diff 47 | *.patch 48 | *.tmp 49 | *.java~ 50 | *.properties~ 51 | *.xml~ 52 | sessionStore 53 | 54 | ### system ignore ### 55 | .DS_Store 56 | Thumbs.db 57 | Servers 58 | .metadata 59 | upload 60 | gen_code 61 | 62 | ### node ### 63 | node_modules 64 | 65 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 66 | hs_err_pid* 67 | replay_pid* 68 | -------------------------------------------------------------------------------- /.springjavaformatconfig: -------------------------------------------------------------------------------- 1 | java-baseline=8 2 | -------------------------------------------------------------------------------- /db/1schema.sql: -------------------------------------------------------------------------------- 1 | -- 核心表 2 | create database `springboot_examples` default character set utf8mb4 collate utf8mb4_general_ci; 3 | 4 | use springboot_examples; -------------------------------------------------------------------------------- /db/2init.sql: -------------------------------------------------------------------------------- 1 | use springboot_examples; 2 | 3 | SET NAMES utf8mb4; 4 | SET FOREIGN_KEY_CHECKS = 0; 5 | 6 | -- ---------------------------- 7 | -- Table structure for people 8 | -- ---------------------------- 9 | DROP TABLE IF EXISTS `people`; 10 | CREATE TABLE `people` ( 11 | `id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键ID', 12 | `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '姓名', 13 | `age` int(0) NULL DEFAULT 0 COMMENT '年龄', 14 | `remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注', 15 | `del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '0' COMMENT '删除标志', 16 | `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间', 17 | `update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '更新时间', 18 | PRIMARY KEY (`id`) USING BTREE 19 | ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '人物表' ROW_FORMAT = Dynamic; 20 | 21 | -- ---------------------------- 22 | -- Records of people 23 | -- ---------------------------- 24 | INSERT INTO `people` VALUES (1, 'Louis', 18, ' ', '0', now(), now()); 25 | 26 | -- ---------------------------- 27 | -- Table structure for pet 28 | -- ---------------------------- 29 | DROP TABLE IF EXISTS `pet`; 30 | CREATE TABLE `pet` ( 31 | `id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键ID', 32 | `people_id` bigint(0) NOT NULL COMMENT '人物ID', 33 | `pet_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '宠物名称', 34 | `del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '0' COMMENT '删除标志', 35 | `create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间', 36 | `update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '更新时间', 37 | PRIMARY KEY (`id`) USING BTREE 38 | ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '宠物表' ROW_FORMAT = Dynamic; 39 | 40 | -- ---------------------------- 41 | -- Records of pet 42 | -- ---------------------------- 43 | INSERT INTO `pet` VALUES (1, 1, '道尔格', '0', now(), now()); 44 | INSERT INTO `pet` VALUES (2, 1, '卡特', '0', now(), now()); 45 | 46 | SET FOREIGN_KEY_CHECKS = 1; 47 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.1.5 9 | 10 | 11 | 12 | com.louis 13 | spring-boot-examples 14 | 1.0 15 | pom 16 | ${project.artifactId} 17 | Demo project for Spring Boot 18 | 19 | 20 | 21 | spring-boot-01-basic 22 | 23 | spring-boot-02-templateEngines 24 | 25 | spring-boot-03-sql 26 | 27 | spring-boot-04-nosql 28 | 29 | spring-boot-05-batch 30 | 31 | spring-boot-06-security 32 | 33 | spring-boot-07-messaging 34 | 35 | spring-boot-08-search 36 | 37 | spring-boot-09-ai 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /spring-boot-01-basic/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | spring-boot-examples 9 | com.louis 10 | 1.0 11 | 12 | 13 | spring-boot-01-basic 14 | pom 15 | 16 | Spring Boot入门包括基础配置 17 | 18 | 19 | 20 | spring-boot-helloWorld 21 | 22 | spring-boot-banner 23 | 24 | spring-boot-properties 25 | 26 | spring-boot-yaml 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-banner/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-01-basic 7 | 1.0 8 | 9 | 10 | spring-boot-banner 11 | 12 | 13 | UTF-8 14 | 17 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-maven-plugin 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-banner/src/main/java/com/louis/springbootbanner/SpringBootBannerApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootbanner; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootBannerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootBannerApplication.class, args); 11 | } 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-banner/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-banner/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ___ 2 | | _ ) __ _ _ _ _ _ ___ _ _ 3 | | _ \ / _` | | ' \ | ' \ / -_) | '_| 4 | |___/ \__,_| |_||_| |_||_| \___| _|_|_ 5 | _|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| 6 | "`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-' 7 | 8 | ${spring-boot.version} 9 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-helloWorld/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-boot-01-basic 7 | com.louis 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | spring-boot-helloWorld 13 | 14 | 15 | 16 | 17 | UTF-8 18 | 17 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-helloWorld/src/main/java/com/louis/springboothelloWorld/SpringBootHelloWorldApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springboothelloWorld; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @SpringBootApplication 10 | @RestController 11 | public class SpringBootHelloWorldApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringBootHelloWorldApplication.class, args); 15 | } 16 | 17 | @GetMapping("/hello") 18 | public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { 19 | return String.format("Hello %s!", name); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-helloWorld/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-helloWorld/src/test/java/com/louis/springboothelloWorld/SpringBootHelloWorldApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springboothelloWorld; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootHelloWorldApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | System.out.println("Hello Spring Boot"); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-properties/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-01-basic 7 | 1.0 8 | 9 | spring-boot-properties 10 | 11 | 12 | UTF-8 13 | 17 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-properties/src/main/java/com/louis/springbootproperties/SpringBootPropertiesApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootproperties; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @SpringBootApplication 10 | @RestController 11 | public class SpringBootPropertiesApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringBootPropertiesApplication.class, args); 15 | } 16 | 17 | @GetMapping("/hello") 18 | public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { 19 | return String.format("Hello %s!", name); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-properties/src/main/java/com/louis/springbootproperties/entity/Properties1.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootproperties.entity; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | 10 | @Component 11 | @ConfigurationProperties(prefix = "com.louis") 12 | public class Properties1 { 13 | private String name; 14 | private Map user; 15 | private List type; 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | public Map getUser() { 26 | return user; 27 | } 28 | 29 | public void setUser(Map user) { 30 | this.user = user; 31 | } 32 | 33 | public List getType() { 34 | return type; 35 | } 36 | 37 | public void setType(List type) { 38 | this.type = type; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "PropertiesEntity{" + 44 | "name='" + name + '\'' + 45 | ", user=" + user + 46 | ", type=" + type + 47 | '}'; 48 | } 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-properties/src/main/java/com/louis/springbootproperties/entity/Properties2.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootproperties.entity; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | /** 7 | * 第二种方式:使用@Value注解方式(该方式仅适用于String类型) 8 | */ 9 | @Component 10 | public class Properties2 { 11 | 12 | @Value("${com.louis.name}") 13 | private String name; 14 | 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "PropertiesEntity1{" + 27 | "name='" + name + '\'' + 28 | '}'; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-properties/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ###\u81EA\u5B9A\u4E49\u914D\u7F6E\u5C5E\u6027\u503C 2 | #string 3 | com.louis.name=SpringBoot\u901A\u8FC7properties\u65B9\u5F0F\u53D6\u503C 4 | #map 5 | com.louis.user[username]=admin 6 | com.louis.user[password]=123456 7 | #list 8 | com.louis.type[0]=properties 9 | com.louis.type[1]=yaml 10 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-properties/src/test/java/com/louis/springbootproperties/SpringBootPropertiesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootproperties; 2 | 3 | import com.louis.springbootproperties.entity.Properties1; 4 | import com.louis.springbootproperties.entity.Properties2; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.core.env.Environment; 9 | 10 | @SpringBootTest 11 | class SpringBootPropertiesApplicationTests { 12 | 13 | 14 | @Autowired 15 | Properties1 properties1; 16 | 17 | @Autowired 18 | Properties2 properties2; 19 | 20 | 21 | @Autowired 22 | Environment environment; 23 | 24 | @Test 25 | void contextLoads() { 26 | System.out.println(properties1.toString()); 27 | System.out.println(properties2.toString()); 28 | System.out.println("name=" + environment.getProperty("com.louis.name") + ",username=" + environment.getProperty("com.louis.user[username]")); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-yaml/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-01-basic 7 | 1.0 8 | 9 | spring-boot-yaml 10 | 11 | 12 | UTF-8 13 | 17 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-test 24 | test 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-maven-plugin 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-yaml/src/main/java/com/louis/springbootyaml/SpringBootYamlApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootyaml; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @SpringBootApplication 10 | @RestController 11 | public class SpringBootYamlApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringBootYamlApplication.class, args); 15 | } 16 | 17 | @GetMapping("/hello") 18 | public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { 19 | return String.format("Hello %s!", name); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-yaml/src/main/java/com/louis/springbootyaml/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootyaml.entity; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author Louis 11 | * @title: People 12 | * @projectName springbootstudy 13 | * @description: TODO 14 | * @date 2019/5/14 14:41 15 | */ 16 | @Component 17 | @ConfigurationProperties(prefix = "com.people") 18 | public class People { 19 | 20 | private String name; 21 | private String petName; 22 | private Map user; 23 | private List type; 24 | private Pet pet; 25 | 26 | @Override 27 | public String toString() { 28 | return "Louis{" + 29 | "name='" + name + '\'' + 30 | ", petName='" + petName + '\'' + 31 | ", user=" + user + 32 | ", type=" + type + 33 | ", pet=" + pet + 34 | '}'; 35 | } 36 | 37 | public String getPetName() { 38 | return petName; 39 | } 40 | 41 | public void setPetName(String petName) { 42 | this.petName = petName; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public Map getUser() { 54 | return user; 55 | } 56 | 57 | public void setUser(Map user) { 58 | this.user = user; 59 | } 60 | 61 | public List getType() { 62 | return type; 63 | } 64 | 65 | public void setType(List type) { 66 | this.type = type; 67 | } 68 | 69 | public Pet getPet() { 70 | return pet; 71 | } 72 | 73 | public void setPet(Pet pet) { 74 | this.pet = pet; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-yaml/src/main/java/com/louis/springbootyaml/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootyaml.entity; 2 | 3 | /** 4 | * @author Louis 5 | * @title: Pet 6 | * @projectName springbootstudy 7 | * @description: TODO 8 | * @date 2019/5/14 14:42 9 | */ 10 | public class Pet { 11 | 12 | private String breed; 13 | private String name; 14 | 15 | @Override 16 | public String toString() { 17 | return "Pet{" + 18 | "breed='" + breed + '\'' + 19 | ", name='" + name + '\'' + 20 | '}'; 21 | } 22 | 23 | public String getBreed() { 24 | return breed; 25 | } 26 | 27 | public void setBreed(String breed) { 28 | this.breed = breed; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-yaml/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | ###自定义配置属性值 2 | #配置文件赋值 3 | com: 4 | people: 5 | #String 6 | name: SpringBoot通过yaml方式取值 7 | petName: 旺财 8 | #map 9 | user: {username: admin,password: 123456} 10 | #list 11 | type: [properties,yaml] 12 | #Object 13 | pet: 14 | breed: dog 15 | #配置文件取值 16 | name: ${com.people.petName} -------------------------------------------------------------------------------- /spring-boot-01-basic/spring-boot-yaml/src/test/java/com/louis/springbootyaml/SpringBootYamlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootyaml; 2 | 3 | import com.louis.springbootyaml.entity.People; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | @SpringBootTest 13 | class SpringBootYamlApplicationTests { 14 | 15 | @Autowired 16 | People people; 17 | 18 | 19 | 20 | @Test 21 | void contextLoads() { 22 | System.out.println(people.toString()); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | spring-boot-examples 7 | com.louis 8 | 1.0 9 | 10 | 11 | spring-boot-02-templateEngines 12 | pom 13 | 14 | Spring Boot整合模版引擎 15 | 16 | 17 | 18 | spring-boot-thymeleaf 19 | 20 | spring-boot-freemarker 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-02-templateEngines 8 | 1.0 9 | 10 | 11 | spring-boot-freemarker 12 | 13 | 17 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-freemarker 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-maven-plugin 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/src/main/java/com/louis/springbootfreemarker/SpringBootFreemarkerApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootfreemarker; 2 | 3 | import com.louis.springbootfreemarker.entity.People; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.ui.Model; 9 | import org.springframework.ui.ModelMap; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.servlet.ModelAndView; 12 | 13 | /** 14 | * @author Louis 15 | */ 16 | @SpringBootApplication 17 | @Controller 18 | public class SpringBootFreemarkerApplication { 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(SpringBootFreemarkerApplication.class, args); 22 | } 23 | 24 | @Autowired 25 | People people; 26 | 27 | 28 | @RequestMapping("/index1") 29 | public String index1(Model model) { 30 | model.addAttribute("name", "Model方式"); 31 | model.addAttribute("people", people); 32 | return "index"; 33 | } 34 | 35 | @RequestMapping("/index2") 36 | public String index2(ModelMap modelMap) { 37 | modelMap.addAttribute("name", "ModelMap方式"); 38 | modelMap.addAttribute("people", people); 39 | return "index"; 40 | } 41 | 42 | @RequestMapping("/index3") 43 | public ModelAndView index3(ModelAndView modelAndView) { 44 | modelAndView.addObject("name", "ModelAndView方式"); 45 | modelAndView.addObject("people", people); 46 | modelAndView.setViewName("index"); 47 | return modelAndView; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/src/main/java/com/louis/springbootfreemarker/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootfreemarker.entity; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author Louis 11 | * @title: People 12 | * @projectName springbootstudy 13 | * @description: TODO 14 | */ 15 | @Component 16 | @ConfigurationProperties(prefix = "com.people") 17 | public class People { 18 | 19 | private String name; 20 | private String petName; 21 | private Map user; 22 | private List type; 23 | private Pet pet; 24 | private Integer age; 25 | 26 | @Override 27 | public String toString() { 28 | return "People{" + 29 | "name='" + name + '\'' + 30 | ", petName='" + petName + '\'' + 31 | ", user=" + user + 32 | ", type=" + type + 33 | ", pet=" + pet + 34 | ", age=" + age + 35 | '}'; 36 | } 37 | 38 | public Integer getAge() { 39 | return age; 40 | } 41 | 42 | public void setAge(Integer age) { 43 | this.age = age; 44 | } 45 | 46 | public String getPetName() { 47 | return petName; 48 | } 49 | 50 | public void setPetName(String petName) { 51 | this.petName = petName; 52 | } 53 | 54 | public String getName() { 55 | return name; 56 | } 57 | 58 | public void setName(String name) { 59 | this.name = name; 60 | } 61 | 62 | public Map getUser() { 63 | return user; 64 | } 65 | 66 | public void setUser(Map user) { 67 | this.user = user; 68 | } 69 | 70 | public List getType() { 71 | return type; 72 | } 73 | 74 | public void setType(List type) { 75 | this.type = type; 76 | } 77 | 78 | public Pet getPet() { 79 | return pet; 80 | } 81 | 82 | public void setPet(Pet pet) { 83 | this.pet = pet; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/src/main/java/com/louis/springbootfreemarker/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootfreemarker.entity; 2 | 3 | /** 4 | * @author Louis 5 | * @title: Pet 6 | * @projectName springbootstudy 7 | * @description: TODO 8 | */ 9 | public class Pet { 10 | 11 | private String breed; 12 | private String name; 13 | 14 | @Override 15 | public String toString() { 16 | return "Pet{" + 17 | "breed='" + breed + '\'' + 18 | ", name='" + name + '\'' + 19 | '}'; 20 | } 21 | 22 | public String getBreed() { 23 | return breed; 24 | } 25 | 26 | public void setBreed(String breed) { 27 | this.breed = breed; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setName(String name) { 35 | this.name = name; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | 2 | ###freemarker静态资源配置 3 | spring: 4 | freemarker: 5 | #设定ftl文件路径 6 | template-loader-path: classpath:/templates/ 7 | cache: false 8 | charset: UTF-8 9 | #是否检查模版位置 10 | check-template-location: true 11 | content-type: text/html 12 | #是否将HttpServletRequest中的属性添加到Model中 13 | expose-request-attributes: false 14 | #是否将HttpSession中的属性添加到Model中 15 | expose-session-attributes: false 16 | request-context-attribute: request 17 | suffix: .ftl 18 | 19 | 20 | ###自定义配置属性值 21 | #配置文件赋值 22 | com: 23 | people: 24 | #String 25 | name: SpringBoot通过yaml方式取值 26 | petName: 旺财 27 | age: 10 28 | #map 29 | user: {username: admin,password: 123456} 30 | #list 31 | type: [properties,yaml] 32 | #Object 33 | pet: 34 | breed: dog 35 | #配置文件取值 36 | name: ${com.people.petName} 37 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/src/main/resources/templates/index.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 36 | 37 | 38 | 39 | 43 | 44 | 45 | 46 | 49 | 50 |
freemarker模版引擎:${name}
输出对象属性值:name:${people.getName()}
petName:${people.getPetName()}
返回字符串长度name长度:${people.getName()?length}
输出集合遍历: 27 | <#list people.getType() as type>${type}
28 |
输出map属性: 33 | ${people.getUser().username}
34 | ${people.getUser()['password']?c} 35 |
算术运算符: 40 | 直接运算:${1+2}
41 | 获取数据后再进行运算:${people.getAge() + 10} 42 |
比较运算符: 47 | 直接运算:<#if 1 gt 2>true<#else>false
48 |
51 | 52 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-freemarker/src/test/java/com/louis/springbootfreemark/SpringBootFreemarkApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootfreemark; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootFreemarkApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-02-templateEngines 7 | 1.0 8 | 9 | 10 | spring-boot-thymeleaf 11 | 12 | 13 | 17 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-thymeleaf 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-test 27 | test 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/src/main/java/com/louis/springbootthymeleaf/SpringBootThymeleafApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootthymeleaf; 2 | 3 | import com.louis.springbootthymeleaf.entity.People; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.ui.Model; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | 11 | @SpringBootApplication 12 | @Controller 13 | public class SpringBootThymeleafApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(SpringBootThymeleafApplication.class, args); 17 | } 18 | 19 | 20 | @Autowired 21 | People people; 22 | 23 | 24 | @GetMapping("/index") 25 | public String hello(Model model) { 26 | model.addAttribute("name", "name"); 27 | model.addAttribute("people", people.toString()); 28 | model.addAttribute("peopleType", people.getType()); 29 | return "index"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/src/main/java/com/louis/springbootthymeleaf/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootthymeleaf.entity; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author Louis 11 | * @title: People 12 | * @projectName springbootstudy 13 | * @description: TODO 14 | * @date 2019/5/14 14:41 15 | */ 16 | @Component 17 | @ConfigurationProperties(prefix = "com.people") 18 | public class People { 19 | 20 | private String name; 21 | private String petName; 22 | private Map user; 23 | private List type; 24 | private Pet pet; 25 | 26 | @Override 27 | public String toString() { 28 | return "Louis{" + 29 | "name='" + name + '\'' + 30 | ", petName='" + petName + '\'' + 31 | ", user=" + user + 32 | ", type=" + type + 33 | ", pet=" + pet + 34 | '}'; 35 | } 36 | 37 | public String getPetName() { 38 | return petName; 39 | } 40 | 41 | public void setPetName(String petName) { 42 | this.petName = petName; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public Map getUser() { 54 | return user; 55 | } 56 | 57 | public void setUser(Map user) { 58 | this.user = user; 59 | } 60 | 61 | public List getType() { 62 | return type; 63 | } 64 | 65 | public void setType(List type) { 66 | this.type = type; 67 | } 68 | 69 | public Pet getPet() { 70 | return pet; 71 | } 72 | 73 | public void setPet(Pet pet) { 74 | this.pet = pet; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/src/main/java/com/louis/springbootthymeleaf/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootthymeleaf.entity; 2 | 3 | /** 4 | * @author Louis 5 | * @title: Pet 6 | * @projectName springbootstudy 7 | * @description: TODO 8 | * @date 2019/5/14 14:42 9 | */ 10 | public class Pet { 11 | 12 | private String breed; 13 | private String name; 14 | 15 | @Override 16 | public String toString() { 17 | return "Pet{" + 18 | "breed='" + breed + '\'' + 19 | ", name='" + name + '\'' + 20 | '}'; 21 | } 22 | 23 | public String getBreed() { 24 | return breed; 25 | } 26 | 27 | public void setBreed(String breed) { 28 | this.breed = breed; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | thymeleaf: 3 | mode: HTML 4 | #编码 5 | encoding: UTF-8 6 | #关闭模版缓存,即时刷新 7 | cache: false 8 | 9 | ###自定义配置属性值 10 | #配置文件赋值 11 | com: 12 | people: 13 | #String 14 | name: SpringBoot通过yaml方式取值 15 | petName: 旺财 16 | age: 10 17 | #map 18 | user: {username: admin,password: 123456} 19 | #list 20 | type: [properties,yaml] 21 | #Object 22 | pet: 23 | breed: dog 24 | #配置文件取值 25 | name: ${com.people.petName} -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 首页 6 | 7 | 8 |

hello,Thymeleaf

9 | 10 |

11 | 12 |

13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /spring-boot-02-templateEngines/spring-boot-thymeleaf/src/test/java/com/louis/springbootthymeleaf/SpringBootThymeleafApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootthymeleaf; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootThymeleafApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-03-sql/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | 10 | 11 | spring-boot-03-sql 12 | pom 13 | 14 | Spring Boot整合SQL数据库篇 15 | 16 | 17 | 18 | spring-boot-jpa 19 | 20 | spring-boot-mybatis 21 | 22 | spring-boot-mybatis-annotation 23 | 24 | spring-boot-mybatis-plus 25 | 26 | spring-boot-mybatis-flex 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-03-sql 8 | 1.0 9 | 10 | 11 | spring-boot-jpa 12 | 13 | 14 | 17 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-data-jpa 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | com.mysql 30 | mysql-connector-j 31 | runtime 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/main/java/com/louis/springbootjpa/SpringBootJpaApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootjpa; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * @author Louis 8 | */ 9 | @SpringBootApplication 10 | 11 | public class SpringBootJpaApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringBootJpaApplication.class, args); 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/main/java/com/louis/springbootjpa/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootjpa.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import jakarta.persistence.*; 5 | 6 | import java.io.Serializable; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @author Louis 12 | * @title: People 13 | * @projectName springbootstudy 14 | * @description: TODO 15 | */ 16 | @Entity 17 | @Table(name = "people") 18 | public class People implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | private Integer id; 23 | private String name; 24 | private Integer age; 25 | @OneToMany(mappedBy = "people", fetch = FetchType.EAGER) 26 | @JsonIgnoreProperties(value="people") 27 | private List petList = new ArrayList<>(); 28 | ; 29 | 30 | @Override 31 | public String toString() { 32 | return "People{" + 33 | "id=" + id + 34 | ", name='" + name + '\'' + 35 | ", age=" + age + 36 | ", petList=" + petList + 37 | '}'; 38 | } 39 | 40 | public void setId(Integer id) { 41 | this.id = id; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public void setAge(Integer age) { 49 | this.age = age; 50 | } 51 | 52 | public void setPetList(List petList) { 53 | this.petList = petList; 54 | } 55 | 56 | public Integer getId() { 57 | return id; 58 | } 59 | 60 | public String getName() { 61 | return name; 62 | } 63 | 64 | public Integer getAge() { 65 | return age; 66 | } 67 | 68 | public List getPetList() { 69 | return petList; 70 | } 71 | 72 | ; 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/main/java/com/louis/springbootjpa/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootjpa.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import jakarta.persistence.*; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Louis 10 | * @title: Pet 11 | * @projectName springbootstudy 12 | * @description: TODO 13 | */ 14 | @Entity 15 | @Table(name = "pet") 16 | public class Pet implements Serializable { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Integer id; 21 | @Column(name = "pet_name") 22 | private String petName; 23 | @ManyToOne 24 | @JoinColumn(name = "people_id") 25 | @JsonIgnoreProperties(value="petList") 26 | private People people; 27 | 28 | 29 | @Override 30 | public String toString() { 31 | return "Pet{" + 32 | "id=" + id + 33 | ", petName='" + petName + '\'' + 34 | '}'; 35 | } 36 | 37 | public void setId(Integer id) { 38 | this.id = id; 39 | } 40 | 41 | public void setPetName(String petName) { 42 | this.petName = petName; 43 | } 44 | 45 | public void setPeople(People people) { 46 | this.people = people; 47 | } 48 | 49 | public Integer getId() { 50 | return id; 51 | } 52 | 53 | public String getPetName() { 54 | return petName; 55 | } 56 | 57 | public People getPeople() { 58 | return people; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/main/java/com/louis/springbootjpa/repository/PeopleRepository.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootjpa.repository; 2 | 3 | import com.louis.springbootjpa.entity.People; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Louis 8 | * @title: PeopleRepository 9 | * @projectName springbootstudy 10 | * @description: TODO 11 | * @date 2019/5/27 15:18 12 | */ 13 | public interface PeopleRepository extends JpaRepository { 14 | } 15 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/main/java/com/louis/springbootjpa/repository/PetRepository.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootjpa.repository; 2 | 3 | import com.louis.springbootjpa.entity.Pet; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Louis 8 | * @title: PetRepository 9 | * @projectName springbootstudy 10 | * @description: TODO 11 | * @date 2019/5/27 15:30 12 | */ 13 | public interface PetRepository extends JpaRepository { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # 数据库配置 2 | spring: 3 | datasource: 4 | driver-class-name: com.mysql.cj.jdbc.Driver 5 | url: jdbc:mysql://localhost:3306/springboot_examples?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai 6 | username: root 7 | password: 123456 8 | 9 | jpa: 10 | database: mysql 11 | show-sql: true 12 | #读取实体类column注解数据 13 | hibernate: 14 | #JPA映射策略:不做修改,直接映射 15 | naming: 16 | physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 17 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-jpa/src/test/java/com/louis/springbootjpa/SpringBootJpaApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootjpa; 2 | 3 | import com.louis.springbootjpa.entity.People; 4 | import com.louis.springbootjpa.entity.Pet; 5 | import com.louis.springbootjpa.repository.PeopleRepository; 6 | import com.louis.springbootjpa.repository.PetRepository; 7 | import org.junit.jupiter.api.Test; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | 11 | import java.util.List; 12 | 13 | @SpringBootTest 14 | class SpringBootJpaApplicationTests { 15 | 16 | @Autowired 17 | private PeopleRepository peopleRepository; 18 | 19 | @Autowired 20 | private PetRepository petRepository; 21 | 22 | 23 | @Test 24 | void contextLoads() { 25 | for (People p : peopleRepository.findAll()) { 26 | System.out.println(p.toString()); 27 | } 28 | } 29 | 30 | public void addPeople() { 31 | People People = new People(); 32 | People.setName("经理"); 33 | People.setAge(18); 34 | peopleRepository.save(People); 35 | } 36 | 37 | public void addPet() { 38 | Pet Pet = new Pet(); 39 | Pet.setPetName("旺旺"); 40 | Pet.setPeople(peopleRepository.getOne(1)); 41 | petRepository.save(Pet); 42 | } 43 | 44 | public List getPeople() { 45 | return peopleRepository.findAll(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-03-sql 8 | 1.0 9 | 10 | spring-boot-mybatis-annotation 11 | Demo project for Spring Boot 12 | 13 | 17 14 | 3.0.2 15 | 16 | 17 | 18 | org.mybatis.spring.boot 19 | mybatis-spring-boot-starter 20 | ${mybatis-spring-boot-starter.version} 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | com.mysql 30 | mysql-connector-j 31 | runtime 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-annotation/src/main/java/com/louis/springbootmybatisannotation/SpringBootMybatisAnnotationApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisannotation; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMybatisAnnotationApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMybatisAnnotationApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-annotation/src/main/java/com/louis/springbootmybatisannotation/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisannotation.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author Louis 7 | * @title: Pet 8 | * @projectName 9 | */ 10 | public class Pet implements Serializable { 11 | 12 | private Integer id; 13 | private String petName; 14 | private People people; 15 | 16 | 17 | @Override 18 | public String toString() { 19 | return "Pet{" + 20 | "id=" + id + 21 | ", petName='" + petName + '\'' + 22 | '}'; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public void setPetName(String petName) { 30 | this.petName = petName; 31 | } 32 | 33 | public void setPeople(People people) { 34 | this.people = people; 35 | } 36 | 37 | public Integer getId() { 38 | return id; 39 | } 40 | 41 | public String getPetName() { 42 | return petName; 43 | } 44 | 45 | public People getPeople() { 46 | return people; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-annotation/src/main/java/com/louis/springbootmybatisannotation/mapper/PeopleMapper.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisannotation.mapper; 2 | 3 | import com.louis.springbootmybatisannotation.entity.People; 4 | import org.apache.ibatis.annotations.*; 5 | 6 | import java.util.List; 7 | 8 | @Mapper 9 | public interface PeopleMapper { 10 | 11 | 12 | @Insert("") 13 | Integer save(People people); 14 | 15 | @Delete("delete from people where id = #{id}") 16 | void delete(@Param("id") Integer id); 17 | 18 | @Update("") 21 | void update(People people); 22 | 23 | @Select("") 24 | List findById(@Param("id") Integer id); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-annotation/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://localhost:3306/springboot_examples?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai 5 | username: root 6 | password: 123456 -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-annotation/src/test/java/com/louis/springbootmybatisannotation/SpringBootMybatisAnnotationApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisannotation; 2 | 3 | import com.louis.springbootmybatisannotation.entity.People; 4 | import com.louis.springbootmybatisannotation.mapper.PeopleMapper; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.util.List; 10 | 11 | @SpringBootTest 12 | class SpringBootMybatisAnnotationApplicationTests { 13 | 14 | 15 | @Autowired 16 | private PeopleMapper peopleMapper; 17 | 18 | @Test 19 | void contextLoads() { 20 | //addPeople(); 21 | updatePeople(); 22 | System.out.println(getPeople()); 23 | } 24 | 25 | 26 | public Integer addPeople() { 27 | People People = new People(); 28 | People.setName("经理"); 29 | People.setAge(18); 30 | return peopleMapper.save(People); 31 | } 32 | 33 | 34 | public void updatePeople() { 35 | People People = new People(); 36 | People.setName("update-man"); 37 | People.setAge(28); 38 | People.setId(2); 39 | peopleMapper.update(People); 40 | } 41 | 42 | public List getPeople() { 43 | return peopleMapper.findById(null); 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/mybatis-flex.config: -------------------------------------------------------------------------------- 1 | processor.mapper.generateEnable=true -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-03-sql 8 | 1.0 9 | 10 | spring-boot-mybatis-flex 11 | Demo project for Spring Boot 12 | 13 | 17 14 | 1.5.5 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.mybatis.spring.boot 25 | mybatis-spring-boot-starter 26 | 3.0.2 27 | 28 | 29 | 30 | com.mysql 31 | mysql-connector-j 32 | runtime 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | org.mybatis.spring.boot 41 | mybatis-spring-boot-starter-test 42 | 3.0.2 43 | test 44 | 45 | 46 | 47 | com.mybatis-flex 48 | mybatis-flex-spring-boot-starter 49 | ${com.mybatis-flex.version} 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/src/main/java/com/louis/springbootmybatisflex/SpringBootMybatisFlexApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisflex; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | @MapperScan("com.louis.springbootmybatisflex.mapper") 9 | public class SpringBootMybatisFlexApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootMybatisFlexApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/src/main/java/com/louis/springbootmybatisflex/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisflex.entity; 2 | 3 | import com.mybatisflex.annotation.Id; 4 | import com.mybatisflex.annotation.KeyType; 5 | import com.mybatisflex.annotation.Table; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * @author Louis 11 | * @title: Pet 12 | * @projectName 13 | */ 14 | @Table("pet") 15 | public class Pet implements Serializable { 16 | 17 | @Id(keyType = KeyType.Auto) 18 | private Integer id; 19 | private String petName; 20 | private People people; 21 | 22 | 23 | @Override 24 | public String toString() { 25 | return "Pet{" + 26 | "id=" + id + 27 | ", petName='" + petName + '\'' + 28 | '}'; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setPetName(String petName) { 36 | this.petName = petName; 37 | } 38 | 39 | public void setPeople(People people) { 40 | this.people = people; 41 | } 42 | 43 | public Integer getId() { 44 | return id; 45 | } 46 | 47 | public String getPetName() { 48 | return petName; 49 | } 50 | 51 | public People getPeople() { 52 | return people; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/src/main/java/com/louis/springbootmybatisflex/mapper/PeopleMapper.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisflex.mapper; 2 | 3 | import com.louis.springbootmybatisflex.entity.People; 4 | import com.mybatisflex.core.BaseMapper; 5 | import org.apache.ibatis.annotations.Mapper; 6 | 7 | @Mapper 8 | public interface PeopleMapper extends BaseMapper { 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://localhost:3306/springboot_examples?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai 5 | username: root 6 | password: 123456 -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-flex/src/test/java/com/louis/springbootmybatisflex/SpringBootMybatisFlexApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisflex; 2 | 3 | import com.louis.springbootmybatisflex.entity.People; 4 | import com.louis.springbootmybatisflex.mapper.PeopleMapper; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.util.List; 10 | 11 | @SpringBootTest 12 | class SpringBootMybatisFlexApplicationTests { 13 | 14 | @Autowired 15 | private PeopleMapper peopleMapper; 16 | 17 | @Test 18 | void contextLoads() { 19 | updatePeople(); 20 | for (People p : peopleMapper.selectAll()) { 21 | System.out.println(p.toString()); 22 | } 23 | } 24 | 25 | 26 | public Integer addPeople() { 27 | People People = new People(); 28 | People.setName("经理"); 29 | People.setAge(18); 30 | return peopleMapper.insert(People); 31 | } 32 | 33 | 34 | public void updatePeople() { 35 | People People = new People(); 36 | People.setName("update-man"); 37 | People.setId(2); 38 | peopleMapper.update(People); 39 | } 40 | 41 | public List getPeople() { 42 | return peopleMapper.selectAll(); 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-plus/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-03-sql 8 | 1.0 9 | 10 | spring-boot-mybatis-plus 11 | Demo project for Spring Boot 12 | 13 | 17 14 | 3.5.3.1 15 | 16 | 17 | 18 | 19 | com.mysql 20 | mysql-connector-j 21 | runtime 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | com.baomidou 30 | mybatis-plus-boot-starter 31 | ${mybatis-plus-boot-starter.version} 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-plus/src/main/java/com/louis/springbootmybatisplus/SpringBootMybatisPlusApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisplus; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMybatisPlusApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMybatisPlusApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-plus/src/main/java/com/louis/springbootmybatisplus/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisplus.entity; 2 | 3 | import com.baomidou.mybatisplus.annotation.TableId; 4 | import com.baomidou.mybatisplus.annotation.TableName; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author Louis 10 | * @title: Pet 11 | * @projectName 12 | * @description: TODO 13 | */ 14 | @TableName("pet") 15 | public class Pet implements Serializable { 16 | 17 | @TableId 18 | private Integer id; 19 | private String petName; 20 | private People people; 21 | 22 | 23 | @Override 24 | public String toString() { 25 | return "Pet{" + 26 | "id=" + id + 27 | ", petName='" + petName + '\'' + 28 | '}'; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setPetName(String petName) { 36 | this.petName = petName; 37 | } 38 | 39 | public void setPeople(People people) { 40 | this.people = people; 41 | } 42 | 43 | public Integer getId() { 44 | return id; 45 | } 46 | 47 | public String getPetName() { 48 | return petName; 49 | } 50 | 51 | public People getPeople() { 52 | return people; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-plus/src/main/java/com/louis/springbootmybatisplus/mapper/PeopleMapper.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisplus.mapper; 2 | 3 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; 4 | import com.louis.springbootmybatisplus.entity.People; 5 | import org.apache.ibatis.annotations.Mapper; 6 | 7 | @Mapper 8 | public interface PeopleMapper extends BaseMapper { 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-plus/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://localhost:3306/springboot_examples?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai 5 | username: root 6 | password: 123456 -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis-plus/src/test/java/com/louis/springbootmybatisplus/SpringBootMybatisPlusApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatisplus; 2 | 3 | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; 4 | import com.louis.springbootmybatisplus.entity.People; 5 | import com.louis.springbootmybatisplus.mapper.PeopleMapper; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | 10 | import java.util.List; 11 | 12 | @SpringBootTest 13 | class SpringBootMybatisPlusApplicationTests { 14 | 15 | @Autowired 16 | private PeopleMapper peopleMapper; 17 | 18 | @Test 19 | void contextLoads() { 20 | for (People p : peopleMapper.selectList(null)) { 21 | System.out.println(p.toString()); 22 | } 23 | } 24 | 25 | 26 | public Integer addPeople() { 27 | People People = new People(); 28 | People.setName("经理"); 29 | People.setAge(18); 30 | return peopleMapper.insert(People); 31 | } 32 | 33 | 34 | public void updatePeople() { 35 | People People = new People(); 36 | People.setName("update-man"); 37 | UpdateWrapper updateWrapper = new UpdateWrapper(); 38 | updateWrapper.eq("id", 2); 39 | peopleMapper.update(People, updateWrapper); 40 | } 41 | 42 | public List getPeople() { 43 | return peopleMapper.selectList(null); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-03-sql 8 | 1.0 9 | 10 | spring-boot-mybatis 11 | Demo project for Spring Boot 12 | 13 | 17 14 | 3.0.2 15 | 16 | 17 | 18 | 19 | org.mybatis.spring.boot 20 | mybatis-spring-boot-starter 21 | ${mybatis-spring-boot-starter.version} 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | com.mysql 31 | mysql-connector-j 32 | runtime 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/src/main/java/com/louis/springbootmybatis/SpringBootMybatisApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatis; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMybatisApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMybatisApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/src/main/java/com/louis/springbootmybatis/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatis.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author Louis 7 | * @title: Pet 8 | * @projectName 9 | * @description: TODO 10 | */ 11 | public class Pet implements Serializable { 12 | 13 | private Integer id; 14 | private String petName; 15 | private People people; 16 | 17 | 18 | @Override 19 | public String toString() { 20 | return "Pet{" + 21 | "id=" + id + 22 | ", petName='" + petName + '\'' + 23 | '}'; 24 | } 25 | 26 | public void setId(Integer id) { 27 | this.id = id; 28 | } 29 | 30 | public void setPetName(String petName) { 31 | this.petName = petName; 32 | } 33 | 34 | public void setPeople(People people) { 35 | this.people = people; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public String getPetName() { 43 | return petName; 44 | } 45 | 46 | public People getPeople() { 47 | return people; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/src/main/java/com/louis/springbootmybatis/mapper/PeopleMapper.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatis.mapper; 2 | 3 | import com.louis.springbootmybatis.entity.People; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import java.util.List; 8 | 9 | @Mapper 10 | public interface PeopleMapper { 11 | 12 | 13 | void save(People people); 14 | 15 | void delete(@Param("id") Integer id); 16 | 17 | void update(People people); 18 | 19 | List findById(@Param("id") Integer id); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.Driver 4 | url: jdbc:mysql://localhost:3306/springboot_examples?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai 5 | username: root 6 | password: 123456 7 | mybatis: 8 | type-aliases-package: com.louis.springbootmybatis.entity 9 | mapper-locations: classpath:mapper/*.xml -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/src/main/resources/mapper/PeopleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | id,name,age 15 | 16 | 17 | 28 | 29 | 30 | delete from people where id = #{id}; 31 | 32 | 33 | 34 | 35 | update people 36 | 37 | name=#{name}, 38 | 39 | where id=#{id} 40 | 41 | 42 | 43 | insert into people(name,age) VALUES (#{name},#{age}) 44 | 45 | 46 | -------------------------------------------------------------------------------- /spring-boot-03-sql/spring-boot-mybatis/src/test/java/com/louis/springbootmybatis/SpringBootMybatisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmybatis; 2 | 3 | import com.louis.springbootmybatis.entity.People; 4 | import com.louis.springbootmybatis.entity.Pet; 5 | import com.louis.springbootmybatis.mapper.PeopleMapper; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | 10 | import java.util.List; 11 | 12 | @SpringBootTest 13 | class SpringBootMybatisApplicationTests { 14 | 15 | 16 | @Autowired 17 | private PeopleMapper peopleMapper; 18 | 19 | @Test 20 | void contextLoads() { 21 | addPeople(); 22 | System.out.println(getPeople()); 23 | } 24 | 25 | 26 | public void addPeople() { 27 | People People = new People(); 28 | People.setName("经理"); 29 | People.setAge(18); 30 | peopleMapper.save(People); 31 | } 32 | 33 | public List getPeople() { 34 | return peopleMapper.findById(null); 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | 10 | 11 | spring-boot-04-nosql 12 | pom 13 | 14 | Spring Boot整合NoSQL及数据缓存 15 | 16 | 17 | 18 | spring-boot-redis 19 | 20 | spring-boot-redis-annotation 21 | 22 | spring-boot-mongodb 23 | 24 | spring-boot-ehcache 25 | 26 | spring-boot-redisson 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-04-nosql 8 | 1.0 9 | 10 | com.louis 11 | 12 | 13 | spring-boot-ehcache 14 | 0.0.1-SNAPSHOT 15 | spring-boot-mongodb 16 | Demo project for Spring Boot 17 | 18 | 17 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-cache 25 | 26 | 27 | 28 | 29 | org.ehcache 30 | ehcache 31 | 3.10.8 32 | jakarta 33 | 34 | 35 | 36 | 37 | jakarta.xml.bind 38 | jakarta.xml.bind-api 39 | 4.0.0 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-web 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | org.projectlombok 58 | lombok 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/java/com/louis/springbootehcache/SpringBootEhcacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootehcache; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cache.annotation.EnableCaching; 6 | 7 | @SpringBootApplication 8 | @EnableCaching 9 | public class SpringBootEhcacheApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootEhcacheApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/java/com/louis/springbootehcache/controller/PeopleController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootehcache.controller; 2 | 3 | import com.louis.springbootehcache.entity.People; 4 | import com.louis.springbootehcache.entity.Pet; 5 | import com.louis.springbootehcache.service.PeopleService; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | @RestController 9 | @RequestMapping("/api") 10 | public class PeopleController { 11 | 12 | private final PeopleService peopleService; 13 | 14 | public PeopleController(PeopleService peopleService) { 15 | this.peopleService = peopleService; 16 | } 17 | 18 | @PostMapping("/people") 19 | public People addPeople(@RequestBody People people) { 20 | return peopleService.addPeople(people); 21 | } 22 | 23 | @GetMapping("/people/{id}") 24 | public People getPeople(@PathVariable Integer id) { 25 | return peopleService.getPeopleById(id); 26 | } 27 | 28 | @PutMapping("/people") 29 | public People updatePeople(@RequestBody People people) { 30 | return peopleService.updatePeople(people); 31 | } 32 | 33 | @DeleteMapping("/people/{id}") 34 | public void deletePeople(@PathVariable Integer id) { 35 | peopleService.deletePeople(id); 36 | } 37 | 38 | @PostMapping("/pets") 39 | public Pet addPet(@RequestBody Pet pet) { 40 | return peopleService.addPet(pet); 41 | } 42 | 43 | @GetMapping("/pets/{id}") 44 | public Pet getPet(@PathVariable Integer id) { 45 | return peopleService.getPetById(id); 46 | } 47 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/java/com/louis/springbootehcache/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootehcache.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author Louis 9 | * @title: People 10 | * @projectName springbootstudy 11 | * @description: TODO 12 | */ 13 | public class People implements Serializable { 14 | 15 | private Integer id; 16 | private String name; 17 | private Integer age; 18 | private List petList = new ArrayList<>(); 19 | ; 20 | 21 | @Override 22 | public String toString() { 23 | return "People{" + 24 | "id=" + id + 25 | ", name='" + name + '\'' + 26 | ", age=" + age + 27 | ", petList=" + petList + 28 | '}'; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | public void setPetList(List petList) { 44 | this.petList = petList; 45 | } 46 | 47 | public Integer getId() { 48 | return id; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public Integer getAge() { 56 | return age; 57 | } 58 | 59 | public List getPetList() { 60 | return petList; 61 | } 62 | 63 | ; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/java/com/louis/springbootehcache/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootehcache.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author Louis 9 | * @title: Pet 10 | * @projectName springbootstudy 11 | */ 12 | public class Pet implements Serializable { 13 | 14 | private Integer id; 15 | private String petName; 16 | @JsonIgnore 17 | private People people; 18 | 19 | 20 | @Override 21 | public String toString() { 22 | return "Pet{" + 23 | "id=" + id + 24 | ", petName='" + petName + '\'' + 25 | '}'; 26 | } 27 | 28 | public void setId(Integer id) { 29 | this.id = id; 30 | } 31 | 32 | public void setPetName(String petName) { 33 | this.petName = petName; 34 | } 35 | 36 | public void setPeople(People people) { 37 | this.people = people; 38 | } 39 | 40 | public Integer getId() { 41 | return id; 42 | } 43 | 44 | public String getPetName() { 45 | return petName; 46 | } 47 | 48 | public People getPeople() { 49 | return people; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/java/com/louis/springbootehcache/service/PeopleService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootehcache.service; 2 | 3 | import com.louis.springbootehcache.entity.People; 4 | import com.louis.springbootehcache.entity.Pet; 5 | import org.springframework.cache.annotation.*; 6 | import org.springframework.stereotype.Service; 7 | import java.util.*; 8 | 9 | @Service 10 | @CacheConfig(cacheNames = "peopleCache") 11 | public class PeopleService { 12 | 13 | // 模拟数据库存储 14 | private final Map peopleDB = new HashMap<>(); 15 | private final Map petDB = new HashMap<>(); 16 | 17 | // 添加Person 18 | @CachePut(key = "#people.id") 19 | public People addPeople(People people) { 20 | peopleDB.put(people.getId(), people); 21 | return people; 22 | } 23 | 24 | // 查询Person 25 | @Cacheable(key = "#id") 26 | public People getPeopleById(Integer id) { 27 | simulateSlowQuery(); // 模拟耗时操作 28 | return peopleDB.get(id); 29 | } 30 | 31 | // 更新Person 32 | @CachePut(key = "#people.id") 33 | public People updatePeople(People people) { 34 | peopleDB.put(people.getId(), people); 35 | return people; 36 | } 37 | 38 | // 删除Person 39 | @CacheEvict(key = "#id") 40 | public void deletePeople(Integer id) { 41 | peopleDB.remove(id); 42 | } 43 | 44 | // 添加Pet 45 | @Cacheable(cacheNames = "petCache", key = "#pet.id") 46 | public Pet addPet(Pet pet) { 47 | petDB.put(pet.getId(), pet); 48 | return pet; 49 | } 50 | 51 | // 查询Pet 52 | @Cacheable(cacheNames = "petCache", key = "#id") 53 | public Pet getPetById(Integer id) { 54 | return petDB.get(id); 55 | } 56 | 57 | // 模拟数据库查询延迟 58 | private void simulateSlowQuery() { 59 | try { 60 | Thread.sleep(2000); 61 | } catch (InterruptedException e) { 62 | e.printStackTrace(); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spring-boot-ehcache 2 | # ??EhCache?? 3 | spring.cache.type=jcache 4 | spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider 5 | spring.cache.jcache.config=classpath:ehcache.xml -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-ehcache/src/main/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | java.lang.Integer 5 | com.louis.springbootehcache.entity.People 6 | 7 | 10 8 | 9 | 1000 10 | 11 | 12 | 13 | 14 | java.lang.Integer 15 | com.louis.springbootehcache.entity.Pet 16 | 17 | 10 18 | 19 | 1000 20 | 21 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-04-nosql 8 | 1.0 9 | 10 | 11 | 12 | spring-boot-mongodb 13 | spring-boot-mongodb 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-data-mongodb 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.projectlombok 30 | lombok 31 | true 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/SpringBootMongodbApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmongodb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMongodbApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMongodbApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/controller/PeopleController.java: -------------------------------------------------------------------------------- 1 | // PeopleController.java 2 | package com.louis.springbootmongodb.controller; 3 | 4 | import com.louis.springbootmongodb.entity.People; 5 | import com.louis.springbootmongodb.service.PeopleService; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.*; 8 | import org.springframework.web.server.ResponseStatusException; 9 | 10 | import java.util.List; 11 | 12 | @RestController 13 | @RequestMapping("/api/people") 14 | public class PeopleController { 15 | private final PeopleService peopleService; 16 | 17 | public PeopleController(PeopleService peopleService) { 18 | this.peopleService = peopleService; 19 | } 20 | 21 | @PostMapping 22 | @ResponseStatus(HttpStatus.CREATED) 23 | public People create(@RequestBody People people) { 24 | return peopleService.create(people); 25 | } 26 | 27 | @GetMapping 28 | public List getAll() { 29 | return peopleService.findAll(); 30 | } 31 | 32 | @GetMapping("/{id}") 33 | public People getById(@PathVariable Integer id) { 34 | return peopleService.findById(id) 35 | .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); 36 | } 37 | 38 | @PutMapping("/{id}") 39 | public People update(@PathVariable Integer id, @RequestBody People people) { 40 | return peopleService.update(id, people) 41 | .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); 42 | } 43 | 44 | @DeleteMapping("/{id}") 45 | @ResponseStatus(HttpStatus.NO_CONTENT) 46 | public void delete(@PathVariable Integer id) { 47 | peopleService.delete(id); 48 | } 49 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/controller/PetController.java: -------------------------------------------------------------------------------- 1 | // PetController.java 2 | package com.louis.springbootmongodb.controller; 3 | 4 | import com.louis.springbootmongodb.entity.Pet; 5 | import com.louis.springbootmongodb.service.PetService; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.web.bind.annotation.*; 8 | import org.springframework.web.server.ResponseStatusException; 9 | 10 | import java.util.List; 11 | 12 | @RestController 13 | @RequestMapping("/api/pets") 14 | public class PetController { 15 | private final PetService petService; 16 | 17 | public PetController(PetService petService) { 18 | this.petService = petService; 19 | } 20 | 21 | @PostMapping 22 | @ResponseStatus(HttpStatus.CREATED) 23 | public Pet create(@RequestBody Pet pet) { 24 | return petService.savePet(pet); 25 | } 26 | 27 | @GetMapping 28 | public List getAll() { 29 | return petService.findAll(); 30 | } 31 | 32 | @GetMapping("/{id}") 33 | public Pet getById(@PathVariable Integer id) { 34 | return petService.findById(id) 35 | .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); 36 | } 37 | 38 | @DeleteMapping("/{id}") 39 | @ResponseStatus(HttpStatus.NO_CONTENT) 40 | public void delete(@PathVariable Integer id) { 41 | petService.delete(id); 42 | } 43 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmongodb.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonManagedReference; 4 | import lombok.Data; 5 | import org.springframework.data.annotation.Id; 6 | import org.springframework.data.mongodb.core.mapping.Document; 7 | 8 | import java.io.Serializable; 9 | import java.util.List; 10 | 11 | /** 12 | * @author Louis 13 | * @title: People 14 | * @projectName springboot-chapter 15 | */ 16 | @Data 17 | @Document(value = "people") 18 | public class People implements Serializable { 19 | 20 | @Id 21 | private Integer id; 22 | private String name; 23 | private Integer age; 24 | @JsonManagedReference 25 | private List petList; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmongodb.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import lombok.Data; 5 | import org.springframework.data.annotation.Id; 6 | import org.springframework.data.mongodb.core.mapping.Document; 7 | 8 | import java.io.Serializable; 9 | 10 | /** 11 | * @author Louis 12 | * @title: Pet 13 | * @projectName springboot-chapter 14 | */ 15 | @Data 16 | @Document(value = "pet") 17 | public class Pet implements Serializable { 18 | 19 | @Id 20 | private Integer id; 21 | private String petName; 22 | @JsonIgnoreProperties(value = "petList") 23 | private People people; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/repository/PeopleRepository.java: -------------------------------------------------------------------------------- 1 | // PeopleRepository.java 2 | package com.louis.springbootmongodb.repository; 3 | 4 | import com.louis.springbootmongodb.entity.People; 5 | import org.springframework.data.mongodb.repository.MongoRepository; 6 | 7 | public interface PeopleRepository extends MongoRepository { 8 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/repository/PetRepository.java: -------------------------------------------------------------------------------- 1 | // PetRepository.java 2 | package com.louis.springbootmongodb.repository; 3 | 4 | import com.louis.springbootmongodb.entity.Pet; 5 | import org.springframework.data.mongodb.repository.MongoRepository; 6 | 7 | public interface PetRepository extends MongoRepository { 8 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/service/PeopleService.java: -------------------------------------------------------------------------------- 1 | // PeopleService.java 2 | package com.louis.springbootmongodb.service; 3 | 4 | import com.louis.springbootmongodb.entity.People; 5 | import com.louis.springbootmongodb.entity.Pet; 6 | import com.louis.springbootmongodb.repository.PeopleRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | import java.util.Optional; 12 | 13 | @Service 14 | public class PeopleService { 15 | @Autowired 16 | private PeopleRepository peopleRepository; 17 | 18 | @Autowired 19 | private PetService petService; 20 | 21 | // 创建用户并处理宠物关联 22 | public People create(People people) { 23 | People savedPeople = peopleRepository.save(people); 24 | if (savedPeople.getPetList() != null) { 25 | savedPeople.getPetList().forEach(pet -> { 26 | pet.setPeople(savedPeople); 27 | petService.savePet(pet); 28 | }); 29 | } 30 | return savedPeople; 31 | } 32 | 33 | // 更新用户时同步更新宠物 34 | public Optional update(Integer id, People updatedPeople) { 35 | return peopleRepository.findById(id).map(existing -> { 36 | existing.setName(updatedPeople.getName()); 37 | existing.setAge(updatedPeople.getAge()); 38 | 39 | // 清除旧宠物关联 40 | existing.getPetList().forEach(pet -> pet.setPeople(null)); 41 | petService.deleteAllByPeople(existing); 42 | 43 | // 设置新宠物关联 44 | if (updatedPeople.getPetList() != null) { 45 | updatedPeople.getPetList().forEach(pet -> { 46 | pet.setPeople(existing); 47 | petService.savePet(pet); 48 | }); 49 | existing.setPetList(updatedPeople.getPetList()); 50 | } 51 | return peopleRepository.save(existing); 52 | }); 53 | } 54 | 55 | // 其他基础方法 56 | public List findAll() { 57 | return peopleRepository.findAll(); 58 | } 59 | 60 | public Optional findById(Integer id) { 61 | return peopleRepository.findById(id); 62 | } 63 | 64 | public void delete(Integer id) { 65 | peopleRepository.findById(id).ifPresent(people -> { 66 | petService.deleteAllByPeople(people); 67 | peopleRepository.deleteById(id); 68 | }); 69 | } 70 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/java/com/louis/springbootmongodb/service/PetService.java: -------------------------------------------------------------------------------- 1 | // PetService.java 2 | package com.louis.springbootmongodb.service; 3 | 4 | import com.louis.springbootmongodb.entity.People; 5 | import com.louis.springbootmongodb.entity.Pet; 6 | import com.louis.springbootmongodb.repository.PetRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | import java.util.Optional; 12 | 13 | @Service 14 | public class PetService { 15 | @Autowired 16 | private PetRepository petRepository; 17 | 18 | public Pet savePet(Pet pet) { 19 | return petRepository.save(pet); 20 | } 21 | 22 | public List findAll() { 23 | return petRepository.findAll(); 24 | } 25 | 26 | public Optional findById(Integer id) { 27 | return petRepository.findById(id); 28 | } 29 | 30 | public void delete(Integer id) { 31 | petRepository.deleteById(id); 32 | } 33 | 34 | public void deleteAllByPeople(People people) { 35 | petRepository.deleteAll(people.getPetList()); 36 | } 37 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | data: 3 | mongodb: 4 | host: 81.68.220.176 5 | port: 27017 6 | username: root 7 | password: '123456' 8 | database: admin 9 | jackson: 10 | date-format: yyyy-MM-dd HH:mm:ss 11 | time-zone: GMT+8 12 | 13 | logging: 14 | level: 15 | org.springframework.data.mongodb.core.MongoTemplate: DEBUG #配置MongoTemplate日志 -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-mongodb/src/test/java/com/louis/springbootmongodb/SpringBootMongodbApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootmongodb; 2 | 3 | import com.louis.springbootmongodb.entity.People; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.data.mongodb.core.FindAndModifyOptions; 8 | import org.springframework.data.mongodb.core.MongoTemplate; 9 | import org.springframework.data.mongodb.core.query.Criteria; 10 | import org.springframework.data.mongodb.core.query.Query; 11 | import org.springframework.data.mongodb.core.query.Update; 12 | 13 | import java.util.List; 14 | 15 | @SpringBootTest 16 | class SpringBootMongodbApplicationTests { 17 | 18 | 19 | @Autowired 20 | private MongoTemplate mongoTemplate; 21 | 22 | @Test 23 | void contextLoads() { 24 | System.out.println(findAndModifyPeople()); 25 | removePeople(); 26 | } 27 | 28 | 29 | public void addPeople() { 30 | People people = new People(); 31 | people.setId(2); 32 | people.setName("Louis"); 33 | people.setAge(18); 34 | mongoTemplate.insert(people); 35 | } 36 | 37 | 38 | public People selectPeople() { 39 | Query queryOne = new Query(Criteria.where("id").is(1)); 40 | People one = mongoTemplate.findOne(queryOne, People.class); 41 | return one; 42 | } 43 | 44 | 45 | public List selectPeopleList() { 46 | //查询List 47 | Query queryList = new Query(Criteria.where("name").is("Louis").and("age").is(18)); 48 | List userList = mongoTemplate.find(queryList, People.class); 49 | return userList; 50 | } 51 | 52 | 53 | public People findAndModifyPeople() { 54 | Query query = new Query(Criteria.where("id").is(2)); 55 | Update update = new Update(); 56 | update.set("age", 25); 57 | FindAndModifyOptions options = new FindAndModifyOptions(); 58 | // 先查询,如果没有符合条件的,会执行插入,插入的值是查询值 + 更新值 59 | options.upsert(true); 60 | // 返回当前最新值 61 | options.returnNew(true); 62 | return mongoTemplate.findAndModify(query, update, options, People.class); 63 | } 64 | 65 | 66 | public void removePeople() { 67 | Query query = new Query(Criteria.where("id").is("2")); 68 | mongoTemplate.remove(query, People.class); 69 | } 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-04-nosql 8 | 1.0 9 | 10 | 11 | 12 | spring-boot-redis-annotation 13 | spring-boot-redis-annotation 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-redis 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-maven-plugin 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/main/java/com/louis/springbootredisannotation/SpringBootRedisAnnotationApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisannotation; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRedisAnnotationApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRedisAnnotationApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/main/java/com/louis/springbootredisannotation/conf/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisannotation.conf; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.redis.connection.RedisConnectionFactory; 6 | import org.springframework.data.redis.core.RedisTemplate; 7 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 8 | import org.springframework.data.redis.serializer.StringRedisSerializer; 9 | 10 | @Configuration 11 | public class RedisConfig { 12 | 13 | @Bean 14 | public RedisTemplate redisTemplate(RedisConnectionFactory factory) { 15 | RedisTemplate template = new RedisTemplate(); 16 | template.setConnectionFactory(factory); 17 | // key采用String的序列化方式 18 | template.setKeySerializer(new StringRedisSerializer()); 19 | // hash的key也采用String的序列化方式 20 | template.setHashKeySerializer(new StringRedisSerializer()); 21 | // value序列化方式采用jackson 22 | template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); 23 | // hash的value序列化方式采用jackson 24 | template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); 25 | template.afterPropertiesSet(); 26 | return template; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/main/java/com/louis/springbootredisannotation/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisannotation.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author Louis 9 | * @title: People 10 | * @projectName springbootstudy 11 | * @description: TODO 12 | */ 13 | public class People implements Serializable { 14 | 15 | private Integer id; 16 | private String name; 17 | private Integer age; 18 | private List petList = new ArrayList<>(); 19 | ; 20 | 21 | @Override 22 | public String toString() { 23 | return "People{" + 24 | "id=" + id + 25 | ", name='" + name + '\'' + 26 | ", age=" + age + 27 | ", petList=" + petList + 28 | '}'; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | public void setPetList(List petList) { 44 | this.petList = petList; 45 | } 46 | 47 | public Integer getId() { 48 | return id; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public Integer getAge() { 56 | return age; 57 | } 58 | 59 | public List getPetList() { 60 | return petList; 61 | } 62 | 63 | ; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/main/java/com/louis/springbootredisannotation/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisannotation.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author Louis 9 | * @title: Pet 10 | * @projectName springbootstudy 11 | * @description: TODO 12 | */ 13 | public class Pet implements Serializable { 14 | 15 | private Integer id; 16 | private String petName; 17 | @JsonIgnoreProperties(value="petList") 18 | private People people; 19 | 20 | 21 | @Override 22 | public String toString() { 23 | return "Pet{" + 24 | "id=" + id + 25 | ", petName='" + petName + '\'' + 26 | '}'; 27 | } 28 | 29 | public void setId(Integer id) { 30 | this.id = id; 31 | } 32 | 33 | public void setPetName(String petName) { 34 | this.petName = petName; 35 | } 36 | 37 | public void setPeople(People people) { 38 | this.people = people; 39 | } 40 | 41 | public Integer getId() { 42 | return id; 43 | } 44 | 45 | public String getPetName() { 46 | return petName; 47 | } 48 | 49 | public People getPeople() { 50 | return people; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/main/java/com/louis/springbootredisannotation/service/PeopleService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisannotation.service; 2 | 3 | import com.louis.springbootredisannotation.entity.People; 4 | import org.springframework.cache.annotation.CachePut; 5 | import org.springframework.cache.annotation.Cacheable; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | @Service 12 | public class PeopleService { 13 | 14 | // 模拟数据库存储 15 | private Map PeopleMap = new HashMap(); 16 | 17 | public void savePeople(People People) { 18 | // 模拟数据库插入操作 19 | PeopleMap.put(People.getName(), People); 20 | } 21 | 22 | @Cacheable(value = "basePeopleInfo") 23 | public People getPeopleByName(String peopleName) { 24 | // 模拟数据库查询并返回 25 | return PeopleMap.get(peopleName); 26 | } 27 | 28 | @CachePut(value = "basePeopleInfo") 29 | public void updatePeopleAge(String peopleName, Integer age) { 30 | People people = PeopleMap.get(peopleName); 31 | people.setAge(age); 32 | // 模拟更新数据库 33 | PeopleMap.put(peopleName, people); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | data: 3 | redis: 4 | host: 127.0.0.1 5 | port: 6379 -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis-annotation/src/test/java/com/louis/springbootredisannotation/SpringBootRedisAnnotationApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisannotation; 2 | 3 | import com.louis.springbootredisannotation.entity.People; 4 | import com.louis.springbootredisannotation.service.PeopleService; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | @SpringBootTest 10 | class SpringBootRedisAnnotationApplicationTests { 11 | 12 | 13 | @Autowired 14 | private PeopleService peopleService; 15 | 16 | @Test 17 | void contextLoads() { 18 | People people = new People(); 19 | people.setName("Louis"); 20 | people.setAge(18); 21 | // 向 redis 中存入数据 22 | peopleService.savePeople(people); 23 | 24 | // 从 redis 中取数据 25 | People peopleInfo = peopleService.getPeopleByName(people.getName()); 26 | System.out.println("查询:" + peopleInfo); 27 | 28 | // 更新 people 的描述信息后查询 29 | peopleService.updatePeopleAge(people.getName(), 20); 30 | peopleInfo = peopleService.getPeopleByName(people.getName()); 31 | System.out.println("更新后查询:" + peopleInfo); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-04-nosql 8 | 1.0 9 | 10 | 11 | spring-boot-redis 12 | Demo project for Spring Boot 13 | 14 | 17 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-data-redis 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/src/main/java/com/louis/springbootredis/SpringBootRedisApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredis; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRedisApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRedisApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/src/main/java/com/louis/springbootredis/conf/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredis.conf; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.redis.connection.RedisConnectionFactory; 6 | import org.springframework.data.redis.core.RedisTemplate; 7 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 8 | import org.springframework.data.redis.serializer.StringRedisSerializer; 9 | 10 | @Configuration 11 | public class RedisConfig { 12 | 13 | @Bean 14 | public RedisTemplate redisTemplate(RedisConnectionFactory factory) { 15 | RedisTemplate template = new RedisTemplate(); 16 | template.setConnectionFactory(factory); 17 | // key采用String的序列化方式 18 | template.setKeySerializer(new StringRedisSerializer()); 19 | // hash的key也采用String的序列化方式 20 | template.setHashKeySerializer(new StringRedisSerializer()); 21 | // value序列化方式采用jackson 22 | template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); 23 | // hash的value序列化方式采用jackson 24 | template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); 25 | template.afterPropertiesSet(); 26 | return template; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/src/main/java/com/louis/springbootredis/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author Louis 9 | * @title: People 10 | * @projectName springbootstudy 11 | * @description: TODO 12 | */ 13 | public class People implements Serializable { 14 | 15 | private Integer id; 16 | private String name; 17 | private Integer age; 18 | private List petList = new ArrayList<>(); 19 | ; 20 | 21 | @Override 22 | public String toString() { 23 | return "People{" + 24 | "id=" + id + 25 | ", name='" + name + '\'' + 26 | ", age=" + age + 27 | ", petList=" + petList + 28 | '}'; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | public void setPetList(List petList) { 44 | this.petList = petList; 45 | } 46 | 47 | public Integer getId() { 48 | return id; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public Integer getAge() { 56 | return age; 57 | } 58 | 59 | public List getPetList() { 60 | return petList; 61 | } 62 | 63 | ; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/src/main/java/com/louis/springbootredis/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredis.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @author Louis 9 | * @title: Pet 10 | * @projectName springbootstudy 11 | * @description: TODO 12 | */ 13 | public class Pet implements Serializable { 14 | 15 | private Integer id; 16 | private String petName; 17 | @JsonIgnoreProperties(value="petList") 18 | private People people; 19 | 20 | 21 | @Override 22 | public String toString() { 23 | return "Pet{" + 24 | "id=" + id + 25 | ", petName='" + petName + '\'' + 26 | '}'; 27 | } 28 | 29 | public void setId(Integer id) { 30 | this.id = id; 31 | } 32 | 33 | public void setPetName(String petName) { 34 | this.petName = petName; 35 | } 36 | 37 | public void setPeople(People people) { 38 | this.people = people; 39 | } 40 | 41 | public Integer getId() { 42 | return id; 43 | } 44 | 45 | public String getPetName() { 46 | return petName; 47 | } 48 | 49 | public People getPeople() { 50 | return people; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | data: 3 | redis: 4 | host: 127.0.0.1 5 | port: 6379 -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redis/src/test/java/com/louis/springbootredis/SpringBootRedisApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredis; 2 | 3 | import com.louis.springbootredis.entity.People; 4 | import com.louis.springbootredis.util.RedisTemplateUtil; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.util.concurrent.TimeUnit; 10 | 11 | @SpringBootTest 12 | class SpringBootRedisApplicationTests { 13 | 14 | @Autowired 15 | private RedisTemplateUtil redisTemplateUtil; 16 | 17 | @Test 18 | void contextLoads() { 19 | People people = new People(); 20 | people.setName("Louis"); 21 | people.setAge(18); 22 | redisTemplateUtil.set("people", people); 23 | redisTemplateUtil.set("people.f", people, 20); 24 | boolean exists = redisTemplateUtil.hasKey("people"); 25 | if (exists) { 26 | System.out.println("exists is true"); 27 | System.out.println(redisTemplateUtil.get("people")); 28 | } else { 29 | System.out.println("exists is false"); 30 | } 31 | 32 | 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redisson/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-04-nosql 8 | 1.0 9 | 10 | com.louis 11 | 12 | spring-boot-redisson 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-redis 23 | 24 | 25 | 26 | org.redisson 27 | redisson-spring-boot-starter 28 | 3.23.4 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redisson/src/main/java/com/louis/springbootredisson/SpringBootRedissonApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisson; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRedissonApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRedissonApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redisson/src/main/java/com/louis/springbootredisson/config/RedissonConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisson.config; 2 | 3 | import org.redisson.Redisson; 4 | import org.redisson.api.RedissonClient; 5 | import org.redisson.config.Config; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration 11 | public class RedissonConfig { 12 | 13 | @Value("${spring.data.redis.host}") 14 | private String host; 15 | 16 | 17 | @Value("${spring.data.redis.port}") 18 | private String port; 19 | 20 | 21 | @Value("${spring.data.redis.password}") 22 | private String password; 23 | 24 | 25 | 26 | @Bean 27 | public RedissonClient redissonClient() { 28 | Config config = new Config(); 29 | config.useSingleServer() 30 | .setAddress("redis://" + System.getProperty("spring.redis.host", host) + ":" + System.getProperty("spring.redis.port", port)) 31 | .setPassword(System.getProperty("spring.redis.password", password)); 32 | return Redisson.create(config); 33 | } 34 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redisson/src/main/java/com/louis/springbootredisson/controller/LockController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisson.controller; 2 | 3 | import com.louis.springbootredisson.service.DistributedLockService; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.util.concurrent.TimeUnit; 9 | 10 | @RestController 11 | public class LockController { 12 | 13 | @Autowired 14 | private DistributedLockService lockService; 15 | 16 | 17 | @GetMapping("/lock") 18 | public String testLock() { 19 | String lockKey = "myLock"; 20 | try { 21 | if (lockService.tryLock(lockKey, 10, 30, TimeUnit.SECONDS)) { 22 | // 模拟业务逻辑 23 | Thread.sleep(5000); 24 | return "Lock acquired and processed"; 25 | } 26 | return "Failed to acquire lock"; 27 | } catch (InterruptedException e) { 28 | Thread.currentThread().interrupt(); 29 | return "Error occurred"; 30 | } finally { 31 | lockService.unlock(lockKey); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redisson/src/main/java/com/louis/springbootredisson/service/DistributedLockService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootredisson.service; 2 | 3 | import org.redisson.api.RLock; 4 | import org.redisson.api.RedissonClient; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.concurrent.TimeUnit; 9 | 10 | @Component 11 | public class DistributedLockService { 12 | 13 | @Autowired 14 | private RedissonClient redissonClient; 15 | 16 | // 获取锁 17 | public boolean tryLock(String lockKey, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException { 18 | RLock lock = redissonClient.getLock(lockKey); 19 | return lock.tryLock(waitTime, leaseTime, unit); 20 | } 21 | 22 | // 释放锁 23 | public void unlock(String lockKey) { 24 | RLock lock = redissonClient.getLock(lockKey); 25 | if (lock.isLocked() && lock.isHeldByCurrentThread()) { 26 | lock.unlock(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /spring-boot-04-nosql/spring-boot-redisson/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-redisson 4 | data: 5 | redis: 6 | host: localhost 7 | port: 6379 8 | password: 9 | 10 | 11 | singleServerConfig: 12 | address: "redis://${spring.data.redis.host}:${spring.data.redis.port}" 13 | password: ${spring.data.redis.password} -------------------------------------------------------------------------------- /spring-boot-05-batch/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | spring-boot-05-batch 10 | pom 11 | 12 | Spring Boot整合任务调度及批处理 13 | 14 | 15 | 16 | spring-boot-task 17 | 18 | spring-boot-quartz 19 | 20 | spring-boot-xxljob 21 | 22 | spring-boot-springbatch 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-quartz/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-05-batch 8 | 1.0 9 | 10 | com.louis 11 | 12 | spring-boot-quartz 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-quartz 23 | 24 | 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-maven-plugin 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-quartz/src/main/java/com/louis/springbootquartz/SpringBootQuartzApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootquartz; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootQuartzApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootQuartzApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-quartz/src/main/java/com/louis/springbootquartz/config/QuartzConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootquartz.config; 2 | 3 | import com.louis.springbootquartz.job.SimpleJob; 4 | import org.quartz.*; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | public class QuartzConfig { 10 | 11 | // 定义JobDetail 12 | @Bean 13 | public JobDetail simpleJobDetail() { 14 | return JobBuilder.newJob(SimpleJob.class) 15 | .withIdentity("simpleJob") // 任务唯一标识 16 | .storeDurably() // 持久化存储 17 | .build(); 18 | } 19 | 20 | // 定义Trigger 21 | @Bean 22 | public Trigger simpleJobTrigger() { 23 | SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule() 24 | .withIntervalInSeconds(5) // 每5秒执行一次 25 | .repeatForever(); 26 | 27 | return TriggerBuilder.newTrigger() 28 | .forJob(simpleJobDetail()) 29 | .withIdentity("simpleTrigger") 30 | .withSchedule(schedule) 31 | .build(); 32 | } 33 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-quartz/src/main/java/com/louis/springbootquartz/job/SimpleJob.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootquartz.job; 2 | 3 | import org.quartz.Job; 4 | import org.quartz.JobExecutionContext; 5 | import org.quartz.JobExecutionException; 6 | import java.time.LocalDateTime; 7 | 8 | public class SimpleJob implements Job { 9 | @Override 10 | public void execute(JobExecutionContext context) throws JobExecutionException { 11 | System.out.println("[Quartz Job] 执行时间:" + LocalDateTime.now()); 12 | } 13 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-quartz/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spring-boot-quartz 2 | # Quartz????? 3 | spring.quartz.job-store-type=jdbc 4 | spring.quartz.jdbc.initialize-schema=always 5 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-springbatch/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-05-batch 8 | 1.0 9 | 10 | com.louis 11 | 12 | spring-boot-springbatch 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-batch 23 | 24 | 25 | 26 | com.mysql 27 | mysql-connector-j 28 | runtime 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-data-jpa 34 | 35 | 36 | 37 | org.projectlombok 38 | lombok 39 | true 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-springbatch/src/main/java/com/louis/springbootspringbatch/SpringBootSpringbatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootspringbatch; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.JobParameters; 5 | import org.springframework.batch.core.launch.JobLauncher; 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 SpringBootSpringbatchApplication implements CommandLineRunner { 12 | 13 | private final JobLauncher jobLauncher; 14 | private final Job importTradeJob; 15 | 16 | public SpringBootSpringbatchApplication(JobLauncher jobLauncher, Job importTradeJob) { 17 | this.jobLauncher = jobLauncher; 18 | this.importTradeJob = importTradeJob; 19 | } 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(SpringBootSpringbatchApplication.class, args); 23 | } 24 | 25 | @Override 26 | public void run(String... args) throws Exception { 27 | jobLauncher.run(importTradeJob, new JobParameters()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-springbatch/src/main/java/com/louis/springbootspringbatch/model/Trade.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootspringbatch.model; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.Id; 5 | import lombok.Data; 6 | 7 | import java.math.BigDecimal; 8 | 9 | @Entity 10 | @Data 11 | public class Trade { 12 | @Id 13 | private String id; 14 | private String symbol; 15 | private BigDecimal price; 16 | private int quantity; 17 | 18 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-springbatch/src/main/java/com/louis/springbootspringbatch/repository/TradeRepository.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootspringbatch.repository; 2 | 3 | import com.louis.springbootspringbatch.model.Trade; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | public interface TradeRepository extends JpaRepository { 7 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-springbatch/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spring-boot-springbatch 2 | # MySQL????? 3 | spring.datasource.url=jdbc:mysql://localhost:3306/spring_batch_db?useSSL=false&serverTimezone=UTC 4 | spring.datasource.username=root 5 | spring.datasource.password=123456 6 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 7 | # JPA?? 8 | spring.jpa.hibernate.ddl-auto=update 9 | spring.jpa.show-sql=true 10 | # Spring Batch?? 11 | spring.batch.jdbc.initialize-schema=always 12 | spring.batch.job.enabled=false -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-springbatch/src/main/resources/trades.csv: -------------------------------------------------------------------------------- 1 | id,symbol,price,quantity 2 | 1,AAPL,150.25,100 3 | 2,GOOGL,2800.50,50 4 | 3,MSFT,300.75,200 -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-task/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-05-batch 8 | 1.0 9 | 10 | 11 | spring-boot-task 12 | Demo project for Spring Boot 13 | 14 | 17 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-task/src/main/java/com/louis/springboottask/SpringBootTaskApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springboottask; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.scheduling.annotation.EnableScheduling; 6 | 7 | 8 | @SpringBootApplication 9 | @EnableScheduling 10 | public class SpringBootTaskApplication { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(SpringBootTaskApplication.class, args); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-task/src/main/java/com/louis/springboottask/config/SchedulerConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springboottask.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.scheduling.annotation.SchedulingConfigurer; 6 | import org.springframework.scheduling.config.ScheduledTaskRegistrar; 7 | 8 | import java.util.concurrent.Executor; 9 | import java.util.concurrent.Executors; 10 | 11 | @Configuration 12 | public class SchedulerConfig implements SchedulingConfigurer { 13 | 14 | @Override 15 | public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 16 | taskRegistrar.setScheduler(taskExecutor()); 17 | } 18 | 19 | @Bean 20 | public Executor taskExecutor() { 21 | // 创建一个包含3个线程的调度线程池 22 | return Executors.newScheduledThreadPool(3); 23 | } 24 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-task/src/main/java/com/louis/springboottask/task/ScheduledTask.java: -------------------------------------------------------------------------------- 1 | package com.louis.springboottask.task; 2 | 3 | import org.springframework.scheduling.annotation.Scheduled; 4 | import org.springframework.stereotype.Component; 5 | import java.time.LocalDateTime; 6 | 7 | @Component 8 | public class ScheduledTask { 9 | 10 | /** 11 | * 固定速率:每隔5秒执行一次(从任务开始时间计算) 12 | */ 13 | @Scheduled(fixedRate = 5000) 14 | public void fixedRateTask() { 15 | System.out.println("[FixedRate] 当前时间:" + LocalDateTime.now()); 16 | } 17 | 18 | /** 19 | * 固定延迟:上一次任务结束后,延迟3秒执行 20 | */ 21 | @Scheduled(fixedDelay = 3000) 22 | public void fixedDelayTask() { 23 | try { 24 | Thread.sleep(2000); // 模拟任务耗时2秒 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | System.out.println("[FixedDelay] 当前时间:" + LocalDateTime.now()); 29 | } 30 | 31 | /** 32 | * Cron表达式:每分钟的第10秒执行 33 | */ 34 | @Scheduled(cron = "10 * * * * ?") 35 | public void cronTask() { 36 | System.out.println("[Cron] 当前时间:" + LocalDateTime.now()); 37 | } 38 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-task/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spring-boot-task 2 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-xxljob/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-05-batch 8 | 1.0 9 | 10 | com.louis 11 | 12 | spring-boot-xxljob 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | 21 | com.xuxueli 22 | xxl-job-core 23 | 2.4.0 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-maven-plugin 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-xxljob/src/main/java/com/louis/springbootxxljob/SpringBootXxljobApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootxxljob; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootXxljobApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootXxljobApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-xxljob/src/main/java/com/louis/springbootxxljob/config/XxlJobConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootxxljob.config; 2 | 3 | import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | public class XxlJobConfig { 10 | 11 | @Value("${xxl.job.admin.addresses}") 12 | private String adminAddresses; 13 | 14 | @Value("${xxl.job.executor.appname}") 15 | private String appName; 16 | 17 | @Value("${xxl.job.executor.port}") 18 | private int port; 19 | 20 | @Bean 21 | public XxlJobSpringExecutor xxlJobExecutor() { 22 | XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); 23 | xxlJobSpringExecutor.setAdminAddresses(adminAddresses); 24 | xxlJobSpringExecutor.setAppname(appName); 25 | xxlJobSpringExecutor.setPort(port); 26 | return xxlJobSpringExecutor; 27 | } 28 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-xxljob/src/main/java/com/louis/springbootxxljob/job/SampleJobHandler.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootxxljob.job; 2 | 3 | import com.xxl.job.core.context.XxlJobHelper; 4 | import com.xxl.job.core.handler.annotation.XxlJob; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class SampleJobHandler { 9 | 10 | /** 11 | * 简单定时任务 12 | */ 13 | @XxlJob("demoJobHandler") 14 | public void demoJobHandler() throws Exception { 15 | System.out.println("[XXL-JOB] 任务执行成功:" + System.currentTimeMillis()); 16 | } 17 | 18 | /** 19 | * 分片任务示例 20 | */ 21 | @XxlJob("shardingJobHandler") 22 | public void shardingJobHandler() throws Exception { 23 | // 获取分片参数 24 | int shardIndex = XxlJobHelper.getShardIndex(); 25 | int shardTotal = XxlJobHelper.getShardTotal(); 26 | 27 | System.out.printf("[分片任务] 当前分片:%d/%d %n", shardIndex, shardTotal); 28 | } 29 | 30 | /** 31 | * 任务参数传递 32 | */ 33 | @XxlJob("paramJobHandler") 34 | public void paramJobHandler() throws Exception { 35 | // 获取任务参数 36 | String jobParam = XxlJobHelper.getJobParam(); 37 | System.out.println("接收参数:" + jobParam); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /spring-boot-05-batch/spring-boot-xxljob/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spring-boot-xxljob 2 | # XXL-JOB?? 3 | xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin 4 | xxl.job.accessToken= 5 | xxl.job.executor.appname=xxl-job-executor-sample 6 | xxl.job.executor.address= 7 | xxl.job.executor.ip= 8 | xxl.job.executor.port=9999 9 | xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler 10 | xxl.job.executor.logretentiondays=30 -------------------------------------------------------------------------------- /spring-boot-06-security/README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot整合Spring Security实现安全管理 2 | 3 | ### 分享平台 4 | 5 | > CSDN博客:[https://blog.csdn.net/m0_37116405](https://blog.csdn.net/m0_37116405) 6 | 7 | > GitHub:[https://github.com/KissLouis](https://github.com/KissLouis) 8 | 9 | 10 | ### [点击进入Spring Security](https://github.com/KissLouis/SpringBoot-SpringSecurity) -------------------------------------------------------------------------------- /spring-boot-06-security/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | com.louis 10 | spring-boot-06-security 11 | 0.0.1-SNAPSHOT 12 | pom 13 | 14 | Spring Boot整合安全控制及权限篇 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | com.louis 10 | spring-boot-07-messaging 11 | 0.0.1-SNAPSHOT 12 | pom 13 | 14 | Spring Boot集成消息队列 15 | 16 | 17 | 18 | spring-boot-rocketmq 19 | 20 | spring-boot-rabbitmq 21 | 22 | spring-boot-activemq 23 | 24 | spring-boot-websocket 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-activemq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-07-messaging 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-activemq 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-activemq 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-maven-plugin 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-activemq/src/main/java/com/louis/springbootactivemq/SpringBootActivemqApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootactivemq; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.jms.annotation.EnableJms; 6 | 7 | @SpringBootApplication 8 | @EnableJms 9 | public class SpringBootActivemqApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SpringBootActivemqApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-activemq/src/main/java/com/louis/springbootactivemq/component/MessageConsumer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootactivemq.component; 2 | 3 | import org.springframework.jms.annotation.JmsListener; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class MessageConsumer { 8 | @JmsListener(destination = "${demo.queue}") 9 | public void receiveMessage(String message) { 10 | System.out.println("Received: " + message); 11 | } 12 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-activemq/src/main/java/com/louis/springbootactivemq/component/MessageProducer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootactivemq.component; 2 | 3 | import lombok.RequiredArgsConstructor; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.jms.core.JmsTemplate; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | @RequiredArgsConstructor 10 | public class MessageProducer { 11 | private final JmsTemplate jmsTemplate; 12 | 13 | @Value("${demo.queue}") 14 | private String queue; 15 | 16 | public void sendMessage(String message) { 17 | jmsTemplate.convertAndSend(queue, message); 18 | System.out.println("Sent: " + message); 19 | } 20 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-activemq/src/main/java/com/louis/springbootactivemq/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootactivemq.controller; 2 | 3 | import com.louis.springbootactivemq.component.MessageProducer; 4 | import lombok.RequiredArgsConstructor; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestParam; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | @RequiredArgsConstructor 11 | public class DemoController { 12 | private final MessageProducer producer; 13 | 14 | @GetMapping("/send") 15 | public String send(@RequestParam String msg) { 16 | producer.sendMessage(msg); 17 | return "Sent: " + msg; 18 | } 19 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-activemq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-activemq 4 | activemq: 5 | broker-url: tcp://localhost:61616 # ActiveMQ???? 6 | user: admin # ???????? 7 | password: admin # ??????? 8 | jms: 9 | pub-sub-domain: false # ???????true??????? 10 | 11 | # ??????? 12 | demo: 13 | queue: spring-boot-queue -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-07-messaging 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-rabbitmq 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-amqp 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-maven-plugin 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/SpringBootRabbitmqApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRabbitmqApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRabbitmqApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/config/RabbitMQConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq.config; 2 | 3 | import org.springframework.amqp.core.Binding; 4 | import org.springframework.amqp.core.BindingBuilder; 5 | import org.springframework.amqp.core.DirectExchange; 6 | import org.springframework.amqp.core.Queue; 7 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; 8 | import org.springframework.amqp.support.converter.MessageConverter; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | 12 | @Configuration 13 | public class RabbitMQConfig { 14 | 15 | @Bean 16 | public MessageConverter jsonMessageConverter() { 17 | return new Jackson2JsonMessageConverter(); 18 | } 19 | 20 | 21 | @Bean 22 | public Queue peopleQueue() { 23 | return new Queue("people_queue", true); // 持久化队列 24 | } 25 | 26 | @Bean 27 | public DirectExchange exchange() { 28 | return new DirectExchange("people_exchange"); 29 | } 30 | 31 | @Bean 32 | public Binding binding(Queue peopleQueue, DirectExchange exchange) { 33 | return BindingBuilder.bind(peopleQueue).to(exchange).with("people_routingKey"); 34 | } 35 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/consumer/MessageConsumer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq.consumer; 2 | 3 | import com.louis.springbootrabbitmq.entity.People; 4 | import org.springframework.amqp.rabbit.annotation.RabbitHandler; 5 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class MessageConsumer { 10 | 11 | @RabbitListener(queues = "people_queue") 12 | @RabbitHandler 13 | public void handleMessage(People people) { 14 | System.out.println("Received message: " + people); 15 | } 16 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq.controller; 2 | 3 | import com.louis.springbootrabbitmq.entity.People; 4 | import com.louis.springbootrabbitmq.producer.MessageProducer; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PostMapping; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class TestController { 12 | 13 | @Autowired 14 | private MessageProducer messageProducer; 15 | 16 | @PostMapping("/send") 17 | public String sendMessage(@RequestBody People people) { 18 | messageProducer.sendPeopleMessage(people); 19 | return "Message sent!"; 20 | } 21 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author Louis 9 | * @title: People 10 | * @projectName springbootstudy 11 | * @description: TODO 12 | */ 13 | public class People implements Serializable { 14 | 15 | private Integer id; 16 | private String name; 17 | private Integer age; 18 | private List petList = new ArrayList<>(); 19 | ; 20 | 21 | @Override 22 | public String toString() { 23 | return "People{" + 24 | "id=" + id + 25 | ", name='" + name + '\'' + 26 | ", age=" + age + 27 | ", petList=" + petList + 28 | '}'; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public void setAge(Integer age) { 40 | this.age = age; 41 | } 42 | 43 | public void setPetList(List petList) { 44 | this.petList = petList; 45 | } 46 | 47 | public Integer getId() { 48 | return id; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public Integer getAge() { 56 | return age; 57 | } 58 | 59 | public List getPetList() { 60 | return petList; 61 | } 62 | 63 | ; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * @author Louis 7 | * @title: Pet 8 | * @projectName springbootstudy 9 | * @description: TODO 10 | */ 11 | public class Pet implements Serializable { 12 | 13 | private Integer id; 14 | private String petName; 15 | private People people; 16 | 17 | 18 | @Override 19 | public String toString() { 20 | return "Pet{" + 21 | "id=" + id + 22 | ", petName='" + petName + '\'' + 23 | '}'; 24 | } 25 | 26 | public void setId(Integer id) { 27 | this.id = id; 28 | } 29 | 30 | public void setPetName(String petName) { 31 | this.petName = petName; 32 | } 33 | 34 | public void setPeople(People people) { 35 | this.people = people; 36 | } 37 | 38 | public Integer getId() { 39 | return id; 40 | } 41 | 42 | public String getPetName() { 43 | return petName; 44 | } 45 | 46 | public People getPeople() { 47 | return people; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/java/com/louis/springbootrabbitmq/producer/MessageProducer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrabbitmq.producer; 2 | 3 | import com.louis.springbootrabbitmq.entity.People; 4 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class MessageProducer { 10 | 11 | @Autowired 12 | private RabbitTemplate rabbitTemplate; 13 | 14 | public void sendPeopleMessage(People people) { 15 | rabbitTemplate.convertAndSend( 16 | "people_exchange", 17 | "people_routingKey", 18 | people 19 | ); 20 | } 21 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rabbitmq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-rabbitmq 4 | rabbitmq: 5 | host: 8.136.151.26 6 | port: 5672 7 | username: guest 8 | password: guest 9 | template: 10 | default-receive-queue: people_queue 11 | listener: 12 | simple: 13 | prefetch: 1 -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-07-messaging 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-rocketmq 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | org.apache.rocketmq 21 | rocketmq-spring-boot-starter 22 | 2.3.1 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | org.projectlombok 30 | lombok 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-maven-plugin 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/SpringBootRocketmqApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRocketmqApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRocketmqApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/config/MessageConverterConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.messaging.converter.MappingJackson2MessageConverter; 6 | import org.springframework.messaging.converter.MessageConverter; 7 | 8 | @Configuration 9 | public class MessageConverterConfig { 10 | 11 | @Bean 12 | public MessageConverter jacksonMessageConverter() { 13 | return new MappingJackson2MessageConverter(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/consumer/DemoMessageConsumer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.consumer; 2 | 3 | import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; 4 | import org.apache.rocketmq.spring.core.RocketMQListener; 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service 8 | @RocketMQMessageListener( 9 | topic = "demo-topic", 10 | consumerGroup = "${rocketmq.consumer.group}", 11 | selectorExpression = "*" // 消费所有tag的消息 12 | ) 13 | public class DemoMessageConsumer implements RocketMQListener { 14 | @Override 15 | public void onMessage(String message) { 16 | System.out.println("Received message: " + message); 17 | } 18 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/consumer/OrderMessageConsumer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.consumer; 2 | 3 | import com.louis.springbootrocketmq.dto.Order; 4 | import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; 5 | import org.apache.rocketmq.spring.core.RocketMQListener; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | @RocketMQMessageListener( 10 | topic = "order-topic", 11 | consumerGroup = "${rocketmq.consumer.group}" 12 | ) 13 | public class OrderMessageConsumer implements RocketMQListener { 14 | @Override 15 | public void onMessage(Order order) { 16 | System.out.println("Received order: " + order); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/consumer/TagAMessageConsumer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.consumer; 2 | 3 | import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; 4 | import org.apache.rocketmq.spring.core.RocketMQListener; 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service 8 | @RocketMQMessageListener( 9 | topic = "demo-topic", 10 | consumerGroup = "${rocketmq.consumer.group}", 11 | selectorExpression = "tagA" // 只消费tagA的消息 12 | ) 13 | public class TagAMessageConsumer implements RocketMQListener { 14 | @Override 15 | public void onMessage(String message) { 16 | System.out.println("Received TagA message: " + message); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.controller; 2 | 3 | import com.louis.springbootrocketmq.dto.Order; 4 | import com.louis.springbootrocketmq.producer.MessageProducer; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | import java.math.BigDecimal; 9 | 10 | @RestController 11 | public class TestController { 12 | private final MessageProducer messageProducer; 13 | 14 | public TestController(MessageProducer messageProducer) { 15 | this.messageProducer = messageProducer; 16 | } 17 | 18 | @GetMapping("/send") 19 | public String sendMessage() { 20 | messageProducer.sendSimpleMessage("Hello RocketMQ!"); 21 | messageProducer.sendMessageWithTag("TagA Message"); 22 | messageProducer.sendObjectMessage(new Order(1L, "20230520001", new BigDecimal("199.99"))); 23 | return "Messages sent!"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/dto/Order.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.math.BigDecimal; 8 | 9 | // 示例DTO 10 | @Data 11 | @AllArgsConstructor 12 | @NoArgsConstructor 13 | public class Order { 14 | private Long id; 15 | private String orderNo; 16 | private BigDecimal amount; 17 | } 18 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/producer/MessageProducer.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.producer; 2 | 3 | import com.louis.springbootrocketmq.dto.Order; 4 | import org.apache.rocketmq.spring.core.RocketMQTemplate; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class MessageProducer { 10 | 11 | @Autowired 12 | private RocketMQTemplate rocketMQTemplate; 13 | 14 | /** 15 | * 发送简单消息 16 | */ 17 | public void sendSimpleMessage(String message) { 18 | rocketMQTemplate.convertAndSend("demo-topic", message); 19 | } 20 | 21 | /** 22 | * 发送带tag的消息 23 | */ 24 | public void sendMessageWithTag(String message) { 25 | rocketMQTemplate.convertAndSend("demo-topic:tagA", message); 26 | } 27 | 28 | /** 29 | * 发送对象消息 30 | */ 31 | public void sendObjectMessage(Order order) { 32 | rocketMQTemplate.convertAndSend("order-topic", order); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/java/com/louis/springbootrocketmq/service/FileStorageService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootrocketmq.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | import org.springframework.web.multipart.MultipartFile; 5 | 6 | @Service 7 | public class FileStorageService { 8 | public String store(MultipartFile file) { 9 | // 实现文件存储逻辑 10 | return "File uploaded: " + file.getOriginalFilename(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-rocketmq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # application.yml 2 | spring: 3 | application: 4 | name: spring-boot-rocketmq 5 | 6 | rocketmq: 7 | name-server: 127.0.0.1:9876 # RocketMQ NameServer 8 | producer: 9 | group: demo-producer-group 10 | consumer: 11 | group: demo-consumer-group -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-websocket/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-07-messaging 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-websocket 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-websocket 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.projectlombok 29 | lombok 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-maven-plugin 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-websocket/src/main/java/com/louis/springbootwebsocket/SpringBootWebsocketApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootwebsocket; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootWebsocketApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootWebsocketApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-websocket/src/main/java/com/louis/springbootwebsocket/config/WebSocketConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootwebsocket.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.messaging.simp.config.MessageBrokerRegistry; 5 | import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; 6 | import org.springframework.web.socket.config.annotation.StompEndpointRegistry; 7 | import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; 8 | 9 | @Configuration 10 | @EnableWebSocketMessageBroker // 启用WebSocket消息代理 11 | public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { 12 | 13 | @Override 14 | public void configureMessageBroker(MessageBrokerRegistry config) { 15 | // 客户端订阅消息的前缀(服务端主动推送消息的路径) 16 | config.enableSimpleBroker("/topic"); 17 | // 客户端发送消息的前缀(服务端接收消息的路径) 18 | config.setApplicationDestinationPrefixes("/app"); 19 | } 20 | 21 | @Override 22 | public void registerStompEndpoints(StompEndpointRegistry registry) { 23 | // 注册WebSocket端点,客户端通过此地址连接 24 | registry.addEndpoint("/ws") 25 | .setAllowedOriginPatterns("*") // 允许跨域 26 | .withSockJS(); // 支持SockJS 27 | } 28 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-websocket/src/main/java/com/louis/springbootwebsocket/controller/WebSocketController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootwebsocket.controller; 2 | 3 | import org.springframework.messaging.handler.annotation.MessageMapping; 4 | import org.springframework.messaging.handler.annotation.SendTo; 5 | import org.springframework.stereotype.Controller; 6 | 7 | @Controller 8 | public class WebSocketController { 9 | 10 | // 接收客户端发送的消息,并广播给所有订阅者 11 | @MessageMapping("/sendMessage") 12 | @SendTo("/topic/messages") 13 | public String handleMessage(String message) { 14 | return "Server Response: " + message; 15 | } 16 | } -------------------------------------------------------------------------------- /spring-boot-07-messaging/spring-boot-websocket/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring.application.name=spring-boot-websocket 2 | -------------------------------------------------------------------------------- /spring-boot-08-search/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | com.louis 10 | spring-boot-08-search 11 | 0.0.1-SNAPSHOT 12 | pom 13 | 14 | Spring Boot整合搜索引擎 15 | 16 | 17 | 18 | spring-boot-elasticsearch 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-08-search 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-elasticsearch 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-data-elasticsearch 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-maven-plugin 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/src/main/java/com/louis/springbootelasticsearch/SpringBootElasticsearchApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootelasticsearch; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootElasticsearchApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootElasticsearchApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/src/main/java/com/louis/springbootelasticsearch/controller/PeopleController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootelasticsearch.controller; 2 | 3 | 4 | import com.louis.springbootelasticsearch.entity.People; 5 | import com.louis.springbootelasticsearch.repository.PeopleRepository; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | @RestController 10 | @RequestMapping("/api") 11 | public class PeopleController { 12 | @Autowired 13 | private PeopleRepository peopleRepository; 14 | 15 | @PostMapping("/people") 16 | public People createPeople(@RequestBody People people) { 17 | return peopleRepository.save(people); 18 | } 19 | 20 | @GetMapping("/people/{id}") 21 | public People getPeople(@PathVariable Integer id) { 22 | return peopleRepository.findById(id).orElse(null); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/src/main/java/com/louis/springbootelasticsearch/entity/People.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootelasticsearch.entity; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.elasticsearch.annotations.Document; 5 | 6 | import java.io.Serializable; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * @author Louis 12 | * @title: People 13 | * @projectName spring-boot-elasticsearch 14 | */ 15 | @Document(indexName = "people_index") 16 | public class People implements Serializable { 17 | 18 | @Id 19 | private Integer id; 20 | private String name; 21 | private Integer age; 22 | private List petList = new ArrayList<>(); 23 | ; 24 | 25 | @Override 26 | public String toString() { 27 | return "People{" + 28 | "id=" + id + 29 | ", name='" + name + '\'' + 30 | ", age=" + age + 31 | ", petList=" + petList + 32 | '}'; 33 | } 34 | 35 | public void setId(Integer id) { 36 | this.id = id; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public void setAge(Integer age) { 44 | this.age = age; 45 | } 46 | 47 | public void setPetList(List petList) { 48 | this.petList = petList; 49 | } 50 | 51 | public Integer getId() { 52 | return id; 53 | } 54 | 55 | public String getName() { 56 | return name; 57 | } 58 | 59 | public Integer getAge() { 60 | return age; 61 | } 62 | 63 | public List getPetList() { 64 | return petList; 65 | } 66 | 67 | ; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/src/main/java/com/louis/springbootelasticsearch/entity/Pet.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootelasticsearch.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import org.springframework.data.annotation.Id; 5 | import org.springframework.data.elasticsearch.annotations.Document; 6 | import org.springframework.data.elasticsearch.annotations.Field; 7 | import org.springframework.data.elasticsearch.annotations.FieldType; 8 | 9 | import java.io.Serializable; 10 | 11 | /** 12 | * @author Louis 13 | * @title: Pet 14 | * @projectName spring-boot-elasticsearch 15 | */ 16 | @Document(indexName = "pet_index") 17 | public class Pet implements Serializable { 18 | 19 | @Id 20 | private Integer id; 21 | @Field(type = FieldType.Text) 22 | private String petName; 23 | @JsonIgnoreProperties("petList") 24 | @Field(type = FieldType.Object, ignoreFields = {"petList"}) 25 | private People people; 26 | 27 | 28 | @Override 29 | public String toString() { 30 | return "Pet{" + 31 | "id=" + id + 32 | ", petName='" + petName + '\'' + 33 | '}'; 34 | } 35 | 36 | public void setId(Integer id) { 37 | this.id = id; 38 | } 39 | 40 | public void setPetName(String petName) { 41 | this.petName = petName; 42 | } 43 | 44 | public void setPeople(People people) { 45 | this.people = people; 46 | } 47 | 48 | public Integer getId() { 49 | return id; 50 | } 51 | 52 | public String getPetName() { 53 | return petName; 54 | } 55 | 56 | public People getPeople() { 57 | return people; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/src/main/java/com/louis/springbootelasticsearch/repository/PeopleRepository.java: -------------------------------------------------------------------------------- 1 | // PeopleRepository.java 2 | package com.louis.springbootelasticsearch.repository; 3 | 4 | import com.louis.springbootelasticsearch.entity.People; 5 | import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 6 | 7 | public interface PeopleRepository extends ElasticsearchRepository { 8 | } -------------------------------------------------------------------------------- /spring-boot-08-search/spring-boot-elasticsearch/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-elasticsearch 4 | data: 5 | elasticsearch: 6 | client: 7 | reactive: 8 | endpoints: localhost:9200 # ES????? 9 | connection-timeout: 5s # ?????? -------------------------------------------------------------------------------- /spring-boot-09-ai/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.louis 6 | spring-boot-examples 7 | 1.0 8 | 9 | com.louis 10 | spring-boot-09-ai 11 | 0.0.1-SNAPSHOT 12 | pom 13 | 14 | Spring Boot整合模型构建AI应用 15 | 16 | 17 | 18 | spring-boot-ollama 19 | 20 | spring-boot-deepseek 21 | 22 | spring-boot-openai 23 | 24 | spring-boot-qwen 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-09-ai 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-deepseek 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | UTF-8 18 | UTF-8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-webflux 25 | 26 | 27 | 28 | 29 | org.projectlombok 30 | lombok 31 | true 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | 43 | io.projectreactor 44 | reactor-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | org.projectlombok 60 | lombok 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/SpringBootDeepseekApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootDeepseekApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootDeepseekApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/config/DeepSeekConfig.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.reactive.function.client.WebClient; 7 | 8 | @Configuration 9 | public class DeepSeekConfig { 10 | 11 | @Value("${deepseek.api.key}") 12 | private String apiKey; 13 | 14 | @Value("${deepseek.api.base-url}") 15 | private String baseUrl; 16 | 17 | @Bean 18 | public WebClient deepSeekWebClient() { 19 | return WebClient.builder() 20 | .baseUrl(baseUrl) 21 | .defaultHeader("Authorization", "Bearer " + apiKey) 22 | .build(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/controller/DeepSeekController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.controller; 2 | 3 | import com.louis.springbootdeepseek.model.DeepSeekResponse; 4 | import com.louis.springbootdeepseek.service.DeepSeekService; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import reactor.core.publisher.Mono; 10 | 11 | @RestController 12 | @RequestMapping("/api/deepseek") 13 | public class DeepSeekController { 14 | 15 | private final DeepSeekService deepSeekService; 16 | 17 | public DeepSeekController(DeepSeekService deepSeekService) { 18 | this.deepSeekService = deepSeekService; 19 | } 20 | 21 | @PostMapping("/generate") 22 | public Mono generateText(@RequestBody String prompt) { 23 | return deepSeekService.generateText(prompt); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/model/Choice.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.model; 2 | 3 | 4 | import lombok.Data; 5 | 6 | @Data 7 | class Choice { 8 | private int index; 9 | private Message message; 10 | private String finish_reason; 11 | } -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/model/DeepSeekRequest.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.model; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.util.List; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @AllArgsConstructor 12 | public class DeepSeekRequest { 13 | private String model; 14 | private List messages; 15 | private double temperature; 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/model/DeepSeekResponse.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.model; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.List; 6 | 7 | @Data 8 | public class DeepSeekResponse { 9 | private String id; 10 | private String object; 11 | private long created; 12 | private String model; 13 | private List choices; 14 | private Usage usage; 15 | } -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/model/Message.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.model; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @NoArgsConstructor 9 | @AllArgsConstructor 10 | public class Message { 11 | private String role; 12 | private String content; 13 | } -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/model/Usage.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.model; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class Usage { 7 | private int prompt_tokens; 8 | private int completion_tokens; 9 | private int total_tokens; 10 | } 11 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/java/com/louis/springbootdeepseek/service/DeepSeekService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootdeepseek.service; 2 | 3 | import com.louis.springbootdeepseek.model.DeepSeekRequest; 4 | import com.louis.springbootdeepseek.model.DeepSeekResponse; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.web.reactive.function.client.WebClient; 10 | import reactor.core.publisher.Mono; 11 | 12 | import java.util.List; 13 | 14 | @Service 15 | public class DeepSeekService { 16 | 17 | private final WebClient webClient; 18 | 19 | @Value("${deepseek.api.model}") 20 | private String model; 21 | 22 | @Value("${deepseek.api.temperature}") 23 | private double temperature; 24 | 25 | @Autowired 26 | public DeepSeekService(WebClient webClient) { 27 | this.webClient = webClient; 28 | } 29 | 30 | public Mono generateText(String prompt) { 31 | DeepSeekRequest request = new DeepSeekRequest(); 32 | request.setModel(model); 33 | request.setTemperature(temperature); 34 | 35 | // 构建消息列表 36 | // 这里简化处理,实际使用时可能需要更复杂的消息结构 37 | // DeepSeek可能有特定的消息格式要求,需要根据其文档调整 38 | request.setMessages(List.of( 39 | new com.louis.springbootdeepseek.model.Message("user", prompt) 40 | )); 41 | 42 | return webClient.post() 43 | .uri("/v1/chat/completions") // 假设的API路径,实际需要根据DeepSeek文档调整 44 | .contentType(MediaType.APPLICATION_JSON) 45 | .bodyValue(request) 46 | .retrieve() 47 | .bodyToMono(DeepSeekResponse.class); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-deepseek/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-deepseek 4 | deepseek: 5 | api: 6 | key: your-api-key 7 | base-url: https://api.deepseek.com/v1 8 | timeout: 5000 9 | model: deepseek-chat 10 | temperature: 0.7 -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-ollama/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-09-ai 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-ollama 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | UTF-8 18 | UTF-8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-maven-plugin 35 | 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-ollama/src/main/java/com/louis/springbootollama/SpringBootOllamaApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootollama; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootOllamaApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootOllamaApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-ollama/src/main/java/com/louis/springbootollama/controller/OllamaController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootollama.controller; 2 | 3 | import com.louis.springbootollama.service.OllamaService; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | public class OllamaController { 10 | 11 | private final OllamaService ollamaService; 12 | 13 | public OllamaController(OllamaService ollamaService) { 14 | this.ollamaService = ollamaService; 15 | } 16 | 17 | @GetMapping("/generate") 18 | public String generate(@RequestParam String model, @RequestParam String prompt) { 19 | return ollamaService.generateResponse(model, prompt); 20 | } 21 | } -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-ollama/src/main/java/com/louis/springbootollama/service/OllamaService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootollama.service; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.http.HttpEntity; 5 | import org.springframework.http.HttpHeaders; 6 | import org.springframework.http.MediaType; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | @Service 14 | public class OllamaService { 15 | 16 | @Value("${ollama.api-url}") 17 | private String ollamaApiUrl; 18 | 19 | private final RestTemplate restTemplate = new RestTemplate(); 20 | 21 | public String generateResponse(String model, String prompt) { 22 | HttpHeaders headers = new HttpHeaders(); 23 | headers.setContentType(MediaType.APPLICATION_JSON); 24 | 25 | Map requestBody = new HashMap<>(); 26 | requestBody.put("model", model); 27 | requestBody.put("prompt", prompt); 28 | 29 | HttpEntity> request = new HttpEntity<>(requestBody, headers); 30 | 31 | Map response = restTemplate.postForObject(ollamaApiUrl, request, Map.class); 32 | return (String) response.get("response"); 33 | } 34 | } -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-ollama/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-ollama 4 | 5 | ollama: 6 | api-url: http://localhost:11434/api/generate -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-openai/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-09-ai 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-openai 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | UTF-8 18 | UTF-8 19 | 20 | 21 | 22 | 23 | com.theokanning.openai-gpt3-java 24 | service 25 | 0.11.1 26 | 27 | 28 | com.github.yulichang 29 | mybatis-plus-join-core 30 | 1.4.5 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | true 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | org.projectlombok 56 | lombok 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-openai/src/main/java/com/louis/springbootopenai/SpringBootOpenaiApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootopenai; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootOpenaiApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootOpenaiApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-openai/src/main/java/com/louis/springbootopenai/controller/OpenAIController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootopenai.controller; 2 | 3 | import com.louis.springbootopenai.service.OpenAIService; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | public class OpenAIController { 10 | 11 | private final OpenAIService openAIService; 12 | 13 | public OpenAIController(OpenAIService openAIService) { 14 | this.openAIService = openAIService; 15 | } 16 | 17 | @GetMapping("/openai/completion") 18 | public String getCompletion(@RequestParam String prompt) { 19 | return openAIService.getCompletion(prompt); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-openai/src/main/java/com/louis/springbootopenai/service/OpenAIService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootopenai.service; 2 | 3 | import com.theokanning.openai.completion.CompletionRequest; 4 | import com.theokanning.openai.service.OpenAiService; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class OpenAIService { 10 | 11 | private final OpenAiService openAiService; 12 | 13 | 14 | public OpenAIService(@Value("${openai.api-key}") String apiKey) { 15 | this.openAiService = new OpenAiService(apiKey); 16 | } 17 | 18 | public String getCompletion(String prompt) { 19 | CompletionRequest completionRequest = CompletionRequest.builder() 20 | .model("text-davinci-003") 21 | .prompt(prompt) 22 | .maxTokens(150) 23 | .temperature(0.7) 24 | .build(); 25 | return openAiService.createCompletion(completionRequest).getChoices().get(0).getText(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-openai/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-openai 4 | openai: 5 | api-key: your-openai-api-key 6 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-qwen/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.louis 7 | spring-boot-09-ai 8 | 0.0.1-SNAPSHOT 9 | 10 | com.louis 11 | 12 | spring-boot-openai 13 | 0.0.1-SNAPSHOT 14 | Demo project for Spring Boot 15 | 16 | 17 17 | UTF-8 18 | UTF-8 19 | 20 | 21 | 22 | 23 | org.springframework.ai 24 | spring-ai-openai-spring-boot-starter 25 | 0.8.1 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | org.projectlombok 40 | lombok 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-qwen/src/main/java/com/louis/springbootqwen/SpringBootQwenApplication.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootqwen; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootQwenApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootQwenApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-qwen/src/main/java/com/louis/springbootqwen/controller/AIController.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootqwen.controller; 2 | 3 | import com.louis.springbootqwen.service.QwenAIService; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestParam; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | public class AIController { 10 | 11 | private final QwenAIService qwenAIService; 12 | 13 | public AIController(QwenAIService qwenAIService) { 14 | this.qwenAIService = qwenAIService; 15 | } 16 | 17 | @GetMapping("/chat") 18 | public String chat(@RequestParam String message) { 19 | return qwenAIService.generateResponse(message); 20 | } 21 | } -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-qwen/src/main/java/com/louis/springbootqwen/service/QwenAIService.java: -------------------------------------------------------------------------------- 1 | package com.louis.springbootqwen.service; 2 | 3 | import org.springframework.ai.chat.ChatClient; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service 7 | public class QwenAIService { 8 | 9 | private final ChatClient chatClient; 10 | 11 | public QwenAIService(ChatClient chatClient) { 12 | this.chatClient = chatClient; 13 | } 14 | 15 | public String generateResponse(String prompt) { 16 | return chatClient.call(prompt); 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /spring-boot-09-ai/spring-boot-qwen/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: spring-boot-qwen 4 | ai: 5 | openai: 6 | base-url: https://dashscope.aliyuncs.com/compatible-mode/v1 # Qwen?API?? 7 | api-key: YOUR_API_KEY # ??????API-KEY 8 | chat: 9 | options: 10 | model: qwen-turbo # ??Qwen?? 11 | -------------------------------------------------------------------------------- /spring-boot-reference.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KissLouis/springboot-chapter/19bd5dcda98d411c5add4981ba64defbdfa65dd2/spring-boot-reference.pdf --------------------------------------------------------------------------------