美文网首页
SpringBoot学习总结(注意点)

SpringBoot学习总结(注意点)

作者: 风丶无痕 | 来源:发表于2018-10-24 22:18 被阅读0次
  • SpringBoot pom.xml文件中默认有两个模块:
    spring-boot-starter:核心模块,包括自动配置支持,日志和YAML;
    spring-boot-starter-test:测试模块,包括JUnit,Hamcrest,Mockito;

  • yaml,是JSON的一个超集,也是一种方便的定义层次配置数据的格式.如果使用Starters ,添加spring-boot-starter 依赖就会自动加载SnakeYAML.

  • 在每个Controller类的顶头添加 @RestController的意思是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了;

  • 启动主程序,打开浏览器访问 http://localhost:8080/hello 就可以看到效果了;
    前提是 类的顶头然后方法的顶头的 @RequestMapping拼接起来为 /hello

  • 自定义Property,在web开发的过程中,需要自定义一些配置文件,
    首先在application.properties中加入如下的配置
    c.snowflake.regin=1
    c.snowflake.node=1
    然后自定义配置类

@Configuration
public class BasicConfig {
    @Value("${c.snowflake.regin}")
    private long regin;

    @Value("${c.snowflake.node}")
    private long node;

    @Bean
    public Snowflake snowflake() {
        return new Snowflake(regin, node);
    }
}

  • 数据库操作.
    JPA是利用Hibernate生成各种自动化的sql,如果只是简单的增删改查,基本不用手写了,因为已经封装好了;

  • 测试;
    加上如下的依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
</dependency>

然后打上如下的注解:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppControllerTest {

    @Test
    public void listAllTest(){
    
    }
}
  • 在dao层中的Repository中的@Query中的sql语句中 select 后面可以跟字段或者表对应实体的别名,from后边跟的是表对应的实体;
@Query("select sc from Scene sc where sc.id=:sceneId and sc.appId=:appId")
List<Scene> findSceneBySceneIdAndAppId(@Param("sceneId") String sceneId, @Param("appId") String appId);

相关文章

网友评论

      本文标题:SpringBoot学习总结(注意点)

      本文链接:https://www.haomeiwen.com/subject/bmrftqtx.html