Article Directory
1. Understand SpringBoot
Native Development: Servlet + jsp, very troublesome, web.xml code or there will be a large number of duplicate content;
Simplify development : Spring Everything inside the profile, integrated framework, or do a lot of large projects, will cause the entire program and project very bloated, throughout the configuration file;
Simplified configuration file : SpringBoot Spring is like an upgraded version of the original lot of things need to manually configure, and now only need to automatically configure!
The trouble of combing the framework of the SSM :
- Many profiles
- web.xml, tomcat must be configured
- lib also need to rely on management
So in all the above SpringBoot, do not need to be configured to run!
2. The first SpringBoot program
1, using the IDEA program to build a SpringBoot
2, fill out basic information about the project Maven
3, check the starter (Spring Web) If you check this, the equivalent help you automatically configured the Spring and SpringMVC, including Tomcat!
4. When finished, wait Maven automatically downloads all dependencies, and then delete unneeded several folders
final catalog as follows:
5, First Program
Note : Be sure to start at SpringBoot master class at the same level or sub-level directory, create a new package! Otherwise it is not recognized
package com.zz.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello,SpringBoot!";
}
}
6, start the main class to start, access the test: http: // localhost: 8080 / hello
expand: We can customize the banner!
Custom boot Logo, default is Spring, can be modified according to their preferences.
(1) banner.txt we create a new file in the resource directory, you can write your own banner in here
(2) Online Web Site Builder: HTTPS: //www.bootschool.net/ascii
(3) start the test to see results
3. Understand the principles SpringBoot
1, how to start?
A note: @SpringBootApplication
a class:SpringApplication
The master boot automatically generated class can be deleted, write yourself a hand!
(1) adding an annotation class @SpringBootApplication above
(2) used to start the run method SpringApplication
Such as write a class named ZzApplication the master boot:
package com.zz;
//自己手写一个主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZzApplication {
public static void main(String[] args) {
//run方法的启动
SpringApplication.run(ZzApplication.class,args);
}
}
2, depending on how the configuration
Thoughts: We do not have to configure tomcat, no configuration servlet, not configured spring, how this came into force?
SpringBoot a default pom.xml, there are a parent and a scene-dependent initiator
Parent dependence:
Father dependent effect analysis
(1) automatically help you manage dependencies, which contains all rely almost common, if you need to rely on and there are, you do not configure, and if no further configuration
(2) plug-ins and resources filtering automatic management
Launcher:
spring-boot-starter-xx introduced corresponding to the desired scene class, will help you automatically import the package required for this scene dependent
official website of all starter: https: //docs.spring.io/spring-boot/ docs / 2.2.5.RELEASE / reference / html / using-spring-boot.html # using-boot
Configuration 4. SpringBoot
All configurations can be configured in the configuration file:
- application.yml / application.yaml
- application.properties
properties profile of our traditional key = value
yaml is SpringBoot recommended configuration file, more powerful
yaml all grammar :( spaces strict requirements, indent stringent requirements)
# 普通键值对
key: value
name: qinjiang
# map/对象
key:
k1: v1
k2: v3
person:
name: qinjiang
age: 3
# 行内写法 {}
person: {name: qinjiang,age: 3}
# list/数组
key:
- v1
- v2
pets:
- dog
- pig
- cat
# 行内写法
pets: [dog,pig,cat]
5. SpringBoot integrated MyBatis
Note: MyBatis All packages are their own, so you want to import their dependence
The entire project directory:
1, and adding drive dependency in pom.xml
<!-- 这是自定义的包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- mysql 驱动 在这不写版本号,默认使用mysql8的版本 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--在这里写的依赖都不需要版本号,因为在父依赖中有!-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2, write a configuration file application.yml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.kuang.pojo
mapper-locations: classpath:com/kuang/mapper/*.xml
3, there is no test data sources, can be tested in the test class
package com.zz;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class HelloSpringbootApplicationTests {
// 自动导入数据源
@Autowired
private DataSource dataSource;
// SpringBoot 2.X 默认集成的是 Hikari
@Test
void contextLoads() throws SQLException {
// 查看默认数据源 class com.zaxxer.hikari.HikariDataSource
System.out.println(dataSource.getClass());
// connection
Connection connection = dataSource.getConnection();
System.out.println(connection);
// 关闭连接
connection.close();
}
}
SpringBoot current default data source is a class com.zaxxer.hikari.HikariDataSource
4, the preparation of an entity class User
package com.zz.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
5, write interfaces UserMapper (using annotations and XML can be)
package com.zz.mapper;
import com.zz.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper // @Mapper 标注这是一个Mapper接口
@Repository // 代表持久层
public interface UserMapper {
// @Select("select * from user") 注解方式
List<User> getUserLists();
}
6, write the interface configuration file UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zz.mapper.UserMapper">
<select id="getUserLists" resultType="User">
select * from user;
</select>
</mapper>
7, the control layer prepared MyBatisController
package com.zz.controller;
import com.zz.mapper.UserMapper;
import com.zz.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class MyBatisController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/list")
public List<User> getUserList(){
List<User> userLists = userMapper.getUserLists();
return userLists;
}
}
8, configuration .xml file filter of resources
due to the .xml configuration file was not exported, the allocation of resources can be filtered in pom.xml
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
9, the main start-based start, the test
In the address bar, enter http: // localhost: 8080 / list, the page will display the data in the database user table
6. SpringBoot Web Application Development
1, the resource store catalog descriptions
static
Static resources
templates
page,templates resource files can only be accessed by controller
resources
Resource files can also be stored
public
SpringBoot source found in static public resources can be placed here
Test:
2, Thymeleaf use, import static resource template using html page written
Thymeleaf is a popular template engine , template engine uses the Java language development, template engine is a technical term, is a cross-cutting cross-platform concepts.
Thymeleaf main objective is to provide a can be correctly displayed in the browser, a good way to create a template format , it can also be used as static modeling. You can use it to create a verified XML and HTML templates. Use thymeleaf created html templates can be opened directly (show static data) in the browser, which is beneficial to the front and rear ends of the splitter.
(1) into the corresponding dependent maven
<!-- thymeleaf依赖,如果要编写页面一定需要这个依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(2) written into the html page templates directory
(3) using the jump controller
package com.zz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //可以被视图解析器解析
public class IndexController {
//只要访问 / 就会跳转到templates目录下的index页面
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/user/index")
public String userIndex(){
return "user/index";
}
@RequestMapping("/user/list")
public String userList(){
return "user/list";
}
}
(4) start the test
3, page pass a value
(1) at the rear end using a Model transmitting method value
package com.zz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
@Controller //可以被视图解析器解析
public class IndexController {
@RequestMapping("/")
public String index(Model model){
model.addAttribute("msg","Hello,SpringBoot!");
model.addAttribute("users", Arrays.asList("小猫","小狗"));
return "index";
}
}
(2) in the front end of th: xxx to receive the rear end of the transmission value, Note: import header file must be constrained
Common values and loop through
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<!-- 普通取值 -->
<p th:text="${msg}"></p>
<!-- 循环遍历:接收后端传递的 users,遍历每个的结果就是user,可以在这个标签内使用!
th:each="item:items"
-->
<!--写法一-->
<h2 th:each="user:${users}" th:text="${user}"></h2>
<!-- 写法二:行内写法 -->
<h2 th:each="user:${users}">[[${user}]]</h2>
<!--写法三-->
<div th:each="user:${users}">
<p th:text="${user}"></p>
</div>
</body>
</html>
Start the test: