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;
    }

2.2 RESTful注解

2.2.1 @PathVariable

从url中获取数据

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

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);
    }

4. SpringBoot集成Dubbo

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

5. SpringBoot打包

5.1 war包

(1) 修改pom.xml文件

  • 指定打包后的名称
<build>
	<finalName>myboot</finalName>
</build>
  • 指定jsp编译目录
  • 指定打包类型为war
<packaging>war</packaging>

(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);
    }
}

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

5.2 jar包

(1) 修改pom.xml文件

  • 指定打包后的名称
<build>
	<finalName>myboot</finalName>
</build>
  • 指定springboot maven插件的版本
<plugins>
	<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<version>1.4.2-RELEASE</version>
	</plugin>
</plugins>

(2) 执行mave package

  • 会生成myboot.jar包

5.3 war包和jar包的区别

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值