SpringBoot相关的知识点总结

32 篇文章 0 订阅
28 篇文章 0 订阅

1,为SpringBoot的Controller类写测试类,如下:

下面针对该Controller编写测试用例验证正确性,具体如下。当然也可以通过浏览器插件等进行请求提交验证:
package com.kfit.demo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.kfit.controller.UserController;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=MockServletContext.class)//MockServletContext.class
@WebAppConfiguration
public class UserControllerTest extends MockMvcResultMatchers{
//模拟mvc对象类.
private MockMvc mvc;
@Before
public void setup(){
/*
* MockMvcBuilders使用构建MockMvc对象.
*/
mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
}
@Test
public void testUserController() throws Exception{
RequestBuilder request = null;
//1. get 以下user列表,应该为空》
//1、构建一个get请求.
request = MockMvcRequestBuilders.get("/users");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string("[]"))
;
System.out.println("UserControllerTest.testUserController().get");
// 2、post提交一个user
request = MockMvcRequestBuilders.post("/users")
.param("id","1")
.param("name","林峰")
.param("age","20")
;
mvc.perform(request).andExpect(status().isOk()).andExpect(content().string("success"));
// 3、get获取user列表,应该有刚才插入的数据
request = MockMvcRequestBuilders.get("/users");
mvc.perform(request).andExpect(status().isOk()).andExpect(content().string("[{\"id\":1,\"name\":\"林峰\",\"age\":20}]"));
// 4、put修改id为1的user
request = MockMvcRequestBuilders.put("/users/1")
.param("name", "林则徐")
.param("age", "30");
mvc.perform(request)
.andExpect(content().string("success"));
// 5、get一个id为1的user
request = MockMvcRequestBuilders.get("/users/1");
mvc.perform(request)
.andExpect(content().string("{\"id\":1,\"name\":\"林则徐\",\"age\":30}"));
// 6、del删除id为1的user
request = MockMvcRequestBuilders.delete("/users/1");
mvc.perform(request)
.andExpect(content().string("success"));
// 7、get查一下user列表,应该为空
request = MockMvcRequestBuilders.get("/users");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string("[]"));
}
}

2,使用@ConfigurationProperties注解进行编码

需要下面的依赖
<!--spring boot  配置处理器  -->       
        < dependency >
            < groupId > org.springframework.boot </ groupId >
            < artifactId > spring-boot-configuration-processor </ artifactId >
            < optional > true </ optional >
        </ dependency >

配置类可以这样写:
@ConfigurationProperties (prefix= "com.kfit.blog" )
public class  BlogProperties {
   
     private  String  name ; // 博客作者
   
     private  String  title ; // 博客标题
 
     //  省略 getter setter
}

在启动类中加入:
@EnableConfigurationProperties({BlogProperties.class})
这种的好处就是只需要配置一个地方,其它地方就是正常定义类的属性即可了。

3,
这条命令: java -jar xxx.jar --server.port=8888 ,通过使用 --server.port 属性来设置 xxx.jar 应用的端口为 8888

4,
多环境的配置思路:
   application.properties 中配置通用内容,并设置 spring.profiles.active=dev ,以开发环境为默认配置
   application-{profile}.properties 中配置各个环境不同的内容
通过命令行方式去激活不同环境的配置。

执行 java -jar xxx.jar --spring.profiles.active=prod ,可以观察到服务端口被设置为 80 ,也就是生产环境的配置( prod

5,在springboot的开发中,开发包和测试包必须一一对应,否则build的时候会出错的.

6,在springboot 的入口类中,我们只需要打上:@SpringBootApplication即可.其他的就免了,比如:
 @Configuration   @EnableAutoConfiguration   @ComponentScan
因为前者是后面几个的汇总.

7,RESTful API
请求类型 URL 功能说明
GET /users 查询用户列表
POST /users 创建一个用户
GET /users/id 根据id查询一个用户
PUT /users/id 根据id更新一个用户
DELETE /users/id 根据id删除一个用户

8,代码提交命令
git pull origin features:features (前一个是远程仓库,后者是本地仓库)

git add .

git commit -m "updated some bugs"

git push origin features origin/features


9,

从前端传来日期的字符串,然后在项目中把字符串转化为Date类型,然后传入JPA的查询语句中.

顺便说下,在mysql中日期的比较可以直接使用 between and 进行.例如:

select * from `apps` where `created_at` between "2012-06-26 00:00:00" and "2012-06-26 00:00:00";
@Query ( "select mon from Monetization mon where appId=:appId and datetime between :startDate and :endDate group by dayid" )


第一步:接收串
@GetMapping ( "/adrevenues" )
public ResponseEntity listReveByAppidAndDate( @RequestParam ( "appId" ) String appId, @RequestParam ( "startDate" )String startDate, @RequestParam ( "endDate" )String endDate){
List<Monetization> monetizationList= monetizationService .findByAppidAndDate(appId,startDate,endDate);
List<RevenueDto> monetizationDtoList=Lists. newArrayList ();
monetizationDtoList.addAll(monetizationList.stream().map( this ::convertReven).collect(Collectors. toList ()));
return ResponseEntity. ok (monetizationDtoList);
}

第二步:转换
@Override
public List<Monetization> findByAppidAndDate(String appId, String startDate, String endDate){
Date start= null ;
try {
start = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).parse(startDate);
Date end= new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).parse(endDate);
return monetizationRepository .findByAppidAndDate(appId,start,end);
} catch (ParseException e) {
e.printStackTrace();
}
return Collections. emptyList ();
}

第三步:使用Date类型与mysql进行交互
public interface MonetizationRepository extends JpaRepository<Monetization,Long>{
@Query ( "select mon from Monetization mon where appId=:appId and datetime between :startDate and :endDate group by dayid" )
public List<Monetization> findByAppidAndDate( @Param ( "appId" )String appId, @Param ( "startDate" )Date startDate, @Param ( "endDate" )Date endDate);

}

学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring BootWeb开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值