SpringBoot相关的知识点

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

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值