add spring-boot-web

This commit is contained in:
ityouknow
2022-12-20 17:00:10 +08:00
parent a6a2a1e74f
commit f592de4a39
18 changed files with 542 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?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.neo</groupId>
<artifactId>spring-boot-web</artifactId>
<version>3.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-web</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,12 @@
package com.neo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}

View File

@@ -0,0 +1,60 @@
package com.neo;
import java.io.IOException;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebConfiguration {
@Bean
public RemoteIpFilter remoteIpFilter() {
return new RemoteIpFilter();
}
@Bean
public FilterRegistrationBean testFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("MyFilter");
registration.setOrder(1);
return registration;
}
public class MyFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest) srequest;
System.out.println("this is MyFilter,url :"+request.getRequestURI());
filterChain.doFilter(srequest, sresponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
}

View File

@@ -0,0 +1,72 @@
package com.neo.model;
import java.io.Serializable;
import jakarta.persistence.*;
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, unique = true, length = 20)
private String userName;
@Column(nullable = false)
private String passWord;
@Column(nullable = false, unique = true, length = 30)
private String email;
@Column(nullable = true, unique = true, length = 30)
private String nickName;
@Column(nullable = false)
private String regTime;
public User() {
super();
}
public User(String nickName,String email,String userName, String passWord, String regTime) {
super();
this.email = email;
this.nickName = nickName;
this.passWord = passWord;
this.userName = userName;
this.regTime = regTime;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getRegTime() {
return regTime;
}
public void setRegTime(String regTime) {
this.regTime = regTime;
}
}

View File

@@ -0,0 +1,12 @@
package com.neo.repository;
import com.neo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserName(String userName);
User findByUserNameOrEmail(String username, String email);
}

View File

@@ -0,0 +1,26 @@
package com.neo.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class NeoProperties {
@Value("${com.neo.title}")
private String title;
@Value("${com.neo.description}")
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@@ -0,0 +1,21 @@
package com.neo.web;
import java.util.Locale;
import java.util.UUID;
import jakarta.servlet.http.HttpSession;
import com.neo.model.User;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(Locale locale, Model model) {
return "Hello World";
}
}

View File

@@ -0,0 +1,26 @@
package com.neo.web;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ThymeleafController {
@RequestMapping("/hi")
public String hello(Locale locale, Model model) {
model.addAttribute("greeting", "Hello!");
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("currentTime", formattedDate);
return "hello";
}
}

View File

@@ -0,0 +1,43 @@
package com.neo.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neo.model.User;
import com.neo.repository.UserRepository;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/add")
public User saveUser(String key) {
User user=new User();
user.setUserName("aa"+key);
user.setEmail("ityouknow@126.com"+key);
user.setNickName("微笑"+key);
user.setPassWord("123456"+key);
user.setRegTime("2022-12-20"+key);
userRepository.save(user);
return user;
}
@RequestMapping("/getUser")
public User getUser() {
User user=userRepository.findByUserName("aa");
System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
return user;
}
@RequestMapping("/getUsers")
public List<User> getUsers() {
List<User> users=userRepository.findAll();
System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
return users;
}
}

View File

@@ -0,0 +1,17 @@
spring.datasource.url=jdbc:mysql://174.137.48.31:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=itmooc_tech
spring.datasource.password=e8tEzFMApR
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# ????????
spring.datasource.hikari.maxLifeTime=600000
spring.jpa.properties.hibernate.hbm2ddl.auto=create
#sql\u8F93\u51FA
spring.jpa.show-sql=true
#format\u4E00\u4E0Bsql\u8FDB\u884C\u8F93\u51FA
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.open-in-view=false
com.neo.title=\u7EAF\u6D01\u7684\u5FAE\u7B11
com.neo.description=\u5206\u4EAB\u751F\u6D3B\u548C\u6280\u672F

View File

@@ -0,0 +1,8 @@
body {
padding-top: 50px;
}
.starter-template {
padding: 40px 15px;
text-align: center;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,18 @@
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="layout :: htmlhead" th:with="title='Hello'"></head>
<body>
<div th:replace="layout :: navbar">(navbar)</div>
<div class="container">
<div class="starter-template">
<h1>Spring MVC / Thymeleaf / Bootstrap</h1>
<p class="lead" th:text="${greeting}">(greeting)</p>
<p>The current time is <span th:text="${currentTime}">(time)</span></p>
</div>
</div>
<div th:include="layout :: footer" id="footer">(footer)</div>
</body>
</html>

View File

@@ -0,0 +1,54 @@
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="htmlhead">
<meta charset="utf-8"></meta>
<meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
<meta name="description" content=""></meta>
<meta name="author" content=""></meta>
<link rel="shortcut icon" type="image/png" th:href="@{/images/favicon.png}"></link>
<title th:text="${title}">(title)</title>
<link th:href="@{/webjars/bootstrap/3.0.3/dist/css/bootstrap.css}" rel="stylesheet"></link>
<link th:href="@{/css/starter.css}" rel="stylesheet"></link>
</head>
<body>
<div th:fragment="navbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="starter-template">
<h1>Spring MVC/Thymeleaf/Bootstrap</h1>
<p class="lead" th:text="${greeting}">(greeting)</p>
</div>
</div>
<div th:fragment="footer" id="footer">
<div class="container">
<p class="muted credit">Spring MVC/Thymeleaf/Bootstrap Project Template</p>
</div>
<script th:src="@{/webjars/jquery/2.0.3/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/3.0.3/js/bootstrap.min.js}"></script>
</div>
</body>
</html>

View File

@@ -0,0 +1,18 @@
package com.neo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
@Test
public void contextLoads() {
System.out.println("hello web");
}
}

View File

@@ -0,0 +1,38 @@
package com.neo.model;
import java.text.DateFormat;
import java.util.Date;
import com.neo.repository.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Test
public void test() throws Exception {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
userRepository.save(new User("aa1", "aa@126.com", "aa", "aa123456",formattedDate));
userRepository.save(new User("bb2", "bb@126.com", "bb", "bb123456",formattedDate));
userRepository.save(new User("cc3", "cc@126.com", "cc", "cc123456",formattedDate));
// Assert.assertEquals(9, userRepository.findAll().size());
Assert.assertEquals("bb2", userRepository.findByUserNameOrEmail("bb", "xxx126.com").getNickName());
userRepository.delete(userRepository.findByUserName("aa"));
}
}

View File

@@ -0,0 +1,35 @@
package com.neo.web;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.neo.util.NeoProperties;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProPertiesTest {
@Autowired
private NeoProperties neoProperties;
@Test
public void getHello() throws Exception {
System.out.println(neoProperties.getTitle());
Assert.assertEquals(neoProperties.getTitle(), "纯洁的微笑");
Assert.assertEquals(neoProperties.getDescription(), "分享生活和技术");
}
@Test
public void testMap() throws Exception {
Map<String, Long> orderMinTime=new HashMap<String, Long>();
orderMinTime.get("123");
}
}

View File

@@ -23,6 +23,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开
- [spring-boot-hello](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-hello)Spring Boot 3.0 Hello World 示例
- [spring-boot-helloworld](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-helloWorld)Spring Boot 3.0 Hello World Test 单元测试示例
- [spring-boot-web](https://github.com/ityouknow/spring-boot-examples/tree/master/3.x/spring-boot-web)Spring Boot 3.0 Hello World web 示例
---