• SpringBoot框架学习(三)——热部署,整合Redis


    1. 程序打包

    打包时要确认 pom.xml 中存在springboot的打包插件,如下

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    打包只需要双击下述的package即可
    在这里插入图片描述
    假设打包后的文件为 1.jar ,运行打包文件只需要在当前目录下的命令行输入 java -jar 1.jar 就行。

    2.Restful 风格

    Restful 风格的软件架构要求我们Web开发人员,使用一个url地址表示一个唯一的资源,然后把原来的请求参数加入到请求资源地址中,原来请求的增,删,改,查操作,改为使用HTTP协议中请求方式GET、POST、PUT、DELETE表示。

    比如原来想要对数据进行增加,可能必须重新定向到一个网页 http://localhost:8080/books/add ,想要对数据进行删除,也必须重新定向到一个URL http://localhost:8080/books/delete,现在使用Restful风格的话就只用使用一个 URL ,改变URL的请求方式就行,比如增加操作,其URL是 http://localhost:8080/books,请求方式是 Get,删除操作的URL是 http://localhost:8080/books,请求方式是 Delete

    3. 网页请求数据操作

    CRUD以及分页查询的操作如下所示

    @RestController
    @RequestMapping("/books")
    public class BookController {
        @Autowired
        private IBookService bookService;
    
        @GetMapping
        public List<Books> getAll(){
            return bookService.list();
        }
    
        @PostMapping
        public Boolean save(@RequestBody Books book){
            return bookService.save(book);
        }
    
        @PutMapping
        public Boolean update(@RequestBody Books book){
            return bookService.modify(book);
        }
    
        @DeleteMapping("{id}")
        public Boolean delete(@PathVariable Integer id){
            return bookService.delete(id);
        }
    
        @GetMapping("{id}")
        public Books getById(@PathVariable Integer id){
            return bookService.getById(id);
        }
    
        @GetMapping("{currentPage}/{pageSize}")
        public IPage<Books> getById(@PathVariable int currentPage, @PathVariable int pageSize){
            return bookService.getPage(currentPage, pageSize);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    @RequestBody 主要用来接收前端传递给后端的json字符串中的数据。
    @PathVariable 表示获取到路径中的信息,如 getById 中的 @PathVariable 即获取到路径中的 @GetMapping("{id}")。举个例子,如果用户请求的URL是 http://localhost:8080/books/2 ,那么 getById 中的参数 id 就是 2

    4. 热部署

    添加以下依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-devtoolsartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    4.1 手工热部署

    运行后如果更新了程序,点击IDEA的 Build 下的 Build Project
    在这里插入图片描述

    4.2 自动热部署

    感觉没啥必要,就不写了吧,自动的太麻烦了,一会自动一会自动的,贼烦。

    5. 整合Redis

    5.1 导入依赖

    整合Redis需要导入依赖如下:

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.datagroupId>
        <artifactId>spring-data-commonsartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.2 Redis设置

    在配置文件application.yml中可以对Redis进行一些配置,示例如下:

    spring:
      redis:
        host: localhost
        port: 6379
    
    • 1
    • 2
    • 3
    • 4

    5.3 测试

    导入Redis后的测试如下:

    @Autowired
    private RedisTemplate redisTemplate;
    
    
    @Test
    void set_value() {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("age", 18);
    }
    
    @Test
    void get_value() {
        ValueOperations ops = redisTemplate.opsForValue();
        Object age = ops.get("age");
        System.out.println(age);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    5.4 测试中的问题

    紧接上面的测试,我们在客户端方面键入 keys * 会打印下面的内容。

    127.0.0.1:6379> keys *
    1) “\xac\xed\x00\x05t\x00\x03age”

    可以看到,键中确实有age,但是age前面还有一些莫名其妙的东西,这是怎么回事呢。

    这样的原因是因为上面测试的代码中的 RedisTemplate 会将传入的两个泛型当做对象进行操作,这就产生了前面的一些看不懂的字符串,要想像之前客户端的一样操作,就可以使用 StringRedisTemplate ,这里面将其传入的两个泛型都定死了,只能是String类型,这也就能够达到我们的目的了。具体代码如下:

    class SpringBootStudyApplicationTests {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
    
        @Test
        void set_value() {
            ValueOperations ops = stringRedisTemplate.opsForValue();
            ops.set("age", "18");
        }
    
        @Test
        void get_value() {
            ValueOperations ops = stringRedisTemplate.opsForValue();
            Object age = ops.get("age");
            System.out.println(age);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    可以看到,上述的改动仅仅将 RedisTemplate 改为了 StringRedisTemplate ,并将传入的对象全部变为了字符串而已。

  • 相关阅读:
    Matplotlib绘图-快速上手可视化工具
    电脑屏幕亮度怎么调节?台式电脑找不到屏幕亮度怎么办
    Web应用安全威胁与防护措施
    ETL之apache hop数据增量同步功能
    《Attention Is All You Need》论文笔记
    squid代理服务器
    tensorflow代码翻译成pytorch代码 -详细教程+案例
    C++类的成员函数作为回调函数
    Linux引导故障排除:从问题到解决方案的详细指南
    【Unity ShaderGraph】| 物体靠近时局部溶解,根据坐标控制溶解的位置【文末送书】
  • 原文地址:https://blog.csdn.net/ifhuke/article/details/127254292