• Spring Boot2


    SpringBoot框架

    1. Spring boot测试

    在测试类中,在@SpringBootTest 注解中使用如下 两种属性:

    @SpringBootTest(classes = HelloSpringbootFirstApplication.class,
            webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

    一旦在测试类中,使用了 @SpringBootTest注解,我们就可以在代码中注入 TestRestTemplate 对象,通过这个对象,可以发送请求并获取响应,然后对响应进行处理【比较和分析】

    2. 如何读取外部的属性文件【默认是只读application.properties文件】

    相关的注解:

    @PropertySource =》 用来读取指定的属性文件

    注:此注解可以放在启动类中,也可以放在另一个配置类中

    如何取出这些属性数据呢?

    方式一:通过封装一个JAVABEAN 来取出来,在这个类中,可以通过@ConfigurationProperties(prefix = "") 来指定前缀,另外,要打上 @Component注解

    方式二: 直接在使用这些数据的地方,通过 @Autowired Environment env 来注入环境对象,再使用env.getProperty("key") 来获取属性值。

    3.SpringBoot与JDBC技术的集成

    1. 导入SpringBoot与JDBC集成的依赖, 以及 目标数据库的驱动 依赖

    2. 配置 application.yml或application.properties 文件,如下:

      # config connection properties
      spring:
      datasource:
        url: jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.jdbc.Driver
        password: 123456
        username: root
    3. 有了以上的步骤,我们在DAO的实现类中,就可以直接注入 JdbcTemplate 对象。

    在SpringBoot中,DAO的测试

    与在 Spring框架中的DAO所不同的是,就是采用 注解不同,如下:

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

    @Autowired
    private IUserDao userDao;
    ....
       
    }

    //传统的Spring框架中 DAO的测试:
    @ContextConfiguration(classes = AppConfig.class)
    public class TestAccountDao extends AbstractTestNGSpringContextTests {

    @Autowired  //根据类型来注入
    private IAccountDao accountDao;

    ...

    }

    4.SpringBoot与mybatis 框架的集成

    步骤:

    1. 加入依赖

      <!-- 与mybatis的集成 -->
             <dependency>
                 <groupId>org.mybatis.spring.boot</groupId>
                 <artifactId>mybatis-spring-boot-starter</artifactId>
                 <version>1.3.2</version>
             </dependency>
             <!-- 目标数据库,这里采用mysql -->
             <dependency>
                 <groupId>mysql</groupId>
                 <artifactId>mysql-connector-java</artifactId>
                 <version>5.1.40</version>
             </dependency>
    2. 在 application.yml或application.properties 文件中,添加对mybatis 映射及类别名相关的配置

      # mybatis configuration
      mybatis:
      mapper-locations: classpath*:mybatis/**/*.xml
      type-aliases-package: com.hcedu.springboot.first.entity
    3. 在接口上面,添加 @Mapper 注解

    4. 编写xml映射文件

    注:mybatis如果采用纯注解编写的话,则无需写 xml 映射文件

    5.SpringBoot 与 thymeleaf 框架 的集成

    thymeleaf页面模板是一个高效的前端模板引擎,它的优点是:

    1. 可以本地化浏览,没有真实数据时,以默认的数据显示。它就是以.html为扩展名

    2. 语法比较简单,类似于EL表达式。

    前端页面模板引擎有很多,SpringBoot支持的也很多,主要有:

    1. Thymeleaf 默认的模板引擎

    2. Beetl

    3. FreeMarker

    4. Velocity

    5. ...

    注:在SpringBoot中,之所以没有选择jsp做为默认的页面模板,主是因为JSP不能以jar格式运行在内嵌的Tomcat服务器中.

    在SpringBoot中,如何集成 thymeleaf呢?

    步骤:

    1. 加入 thymeleaf的依赖【还有一个 nekohtml 】

       <!-- 与thymeleaf 集成 -->
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-thymeleaf</artifactId>
             </dependency>
             <!-- 允许使用非严格的HTML语法 -->
             <dependency>
                 <groupId>net.sourceforge.nekohtml</groupId>
                 <artifactId>nekohtml</artifactId>
                 <version>1.9.22</version>
             </dependency>
    2. 在 application.yml文件中配置

      spring: 
      # thymeleaf config
      thymeleaf:
        cache: false
        encoding: UTF-8
        mode: HTML
        servlet:
          content-type: text/html
    3. 接下来就是Controller的开发和前端页面的开发

    注: 有关thymeleaf的语法,请查看官方的文档。

  • 相关阅读:
    深入理解Java内存模型(四)——volatile
    深入理解Java内存模型(三)——顺序一致性
    深入理解Java内存模型(二)——重排序
    深入理解Java内存模型(一)——基础
    HashMap完全解读
    HTTP访问控制(CORS)
    理解RESTful架构
    转: Java LinkedList基本用法
    JAVA_POI 操作Excel
    maven 详解二
  • 原文地址:https://www.cnblogs.com/fanzhuangzhuang/p/11236705.html
Copyright © 2020-2023  润新知