@@ -0,0 +1,69 @@ | |||
<?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>1.0.01-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>2.1.3.RELEASE</version> | |||
</parent> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<java.version>1.8</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.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> |
@@ -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); | |||
} | |||
} |
@@ -0,0 +1,60 @@ | |||
package com.neo; | |||
import java.io.IOException; | |||
import javax.servlet.Filter; | |||
import javax.servlet.FilterChain; | |||
import javax.servlet.FilterConfig; | |||
import javax.servlet.ServletException; | |||
import javax.servlet.ServletRequest; | |||
import javax.servlet.ServletResponse; | |||
import javax.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 | |||
} | |||
} | |||
} | |||
@@ -0,0 +1,76 @@ | |||
package com.neo.model; | |||
import java.io.Serializable; | |||
import javax.persistence.Column; | |||
import javax.persistence.Entity; | |||
import javax.persistence.GeneratedValue; | |||
import javax.persistence.Id; | |||
@Entity | |||
public class User implements Serializable { | |||
private static final long serialVersionUID = 1L; | |||
@Id | |||
@GeneratedValue | |||
private Long id; | |||
@Column(nullable = false, unique = true) | |||
private String userName; | |||
@Column(nullable = false) | |||
private String passWord; | |||
@Column(nullable = false, unique = true) | |||
private String email; | |||
@Column(nullable = true, unique = true) | |||
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; | |||
} | |||
} |
@@ -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); | |||
} |
@@ -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; | |||
} | |||
} |
@@ -0,0 +1,21 @@ | |||
package com.neo.web; | |||
import java.util.Locale; | |||
import java.util.UUID; | |||
import javax.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"; | |||
} | |||
} |
@@ -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"; | |||
} | |||
} |
@@ -0,0 +1,31 @@ | |||
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("/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; | |||
} | |||
} |
@@ -0,0 +1,14 @@ | |||
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true | |||
spring.datasource.username=root | |||
spring.datasource.password=root | |||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | |||
spring.jpa.properties.hibernate.hbm2ddl.auto=create | |||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect | |||
#sql\u8F93\u51FA | |||
spring.jpa.show-sql=true | |||
#format\u4E00\u4E0Bsql\u8FDB\u884C\u8F93\u51FA | |||
spring.jpa.properties.hibernate.format_sql=true | |||
com.neo.title=\u7EAF\u6D01\u7684\u5FAE\u7B11 | |||
com.neo.description=\u5206\u4EAB\u751F\u6D3B\u548C\u6280\u672F |
@@ -0,0 +1,8 @@ | |||
body { | |||
padding-top: 50px; | |||
} | |||
.starter-template { | |||
padding: 40px 15px; | |||
text-align: center; | |||
} |
@@ -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> |
@@ -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> |
@@ -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"); | |||
} | |||
} |
@@ -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")); | |||
} | |||
} |
@@ -0,0 +1,57 @@ | |||
package com.neo.web; | |||
import static org.hamcrest.Matchers.equalTo; | |||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | |||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.springframework.boot.test.context.SpringBootTest; | |||
import org.springframework.http.MediaType; | |||
import org.springframework.mock.web.MockServletContext; | |||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |||
import org.springframework.test.context.junit4.SpringRunner; | |||
import org.springframework.test.context.web.WebAppConfiguration; | |||
import org.springframework.test.web.servlet.MockMvc; | |||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | |||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | |||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | |||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |||
import static org.hamcrest.Matchers.equalTo; | |||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | |||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |||
import com.neo.web.HelloController; | |||
@RunWith(SpringRunner.class) | |||
@SpringBootTest | |||
public class HelloControlerTests { | |||
private MockMvc mvc; | |||
@Before | |||
public void setUp() throws Exception { | |||
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); | |||
} | |||
@Test | |||
public void getHello() throws Exception { | |||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) | |||
.andExpect(MockMvcResultMatchers.status().isOk()) | |||
.andDo(MockMvcResultHandlers.print()) | |||
.andReturn(); | |||
} | |||
@Test | |||
public void testHello() throws Exception { | |||
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) | |||
.andExpect(status().isOk()) | |||
.andExpect(content().string(equalTo("Hello World"))); | |||
} | |||
} |
@@ -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>(); | |||
long xx=orderMinTime.get("123"); | |||
} | |||
} |