• SpringBoot ORM操作MySQL、REST接口架构风格、集成Redis和集成Dubbo


    1. 数据库操作

    Object Relation MySQL

    1.1 使用步骤

    (1) mybatis启动依赖
    完成mybatis对象自动配置,对象放在容器中。
    (2) pom.xml文件中指定把src/main/java目录中的xml文件包含到classpath中
    (3) 创建实体类
    (4) 创建Dao接口
    (5) 创建Dao接口对应的mapper文件,xml文件编写sql语句。
    (6) 创建Service层对象,调用dao层的方法,完成数据库操作。

    2. Rest接口

    2.1 架构风格

    api的组织方式。

    传统的一个风格: http://localhost:8080/mytrans/addStudent?name=zhangsan&age=20
    在地址上提供了访问的资源名称addStudent,在其之后使用了get方式传递参数。

    REST: Representational State Transfer,表现层状态转移。
    是一种架构风格和设计理念,并不是标准。
    优点: 更简洁,更有层次。
    表现层状态转移: 表现层就是视图层,用于显示资源。通过页面显示操作资源的结果。
    状态:资源的变化。
    转移:资源可以变化的。
    一句话描述rest: 使用url表示资源,使用http动作操作资源

    GET:查询
    post:创建
    put:更新
    delete:删除资源

    注意: rest接口中, 请求方式 + url 一定是惟一的。 否则具有二义性,服务器直接500错误。举例如下:

        @GetMapping("/student/{stuId}")
        public String queryStudent(@PathVariable(value = "stuId") Integer studentId) {
            return "student id is = " + studentId;
        }
    
        @GetMapping("/student/{age}")
        public String queryStudentByAge(@PathVariable(value = "age") Integer age) {
            return "student age is = " + age;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.2 RESTful注解

    2.2.1 @PathVariable

    从url中获取数据

        @GetMapping("/student/{stuId}")
        public String queryStudent(@PathVariable(value = "stuId") Integer studentId) {
            return "student id is = " + studentId;
        }
    
    • 1
    • 2
    • 3
    • 4

    2.2.2 @GetMapping、@PostMapping、@PutMapping和@DeleteMapping

    @GetMapping : 支持get请求方式,等同于 @RequestMapping(method = RequestMethod.GET)
    @PostMapping : 支持post请求方式,等同于 @RequestMapping(method = RequestMethod.POST)
    @PutMapping : 支持put请求方式,等同于 @RequestMapping(method = RequestMethod.PUT)
    @DeleteMapping : 支持delete请求方式,等同于 @RequestMapping(method = RequestMethod.DELETE)

    2.2.3 @RestController

    符合注解,是@Controller和@ResponseBody的组合。
    使用 @RestController,当前类中所有的方法都使用了ResponseBody

    3. SpringBoot集成Redis

    RedisTemplate:本质上使用的lettuce客户端。

    3.1 StringRedisTemplate 和 RedisTemplate

    StringRedisTemplate :k v都是String ,使用的是String的序列化,可读性好。
    RedisTemplate:把k v 经过了序列化存储到redis。 k v是序列化的内容,不能直接识别。默认使用的jdk的序列化机制。

    RedisTemplate可以修改序列化器

        public String addString(String k, String v) {
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
    
            redisTemplate.opsForValue().set(k, v);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4. SpringBoot集成Dubbo

    Dubbo基于RPC协议的一个框架。

    5. SpringBoot打包

    5.1 war包

    (1) 修改pom.xml文件

    • 指定打包后的名称
    <build>
    	<finalName>mybootfinalName>
    build>
    
    • 1
    • 2
    • 3
    • 指定jsp编译目录
    • 指定打包类型为war
    <packaging>warpackaging>
    
    • 1

    (2) 主启动类继承SpringBootServletInitializer
    将Application类暴露出去,这样可以使用独立的tomcat服务器

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Application.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    (3) 部署
    将war放在tomcat等服务器的发布目录中。

    5.2 jar包

    (1) 修改pom.xml文件

    • 指定打包后的名称
    <build>
    	<finalName>mybootfinalName>
    build>
    
    • 1
    • 2
    • 3
    • 指定springboot maven插件的版本
    <plugins>
    	<plugin>
    		<groupId>org.springframework.bootgroupId>
    		<artifactId>spring-boot-maven-pluginartifactId>
    		<version>1.4.2-RELEASEversion>
    	plugin>
    plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (2) 执行mave package

    • 会生成myboot.jar包

    5.3 war包和jar包的区别

    war:可以利用真正的容器的能力
    jar:方便,不需要使用额外的容器。

  • 相关阅读:
    【C/PTA——循环结构3】
    数据结构之 哈希表习题 力扣oj(附加思路版)
    Git 和 Github 的使用
    ICO操作Bean管理的(bean的作用域和生命周期)
    美团秋招高频面试问题汇总!(内附答案!)
    delphi fmx zxing原生不使用外部库二维码,条码扫描速度很快
    mac之 iTerm2 + Oh My Zsh 终端安装教程
    Postman之接口测试
    G1D4-软考中级《项目集成管理》-下午真题
    情人节程序员用HTML网页表白【情人节爱你的代码】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
  • 原文地址:https://blog.csdn.net/kaikai_sk/article/details/126704824