commit e6f2dd154cf1095a83852db90f4dd8e9ec4d81ba Author: weishao Date: Thu Nov 3 15:10:12 2022 +0800 init diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e0a2f96 --- /dev/null +++ b/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + com.neo + spring-boot-web + 1.0.01-SNAPSHOT + war + + spring-boot-web + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.1.3.RELEASE + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.webjars.bower + jquery + 2.0.3 + + + org.webjars.bower + bootstrap + 3.0.3 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/src/main/java/com/neo/WebApplication.java b/src/main/java/com/neo/WebApplication.java new file mode 100644 index 0000000..ede8e47 --- /dev/null +++ b/src/main/java/com/neo/WebApplication.java @@ -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); + } +} diff --git a/src/main/java/com/neo/WebConfiguration.java b/src/main/java/com/neo/WebConfiguration.java new file mode 100644 index 0000000..cbf7177 --- /dev/null +++ b/src/main/java/com/neo/WebConfiguration.java @@ -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 + } + } +} + + + diff --git a/src/main/java/com/neo/model/User.java b/src/main/java/com/neo/model/User.java new file mode 100644 index 0000000..5320411 --- /dev/null +++ b/src/main/java/com/neo/model/User.java @@ -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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/neo/repository/UserRepository.java b/src/main/java/com/neo/repository/UserRepository.java new file mode 100644 index 0000000..b5c0c63 --- /dev/null +++ b/src/main/java/com/neo/repository/UserRepository.java @@ -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 findByUserName(String userName); + + User findByUserNameOrEmail(String username, String email); + +} \ No newline at end of file diff --git a/src/main/java/com/neo/util/NeoProperties.java b/src/main/java/com/neo/util/NeoProperties.java new file mode 100644 index 0000000..ab337bf --- /dev/null +++ b/src/main/java/com/neo/util/NeoProperties.java @@ -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; + } + +} diff --git a/src/main/java/com/neo/web/HelloController.java b/src/main/java/com/neo/web/HelloController.java new file mode 100644 index 0000000..ac3dee8 --- /dev/null +++ b/src/main/java/com/neo/web/HelloController.java @@ -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"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/neo/web/ThymeleafController.java b/src/main/java/com/neo/web/ThymeleafController.java new file mode 100644 index 0000000..c384bd1 --- /dev/null +++ b/src/main/java/com/neo/web/ThymeleafController.java @@ -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"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/neo/web/UserController.java b/src/main/java/com/neo/web/UserController.java new file mode 100644 index 0000000..b722a87 --- /dev/null +++ b/src/main/java/com/neo/web/UserController.java @@ -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 getUsers() { + List users=userRepository.findAll(); + System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); + return users; + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..bf2d2c8 --- /dev/null +++ b/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/src/main/resources/static/css/starter.css b/src/main/resources/static/css/starter.css new file mode 100644 index 0000000..e2fc60c --- /dev/null +++ b/src/main/resources/static/css/starter.css @@ -0,0 +1,8 @@ +body { + padding-top: 50px; +} + +.starter-template { + padding: 40px 15px; + text-align: center; +} \ No newline at end of file diff --git a/src/main/resources/static/images/favicon.png b/src/main/resources/static/images/favicon.png new file mode 100644 index 0000000..890ef06 Binary files /dev/null and b/src/main/resources/static/images/favicon.png differ diff --git a/src/main/resources/templates/hello.html b/src/main/resources/templates/hello.html new file mode 100644 index 0000000..c417054 --- /dev/null +++ b/src/main/resources/templates/hello.html @@ -0,0 +1,18 @@ + + + + +
(navbar)
+ +
+
+

Spring MVC / Thymeleaf / Bootstrap

+

(greeting)

+

The current time is (time)

+
+
+ + + + + diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html new file mode 100644 index 0000000..422fbb7 --- /dev/null +++ b/src/main/resources/templates/layout.html @@ -0,0 +1,54 @@ + + + + + + + + + + (title) + + + + + + + + +
+
+

Spring MVC/Thymeleaf/Bootstrap

+

(greeting)

+
+
+ + + + + diff --git a/src/test/java/com/neo/WebApplicationTests.java b/src/test/java/com/neo/WebApplicationTests.java new file mode 100644 index 0000000..cd4cb77 --- /dev/null +++ b/src/test/java/com/neo/WebApplicationTests.java @@ -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"); + } + +} diff --git a/src/test/java/com/neo/model/UserRepositoryTests.java b/src/test/java/com/neo/model/UserRepositoryTests.java new file mode 100644 index 0000000..3b7d8ea --- /dev/null +++ b/src/test/java/com/neo/model/UserRepositoryTests.java @@ -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")); + } + +} \ No newline at end of file diff --git a/src/test/java/com/neo/web/HelloControlerTests.java b/src/test/java/com/neo/web/HelloControlerTests.java new file mode 100644 index 0000000..f0a83c9 --- /dev/null +++ b/src/test/java/com/neo/web/HelloControlerTests.java @@ -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"))); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/neo/web/ProPertiesTest.java b/src/test/java/com/neo/web/ProPertiesTest.java new file mode 100644 index 0000000..93722c9 --- /dev/null +++ b/src/test/java/com/neo/web/ProPertiesTest.java @@ -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 orderMinTime=new HashMap(); + long xx=orderMinTime.get("123"); + } + +} \ No newline at end of file