文章目录
1. 程序打包
打包时要确认 pom.xml
中存在springboot的打包插件,如下
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包只需要双击下述的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);
}
}
@RequestBody
主要用来接收前端传递给后端的json字符串中的数据。
@PathVariable
表示获取到路径中的信息,如 getById
中的 @PathVariable
即获取到路径中的 @GetMapping("{id}")
。举个例子,如果用户请求的URL是 http://localhost:8080/books/2
,那么 getById
中的参数 id
就是 2
。
4. 热部署
添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
4.1 手工热部署
运行后如果更新了程序,点击IDEA的 Build
下的 Build Project
。
4.2 自动热部署
感觉没啥必要,就不写了吧,自动的太麻烦了,一会自动一会自动的,贼烦。
5. 整合Redis
5.1 导入依赖
整合Redis需要导入依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
5.2 Redis设置
在配置文件application.yml
中可以对Redis进行一些配置,示例如下:
spring:
redis:
host: localhost
port: 6379
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);
}
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);
}
}
可以看到,上述的改动仅仅将 RedisTemplate
改为了 StringRedisTemplate
,并将传入的对象全部变为了字符串而已。