spring初体验
springboot基于Spring4.0设计,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决
1.新建maven项目
2.
代码如下:
user
package com.zhongruan.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private int id;
private String username;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Hander
package com.zhongruan.controller;
import com.zhongruan.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.List;
@Controller
public class Handler {
/*@GetMapping("/index")
public String index() {
System.out.println("执行了index方法");
return "index";
}*/
@GetMapping("/index")
public String index(Model model) {
System.out.println("执行了index方法");
List<User> list = new ArrayList<>();
list.add(new User(1, "张三", 18));
model.addAttribute("list", list);
return "index";
}
}
Userhandle
package com.zhongruan.controller;
import com.zhongruan.bean.User;
import com.zhongruan.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@RestController
public class UserHandler {
@Autowired
UserRepository userRepository;
// Restful API
@GetMapping("/findAll")
public Collection<User> findAll() {
return userRepository.findAll();
}
// findAll?id=1 ===> findAll/1
@GetMapping("/findById/{id}")
public User findById(@PathVariable int id) {
return userRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody User user) {
userRepository.saveOrUpdate(user);
}
@DeleteMapping("/delete/{id}")
public void deleteById(@PathVariable int id) {
userRepository.deleteById(id);
}
}
Application
package com.zhongruan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<h1>
HelloWorld!!!!!
</h1>
<table>
<tr>
<th>用户ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}">2</td>
<!-- <td th:text="'用户名是'+${user.username}+'===='">李四</td>-->
<td th:text="|用户名是, ${user.username}|">李四</td>
<td th:text="${user.age}">20</td>
</tr>
</table>
</body>
</html>
application.yml
server:
port: 8093
spring:
thymeleaf:
mode: HTML
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongruan</groupId>
<artifactId>springboot_demo02</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.1.RELEASE</version>
</parent>
<!-- 引入依赖 -->
<dependencies>
<!-- Springboot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<!-- thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
结果
findall操作
添加(save)操作
删除操作