一、介绍
1.优势
①创建独⽴的 Spring 应⽤程序
②嵌⼊的 Tomcat,⽆需部署 WAR ⽂件
③ 简化 Maven 配置
④⾃动配置 Spring
⑤提供⽣产就绪型功能,如指标,健康检查和外部配置
2.特性
①为基于 Spring 的开发提供更快的⼊⻔体验
②开箱即⽤,没有代码⽣成,也⽆需 XML 配置。同时也可以修改默认值来满⾜特定的需求
③提供了⼀些⼤型项⽬中常⻅的⾮功能特性,如嵌⼊式服务器、安全、指标,健康检测、外部配置等
④Spring Boot 并不是对 Spring 功能上的增强,⽽是提供了⼀种快速使⽤ Spring 的⽅式
二、搭建springboot项目
通过spring initializr创建spring boot项⽬选择

 

选择spring boot的版本号和所需要的依赖

三、如何访问前端页面

引⼊所需的依赖
 

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

2.编写Controller就可以了

 @Controller
public class DemoController {
    @RequestMapping("index")
    public String index(String name, Model model) {
        model.addAttribute("name", name);
        return "index";
   }
}

 3前端界面

<!DOCTYPE html> <html lang="en"> <head>
    <meta charset="UTF-8">
    <title>项⽬⾸⻚</title>
</head> <body>
  <h1>第⼀个⻚⾯</h1> <div th:text="${name}"></div>
</body>
</html>

四、怎么读取后端的值
通过Model可以设置这个值 可以通过模板引擎读取值

<div th:text="${name}"></div>

 五、介绍配置⽂件
1.yml ---------------注意yml要严格区分格式,空格数要对!

server:
 port: 8080
 servlet:
   context-path: /api

 2properties

server.port=8085
server.servlet.context-path=/springboot

 

六、项⽬打包
在终端输入:mvn clean package
运⾏java -jar jar包

mvn clean package 在target⽬录下⽣成⼀个jar包
如果想启动这个项⽬只要有java环境就可以了
java -jar jar包 启动项⽬指定参数
java -jar -Dserver.port=8082 jar包
springboot读取配置的顺序

    启动参数上的配置
        jar包⽬录下config/application.properties
        classpath:application.properties
        classpath:application.yml
        七、集成jdbcTemplate
        1.引⼊依赖
<!-- 添加mysql jdbc依赖 -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>
       <!-- 添加springboot jdbcTemplate依赖 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-jdbc</artifactId>
       </dependency>
2添加配置文件

spring:
 datasource:
   driver-class-name: com.mysql.cj.jdbc.Driver
   url: jdbc:mysql://localhost:3306/erp16?
useUnicode=true&characterEncoding=UTF-8
   username: root
   password: root
   dbcp2:
     max-idle: 20
     min-idle: 10
3 注入jdbcTemplate执行sql语句

@Controller
@AllArgsConstructor
public class DemoController {
   private final JdbcTemplate jdbcTemplate;
   @RequestMapping("index")
   public String index(String name, Model model) {
       model.addAttribute("name", name);
       return "index";
   }
   @RequestMapping("user")
   public String test() {
       List<Map<String, Object>> list =
jdbcTemplate.queryForList("select * from t_user");
       System.out.println(list);
       return "user";
   }
}
4 前端渲染

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf" lang="ch">
<head>
   <meta charset="UTF-8">
   <title>⽤户界⾯</title>
</head>
<body>
⽤户界⾯
<table>
   <thead>
 <tr>
     <th>id</th>
     <th>⽤户名</th>
     <th>密码</th>
 </tr>
   </thead>
   <tbody>
   <tr th:each="item:${list}">
       <td
               th:text="${item.id}"></td>
       <td
               th:text="${item.username}"></td>
       <td
               th:text="${item.password}"></td>
   </tr>
   </tbody>
</table>
</body>
</html>

更多推荐

Springboot