Spring Boot入門第二天:一個基於Spring Boot的Web應用,使用了Spring Data JPA和Freemarker。
今天打算從數據庫中取數據,並展示到視圖中。不多說,先上圖:
第一步:添加依賴。打開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.yws710.springboot</groupId> <artifactId>demo1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 使用Freemarker替代JSP做頁面渲染 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project>
第二步:配置數據源(前提是數據庫已經建好了)。在classpath:resources目錄下新建一個名為application.properties的文件。在文件中添加如下內容:
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemo1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
這是今天僅有的配置信息。
第三步:domain層。創建實體類,在domain包中新建一個User類:
package com.yws710.springboot.demo1.domain; import javax.persistence.*; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; @Entity @Table(name="t_user") public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private Date birthday; private BigDecimal salary; public User(){} public User(int id, String name, Date birthday, BigDecimal salary) { this.id = id; this.name = name; this.birthday = birthday; this.salary = salary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public BigDecimal getSalary() { return salary; } public void setSalary(BigDecimal salary) { this.salary = salary; } }
第四步:dao層。在repository包中新建一個UserRepository接口:
package com.yws710.springboot.demo1.repository; import com.yws710.springboot.demo1.domain.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Integer> { }
神奇的Spring Data JPA,自從用了它,我連最簡單的查詢語句都不會寫了。
第五步:service層。在service包中創建UserService接口,以及它的實現類UserServiceImpl。
package com.yws710.springboot.demo1.service; import com.yws710.springboot.demo1.domain.User; import java.util.List; public interface UserService { List<User> userList(); }
package com.yws710.springboot.demo1.service.impl; import com.yws710.springboot.demo1.domain.User; import com.yws710.springboot.demo1.repository.UserRepository; import com.yws710.springboot.demo1.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserRepository userRepository; @Override public List<User> userList() { return userRepository.findAll(); } }
第六步:控制層。在controller包中創建UserController類:
package com.yws710.springboot.demo1.controller; import com.yws710.springboot.demo1.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/user") public class UserController { @Autowired UserService userService; @RequestMapping("/list") public ModelAndView userList() throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("userList", userService.userList()); mv.setViewName("/user/list"); return mv; } }
第七步:視圖層。在classpath:resources中創建一個名為templates的文件夾,這是Spring Boot默認的視圖文件存放位置,創建一個user目錄,在user中建立一個名為list.ftl的文件(Freemarker模板文件),文件內容如下:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>用戶列表</title> <link href="/css/main.css" rel="stylesheet" /> </head> <body> <table> <thead> <tr> <th>ID</th> <th>姓名</th> <th>生日</th> <th>薪資</th> </tr> </thead> <tbody> <#list userList as user> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.birthday?string(‘yyyy-MM-dd‘)}</td> <td>${user.salary}</td> </tr> </#list> </tbody> </table> </body> </html>
為了展示靜態資源的訪問,特地在classpath:resources中創建了一個static文件夾。你猜的沒錯,這就是Spring Boot默認的靜態資源存放位置(其實默認的位置有三個,詳見Spring Boot官方文檔),main.css文件就存放在static中的css文件夾中。註意模板文件中引用css文件的寫法(感覺自己好啰嗦)。
第八步:創建啟動類。在demo1包中新建一個App類:
package com.yws710.springboot.demo1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
第九步:運行App類中的main方法。在瀏覽器中輸入 http://localhost:8080/user/list,頁面顯示結果如下:
最後啰嗦一句,註意項目結構圖中各文件的位置。
收工,明天打算使用日誌和阿裏巴巴的數據連接池Druid。
Spring Boot入門第二天:一個基於Spring Boot的Web應用,使用了Spring Data JPA和Freemarker。