Spring Boot2

SpringBoot framework

1. Spring boot test

In the test class, the following two properties in @SpringBootTest annotation:

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

Once the test class used @SpringBootTest annotations, we can inject code TestRestTemplate object, this object may send a request and get a response, then the response is processed and analyzed [Comparative]

2. How to read an external properties file is read-only application.properties file [default]

On a related note:

@PropertySource = "is used to read the specified file attribute

NOTE: This annotation can be placed startup class, may be arranged in another class

How to remove these attributes data it?

Method one: JAVABEAN a package to take out, in this class, can be (prefix = "") is specified by the prefix @ConfigurationProperties, further, the play annotations @Component

Two ways: directly use the data in place by injection to @Autowired Environment env environment object, then use env.getProperty ( "key") to obtain the property value.

JDBC technology and integrated 3.SpringBoot

  1. SpringBoot dependence introduction and integration JDBC, and a drive-dependent target database

  2. Configuring application.yml or application.properties file, as follows:

    # 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. With these steps, we DAO implementation class, it can be injected directly into the JdbcTemplate object.

In SpringBoot in, DAO test

With DAO in the Spring Framework is different, different annotation is as follows:

@RunWith ( . SpringRunner class) 
@SpringBootTest
public class TestUserDao { @Autowired Private IUserDao userDao; .... } // DAO in conventional tests Spring Framework: @ContextConfiguration ( classes = . AppConfig class) public class TestAccountDao the extends {of AbstractTestNGSpringContextTests for @Autowired   // injection according to the type of Private IAccountDao AccountDao; ... }




   











4.SpringBoot integrated framework and mybatis

step:

  1. Join dependence

    <!-- 与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. In application.yml or application.properties file, add the relevant configuration mybatis map and category name

    # mybatis configuration
    mybatis:
    mapper-locations: classpath*:mybatis/**/*.xml
    type-aliases-package: com.hcedu.springboot.first.entity
  3. In the interface above, add annotations @Mapper

  4. Write xml mapping file

Note: mybatis if pure notes written word, you do not need to write xml mapping file

5.SpringBoot integrated framework and thymeleaf

thymeleaf page template is an efficient front-end template engine, its advantages are:

  1. When browsing can be localized, there is no real data is displayed in the default data. It is a .html extension

  2. The syntax is simple, similar to the EL expression.

There are a lot of front page template engine, SpringBoot also a lot of support, including:

  1. Thymeleaf default template engine

  2. Beetl

  3. FreeMarker

  4. Velocity

  5. ...

Note: In SpringBoot, the reason is not selected as the default jsp page template, because the main JSP can not run jar format embedded in Tomcat server.

In SpringBoot, how to integrate thymeleaf it?

step:

  1. Join thymeleaf dependence [there is] a 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. In the configuration file application.yml

    spring: 
    # thymeleaf config
    thymeleaf:
      cache: false
      encoding: UTF-8
      mode: HTML
      servlet:
        content-type: text/html
  3. The next step is to develop Controller development and front pages

Note: The syntax thymeleaf, check out the official documentation.

Guess you like

Origin www.cnblogs.com/fanzhuangzhuang/p/11236705.html