SpringBoot入门系列:第四篇 redis ﹏ヽ暗。殇╰゛Y 2022-09-24 01:21 161阅读 0赞 ## 出于对作者的尊重和感谢,原文地址为 http://blog.csdn.net/lxhjh/article/details/51753852 ## ## 一,准备工作,建立spring-boot-sample-redis工程 ## 1、http://start.spring.io/ A、Artifact中输入spring-boot-sample-redis B、勾选Web下的web C、勾选NOSQL下的Redis 2、Eclips中导入工程spring-boot-sample-redis A、解压快捷工程spring-boot-sample-redis到某文件夹 B、eclips中file->import->Import Existing Maven Projects-->Select Maven projects-->finish导入工程 3、工程导入之后,文件结构如下图 ![Center][] 4、在包com.example下建立web文件夹 5、便于测试,引入spring-boot-sample-helloworld的HelloController及配置文件logback.xml HelloController代码为 **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.example.web; 2. 3. **import** org.slf4j.Logger; 4. **import** org.slf4j.LoggerFactory; 5. **import** org.springframework.web.bind.annotation.PathVariable; 6. **import** org.springframework.web.bind.annotation.RequestMapping; 7. **import** org.springframework.web.bind.annotation.RestController; 8. 9. @RestController 10. **public** **class** HelloController \{ 11. 12. **protected** **static** Logger logger=LoggerFactory.getLogger(HelloController.**class**); 13. 14. @RequestMapping("/") 15. **public** String helloworld()\{ 16. logger.debug("访问hello"); 17. **return** "Hello world!"; 18. \} 19. 20. @RequestMapping("/hello/\{name\}") 21. **public** String helloName(@PathVariable String name)\{ 22. logger.debug("访问helloName,Name=\{\}",name); 23. **return** "Hello "\+name; 24. \} 25. \} logback.xml配置为 **\[html\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **<****configuration****>** 2. <!-- %m输出的信息,%p日志级别,%t线程名,%d日期,%c类的全名,,,, --> 3. **<****appender** name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"**>** 4. **<****encoder****>** 5. **<****pattern****>**%d %p (%file:%line\\)- %m%n**</****pattern****>** 6. **<****charset****>**GBK**</****charset****>** 7. **</****encoder****>** 8. **</****appender****>** 9. **<****appender** name="baselog" 10. class="ch.qos.logback.core.rolling.RollingFileAppender"**>** 11. **<****File****>**log/base.log**</****File****>** 12. **<****rollingPolicy** class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"**>** 13. **<****fileNamePattern****>**log/base.log.%d.i%**</****fileNamePattern****>** 14. **<****timeBasedFileNamingAndTriggeringPolicy** class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"**>** 15. <!-- or whenever the file size reaches 64 MB --> 16. **<****maxFileSize****>**64 MB**</****maxFileSize****>** 17. **</****timeBasedFileNamingAndTriggeringPolicy****>** 18. **</****rollingPolicy****>** 19. **<****encoder****>** 20. **<****pattern****>** 21. %d %p (%file:%line\\)- %m%n 22. **</****pattern****>** 23. **<****charset****>**UTF-8**</****charset****>** <!-- 此处设置字符集 --> 24. **</****encoder****>** 25. **</****appender****>** 26. **<****root** level="info"**>** 27. **<****appender-ref** ref="STDOUT" **/>** 28. **</****root****>** 29. **<****logger** name="com.example" level="DEBUG"**>** 30. **<****appender-ref** ref="baselog" **/>** 31. **</****logger****>** 32. **</****configuration****>** 注:logback.xml文件位于src/main/resources下 6、启动工程,通过浏览器查看正确性 http://localhost:8080/ http://localhost:8080/hello/上帝 ## 二,使用StringRedisTemplate ## 1、web文件下建立class,类名为StringRedisController,编写StringRedisController为 **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.example.web; 2. 3. **import** javax.annotation.Resource; 4. 5. **import** org.slf4j.Logger; 6. **import** org.slf4j.LoggerFactory; 7. **import** org.springframework.beans.factory.annotation.Autowired; 8. **import** org.springframework.data.redis.core.StringRedisTemplate; 9. **import** org.springframework.data.redis.core.ValueOperations; 10. **import** org.springframework.web.bind.annotation.RequestMapping; 11. **import** org.springframework.web.bind.annotation.RestController; 12. 13. @RestController 14. **public** **class** StringRedisController \{ 15. 16. **protected** **static** Logger logger=LoggerFactory.getLogger(StringRedisController.**class**); 17. 18. @Autowired 19. StringRedisTemplate stringRedisTemplate; 20. 21. @Resource(name="stringRedisTemplate") 22. ValueOperations<String,String> valOpsStr; 23. 24. @RequestMapping("set") 25. **public** String setKeyAndValue(String key,String value)\{ 26. logger.debug("访问set:key=\{\},value=\{\}",key,value); 27. valOpsStr.set(key, value); 28. **return** "Set Ok"; 29. \} 30. 31. @RequestMapping("get") 32. **public** String getKey(String key)\{ 33. logger.debug("访问get:key=\{\}",key); 34. **return** valOpsStr.get(key); 35. \} 36. \} 2、修改工程配置文件application.properties,增加以下内容 **\[html\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. \#redis数据库名称 从0到15,默认为db0 2. spring.redis.database=1 3. \#redis服务器名称 4. spring.redis.host=127.0.0.1 5. \#redis服务器密码 6. spring.redis.password=123456 7. \#redis服务器连接端口号 8. spring.redis.port=6379 9. \#redis连接池设置 10. spring.redis.pool.max-idle=8 11. spring.redis.pool.min-idle=0 12. spring.redis.pool.max-active=8 13. spring.redis.pool.max-wait=-1 14. \#spring.redis.sentinel.master= 15. \#spring.redis.sentinel.nodes= 16. spring.redis.timeout=60000 注:win平台,下载一个,修改密码,就可直接运行 3、运行测试 在浏览器中先输入 http://localhost:8080/set?key=lxh2&&value=1001 再输入 http://localhost:8080/get?key=lxh2 再改改value的值试试 ## 三、使用RedisTemplate ## 1、在包com.example下建立文件夹domain 2、在domain中建议类person **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.example.domain; 2. 3. **import** java.io.Serializable; 4. 5. **public** **class** Person **implements** Serializable \{ 6. 7. **private** **static** **final** **long** serialVersionUID = 1L; 8. 9. **private** String id; 10. **private** String name; 11. **private** Integer age; 12. 13. 14. **public** Person() \{ 15. **super**(); 16. \} 17. 18. 19. **public** Person(String id, String name, Integer age) \{ 20. **super**(); 21. **this**.id = id; 22. **this**.name = name; 23. **this**.age = age; 24. \} 25. **public** String getId() \{ 26. **return** id; 27. \} 28. **public** **void** setId(String id) \{ 29. **this**.id = id; 30. \} 31. **public** String getName() \{ 32. **return** name; 33. \} 34. **public** **void** setName(String name) \{ 35. **this**.name = name; 36. \} 37. **public** Integer getAge() \{ 38. **return** age; 39. \} 40. **public** **void** setAge(Integer age) \{ 41. **this**.age = age; 42. \} 43. 44. \} 注意:一定有空的和全字段的构造函数 3、在domain中建立类PersonDao **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.example.domain; 2. 3. **import** javax.annotation.Resource; 4. 5. **import** org.springframework.beans.factory.annotation.Autowired; 6. **import** org.springframework.data.redis.core.RedisTemplate; 7. **import** org.springframework.data.redis.core.ValueOperations; 8. **import** org.springframework.stereotype.Repository; 9. 10. @Repository 11. **public** **class** PersonDao \{ 12. 13. @Autowired 14. RedisTemplate<Object,Object> redisTemplate; 15. 16. @Resource(name="redisTemplate") 17. ValueOperations<Object,Object> valOps; 18. 19. **public** **void** save(Person person)\{ 20. valOps.set(person.getId(), person); 21. \} 22. 23. **public** Person getPerson(String id)\{ 24. **return** (Person) valOps.get(id); 25. \} 26. \} 4、在Web中建立类ObjectRedisController **\[java\]** [view plain][] [copy][view plain] [![在CODE上查看代码片][CODE]][CODE_CODE] [![派生到我的代码片][ico_fork.svg]][ico_fork.svg 1] 1. **package** com.example.web; 2. 3. **import** org.slf4j.Logger; 4. **import** org.slf4j.LoggerFactory; 5. **import** org.springframework.beans.factory.annotation.Autowired; 6. **import** org.springframework.web.bind.annotation.RequestMapping; 7. **import** org.springframework.web.bind.annotation.RestController; 8. 9. **import** com.example.domain.Person; 10. **import** com.example.domain.PersonDao; 11. 12. @RestController 13. **public** **class** ObjectRedisController \{ 14. 15. **protected** **static** Logger logger=LoggerFactory.getLogger(ObjectRedisController.**class**); 16. 17. @Autowired 18. PersonDao personDao; 19. 20. @RequestMapping("/setPerson") 21. **public** **void** set(String id,String name,Integer age)\{ 22. logger.debug("访问setPerson:id=\{\},name=\{\},age=\{\}",id,name,age); 23. Person person=**new** Person(id,name,age); 24. personDao.save(person); 25. \} 26. 27. @RequestMapping("/getPerson") 28. **public** Person getPerson(String id)\{ 29. **return** personDao.getPerson(id); 30. \} 31. 32. \} 5、运行测试 http://localhost:8080/setPerson?id=2&&name=lxh2&&age=2 http://localhost:8080/getPerson?id=2 [Center]: /images/20220720/00d0e51513db4edebfe4bc4148f68192.png [view plain]: http://blog.csdn.net/lxhjh/article/details/51753852# [CODE]: https://code.csdn.net/assets/CODE_ico.png [CODE_CODE]: https://code.csdn.net/snippets/1728814 [ico_fork.svg]: https://code.csdn.net/assets/ico_fork.svg [ico_fork.svg 1]: https://code.csdn.net/snippets/1728814/fork
还没有评论,来说两句吧...