Welcome to Spring Boot
代码结构
src/main/java 下 controller层,路由功能
dao层,数据库的访问
domain,bean的存放
service,业务层
application.java,spring boot的主启动程序 src/main/resources/application.properties ,spring boot的配置文件
详细代码说明
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.jwen</groupId>
<artifactId>login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>login</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
</dependencies>
</plugin>
</plugins>
</build> </project>
简易说明:
thymeleaf -- 用来渲染模板,spring boot 不建议使用JSP
devtools -- 用来热部署,可以只关注coding,保存后自行restart
jpa -- 数据库访问,很好很强大
LoginApplication.java
package com.jwen.login; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class LoginApplication { public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
}
}
UserController.java
package com.jwen.login.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.jwen.login.domain.User;
import com.jwen.login.service.UserService; @Controller
@EnableAutoConfiguration
public class UserController { @Autowired
private UserService userService; @RequestMapping("/")
@ResponseBody
String welcome() {
return "welcome my first spring boot project";
} @RequestMapping("/notVerify")
@ResponseBody
String notVerify() {
return "username or password NOT correct";
} @RequestMapping("/login")
String login(Model model) {
model.addAttribute("user", new User());
return "login";
} @RequestMapping("/register")
String register(Model model) {
model.addAttribute("user", new User());
return "register";
} @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
@ResponseBody
String registerUser(User user, Model model) {
return userService.registerUser(user);
} @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
String userLogin(User user, Model model) {
boolean verify = userService.verifyUser(user);
if (verify) {
model.addAttribute("name", user.getName());
model.addAttribute("password", user.getPassword());
return "result";
} else {
return "redirect:/notVerify";
} } }
@RequestMapping("/login") ensures that HTTP requests to/login
are mapped to thelogin()
method,这个注解可以使指定的uri指向到该方法
@ResponseBody 返回response,一般return为string的话,页面就直接返回该string 配置application.properties,指向html文件:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
会去识别resources/templates下的html文件,根据return回来的string来匹配
@RequestMapping("/login")
String login(Model model) {
model.addAttribute("user", new User());
return "login";
}
这里没有@ResponseBody ,因此会去匹配到login.html