学习笔记050——SpringBoot学习1

Spring Boot

Spring Boot 可以快速构建基于 Spring 的 Java 应用,可以快速整合各种框架,不需要开发者进行配置,Spring Boot 会实现自动装配

1、Spring Boot 配置文件

Spring Boot 去掉了所有的 XML 文件,因为 Spring Boot 可以实现自动装载,那么就不需要开发者手动在 XML 中进行组件的装配。

但是一些个性化的配置仍然需要开发者手动配置,比如数据库连接信息,Spring Boot 提供了 application 配置文件,进行个性化配置。

application 支持两种格式,properties 和 yml,Spring Boot 默认是 properties,但是实际开发中更推荐使用 yml,因为它更简单 更直观。

properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=dbc:mysql://localhost:3306/test12
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test12
    driver-class-name: com.mysql.cj.jdbc.Driver

properties 优先级更高

2、Spring Boot 整合视图层

Spring Boot 不推荐使用 JSP,推荐 Thymeleaf

1、pom.xml 添加 Thymeleaf 依赖

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

2、创建 application.yml 配置视图解析器

spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html

3、Controller 定义业务方法,返回数据和视图

@RequestMapping("/index")
public ModelAndView index(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("index");
    modelAndView.addObject("name", "张三");
    return modelAndView;
}

4、创建 index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${name}"></h1>
</body>
</html>

Thymeleaf 标签

th:text 用于文本显示,将业务数据的值填充到 HTML 代码中。

th:if 用于条件判断,对业务数据的值进行判断,如果条件成立则显示内容。

th:each 用于遍历集合,对后台传来的集合数据进行迭代,显示到页面中。

@RequestMapping("/index")
public ModelAndView index(){
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("index");
    modelAndView.addObject("name", "张三");
    modelAndView.addObject("score", 90);
    User user = new User();
    user.setId(1);
    user.setUsername("张三");
    user.setPassword("123123");
    user.setAge(22);
    modelAndView.addObject("user", user);
    modelAndView.addObject("list", this.userService.list());
    return modelAndView;
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${name}"></h1>
    <h2 th:if="${score>=90}">优秀</h2>
    <h2 th:if="${score<90}">良好</h2>
    <p th:text="${user.id}"></p>
    <p th:text="${user.username}"></p>
    <p th:text="${user.password}"></p>
    <p th:text="${user.age}"></p>
    <hr/>
    <table>
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
        </tr>
        <tr th:each="user:${list}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.username}"></td>
            <td th:text="${user.password}"></td>
            <td th:text="${user.age}"></td>
        </tr>
    </table>
</body>
</html>

3、Spring Boot 整合持久层

Spring Data JPA

1、pom.xml 导入相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2、创建实体类

package com.htl.entity;

import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Data
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column
    private String name;
    @Column
    private Integer score;
    @Column
    private Date birthday;
}

3、创建接口

package com.htl.repository;

import com.htl.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository extends JpaRepository<Student,Integer> {
}

4、创建 Controller

package com.htl.controller;

import com.htl.entity.Student;
import com.htl.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentRepository studentRepository;

    @RequestMapping("/list")
    @ResponseBody
    public List<Student> list(){
        return this.studentRepository.findAll();
    }

    @RequestMapping("/findById/{id}")
    @ResponseBody
    public Student findById(@PathVariable("id") Integer id){
        Student student = this.studentRepository.getById(id);
        return student;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值