Day_34 springboot01

本文详细介绍了Spring Boot的优势、特性,以及如何搭建一个Spring Boot项目,包括添加Thymeleaf和Web依赖、编写Controller、设置配置文件、集成JdbcTemplate操作数据库。此外,还展示了如何启动项目、自动打开浏览器,并提供了关键代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. springboot 优势

创建独⽴的 Spring 应⽤程序
嵌⼊的 Tomcat,⽆需部署 WAR ⽂件
简化 Maven 配置
⾃动配置 Spring
提供⽣产就绪型功能,如指标,健康检查和外部配置

2. 特性

为基于 Spring 的开发提供更快的⼊⻔体验
开箱即⽤,没有代码⽣成,也⽆需 XML 配置。同时也可以修改默认值来满⾜特定的需求
提供了⼀些⼤型项⽬中常⻅的⾮功能特性,如嵌⼊式服务器、安全、指标,健康检测、外部配置等
Spring Boot 并不是对 Spring 功能上的增强,⽽是提供了⼀种快速使⽤ Spring 的⽅式

3. 搭建springboot 项目

 

 4. 访问前端页面

4.1 引依赖

     <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>

4.2 controller 编写

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

4.3 页面编写

放到resource/templates

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

5. 读取后端值

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

6. 配置文件

读取resource/application.properties \ application.yml

端⼝号配置,项⽬前缀配置

yml

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

properties

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

 7. 项目打包

mvn clean package

运⾏ java -jar jar包

mvn clean package

在target⽬录下⽣成⼀个jar包

如果想启动这个项⽬只要有java环境就可以了

java -jar jar包

启动项⽬指定参数

java -jar -Dserver.port=8082 jar包 springboot读取配置的顺序

1. 启动参数上的配置

2. jar包⽬录下config/application.properties

3. classpath:application.properties

4. classpath:application.yml
 

8. 集成jdbcTemplate

8.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>

8.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

8.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";
   }
}

8.4 循环渲染

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 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>

9. 自动打开浏览器

package com.tledu.springboot01.core;
import lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class OpenBrowser implements CommandLineRunner {
    private final Environment environment;
    @Override
    public void run(String... args) throws Exception {
        System.out.println("应⽤已经准备就绪 ... 启动浏览器并⾃动加载指定的⻚⾯
... ");
        try {
            String port = environment.getProperty("server.port");
            String contextPath =
environment.getProperty("server.servlet.context-path");
            if(port == null){
           port = "8080";
           }
            contextPath = contextPath == null?"":contextPath;
            //指定⾃⼰项⽬的路径
            Runtime.getRuntime().exec("cmd   /c   start  
http://localhost:"+port+contextPath);
       } catch (Exception ex) {
            ex.printStackTrace();
       }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值