1. Spring boot test
In the test class, the following two properties in @SpringBootTest annotation:
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
-
SpringBoot dependence introduction and integration JDBC, and a drive-dependent target database
-
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 -
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:
4.SpringBoot integrated framework and mybatis
step:
-
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> -
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 -
In the interface above, add annotations @Mapper
-
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:
When browsing can be localized, there is no real data is displayed in the default data. It is a .html extension
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:
-
Thymeleaf default template engine
-
Beetl
-
FreeMarker
-
Velocity
-
...
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:
-
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> -
In the configuration file application.yml
spring:
# thymeleaf config
thymeleaf:
cache: false
encoding: UTF-8
mode: HTML
servlet:
content-type: text/html
-
The next step is to develop Controller development and front pages
Note: The syntax thymeleaf, check out the official documentation.