diff --git a/3.x/spring-boot-web/pom.xml b/3.x/spring-boot-web/pom.xml new file mode 100644 index 0000000..ddda10f --- /dev/null +++ b/3.x/spring-boot-web/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.neo + spring-boot-web + 3.0.0-SNAPSHOT + war + + spring-boot-web + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + + UTF-8 + 17 + + + + + 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.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + 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/3.x/spring-boot-web/src/main/java/com/neo/WebApplication.java b/3.x/spring-boot-web/src/main/java/com/neo/WebApplication.java new file mode 100644 index 0000000..ede8e47 --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/main/java/com/neo/WebConfiguration.java b/3.x/spring-boot-web/src/main/java/com/neo/WebConfiguration.java new file mode 100644 index 0000000..604679b --- /dev/null +++ b/3.x/spring-boot-web/src/main/java/com/neo/WebConfiguration.java @@ -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 + } + } +} + + + diff --git a/3.x/spring-boot-web/src/main/java/com/neo/model/User.java b/3.x/spring-boot-web/src/main/java/com/neo/model/User.java new file mode 100644 index 0000000..9914cd6 --- /dev/null +++ b/3.x/spring-boot-web/src/main/java/com/neo/model/User.java @@ -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; + } + +} \ No newline at end of file diff --git a/3.x/spring-boot-web/src/main/java/com/neo/repository/UserRepository.java b/3.x/spring-boot-web/src/main/java/com/neo/repository/UserRepository.java new file mode 100644 index 0000000..b5c0c63 --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/main/java/com/neo/util/NeoProperties.java b/3.x/spring-boot-web/src/main/java/com/neo/util/NeoProperties.java new file mode 100644 index 0000000..ab337bf --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/main/java/com/neo/web/HelloController.java b/3.x/spring-boot-web/src/main/java/com/neo/web/HelloController.java new file mode 100644 index 0000000..f9dad31 --- /dev/null +++ b/3.x/spring-boot-web/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 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"; + } + +} \ No newline at end of file diff --git a/3.x/spring-boot-web/src/main/java/com/neo/web/ThymeleafController.java b/3.x/spring-boot-web/src/main/java/com/neo/web/ThymeleafController.java new file mode 100644 index 0000000..c384bd1 --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/main/java/com/neo/web/UserController.java b/3.x/spring-boot-web/src/main/java/com/neo/web/UserController.java new file mode 100644 index 0000000..5753df1 --- /dev/null +++ b/3.x/spring-boot-web/src/main/java/com/neo/web/UserController.java @@ -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 getUsers() { + List users=userRepository.findAll(); + System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功"); + return users; + } +} \ No newline at end of file diff --git a/3.x/spring-boot-web/src/main/resources/application.properties b/3.x/spring-boot-web/src/main/resources/application.properties new file mode 100644 index 0000000..0aed4d0 --- /dev/null +++ b/3.x/spring-boot-web/src/main/resources/application.properties @@ -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 \ No newline at end of file diff --git a/3.x/spring-boot-web/src/main/resources/static/css/starter.css b/3.x/spring-boot-web/src/main/resources/static/css/starter.css new file mode 100644 index 0000000..e2fc60c --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/main/resources/static/images/favicon.png b/3.x/spring-boot-web/src/main/resources/static/images/favicon.png new file mode 100644 index 0000000..890ef06 Binary files /dev/null and b/3.x/spring-boot-web/src/main/resources/static/images/favicon.png differ diff --git a/3.x/spring-boot-web/src/main/resources/templates/hello.html b/3.x/spring-boot-web/src/main/resources/templates/hello.html new file mode 100644 index 0000000..c417054 --- /dev/null +++ b/3.x/spring-boot-web/src/main/resources/templates/hello.html @@ -0,0 +1,18 @@ + + + + + (navbar) + + + + Spring MVC / Thymeleaf / Bootstrap + (greeting) + The current time is (time) + + + + + + + diff --git a/3.x/spring-boot-web/src/main/resources/templates/layout.html b/3.x/spring-boot-web/src/main/resources/templates/layout.html new file mode 100644 index 0000000..422fbb7 --- /dev/null +++ b/3.x/spring-boot-web/src/main/resources/templates/layout.html @@ -0,0 +1,54 @@ + + + + + + + + + + (title) + + + + + + + + + + + Toggle navigation + + + + + Project name + + + + Home + About + Contact + + + + + + + + Spring MVC/Thymeleaf/Bootstrap + (greeting) + + + + + + + diff --git a/3.x/spring-boot-web/src/test/java/com/neo/WebApplicationTests.java b/3.x/spring-boot-web/src/test/java/com/neo/WebApplicationTests.java new file mode 100644 index 0000000..cd4cb77 --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/test/java/com/neo/model/UserRepositoryTests.java b/3.x/spring-boot-web/src/test/java/com/neo/model/UserRepositoryTests.java new file mode 100644 index 0000000..3b7d8ea --- /dev/null +++ b/3.x/spring-boot-web/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/3.x/spring-boot-web/src/test/java/com/neo/web/ProPertiesTest.java b/3.x/spring-boot-web/src/test/java/com/neo/web/ProPertiesTest.java new file mode 100644 index 0000000..2d76b35 --- /dev/null +++ b/3.x/spring-boot-web/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(); + orderMinTime.get("123"); + } + +} \ No newline at end of file diff --git a/README.md b/README.md index e46a673..178a7fc 100644 --- a/README.md +++ b/README.md @@ -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 示例 ---
(greeting)
The current time is (time)