1.2 分析
1 創建數據庫 user表
2 持久層框架 spring data jpa
3 json jsp 靜態html freemarker
1.3頁面展示
HTML展示數據 vue.js angular.js
動態頁面顯示 :每次請求都生成一次頁面
jsp 本質上就是servlet 工程web 工程-
springbooot 項目工程中不推薦使用jsp
模板技術 freemarker
tymeleaf
velocity
使用步驟:
a : 添加依賴
b: 創建模板文件 保存位置resources/templates 目錄下 文件后綴名.ftl
c 編寫controller 把結果傳遞給模板
1.4 yaml 文件格式
key --value
1.4.1 語法 key: value
key1:
key2:
key3: value
1.4.2 取屬性值
1.:創建對應的數據表
CREATE TABLE `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(50) DEFAULT NULL, `password` VARCHAR(50) DEFAULT NULL, `name` VARCHAR(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '張三'); INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
<!--添加spring mvc 依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<!--添加springdatajpa的依賴-->
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
注意:版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
//注意繼承父類的版本 <version>2.0.2.RELEASE</version> </parent> <groupId>com.offcn</groupId> <artifactId>springbootdemo1</artifactId> <version>0.0.1-SNAPSHOT</version>
3:配置數據源
#DB Configation spring: datasource: driverClassName: com.mysql.jdbc.Driver //注意出現連接不上數據庫那么在數據庫名后面添加 ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT url: jdbc:mysql://127.0.0.1:3306/數據庫名 username: root password: 813100 jpa: database: MySQL show-sql: true generate-ddl: true
<html> <head> <title> spring boot</title> </head> <body> <table border="3px"> <thead> <tr> <th>id</th> <th>賬號</th> <th>密碼</th> <th>名字</th> </tr> </thead> <#list userList as user > <!--userList為controller中添加到域對象中的數據--> <tbody> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.name}</td> </tr> </tbody> </#list> </table> </body> </html>
創建實體類
注意:要在實體類上添加@Entity和@Table注解
package com.wf.entity;
import javax.persistence.*;
//指數據庫表對應的實體類
@Entity
//創建一個表,表名為user
//要連接的數據庫
@Table(name="user")
public class User {
//主鍵自增長
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private String name;
//這個里面只能用get和set方法不要用注解會有沖突
創建Controller接口
package com.wf.controller; import com.wf.dao.UserDao; import com.wf.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/page") public class PageUserController { @Autowired private UserDao userDao; //查詢數據庫數據
@RequestMapping("/user/list") public String getUserList(ModelMap map){ List<User> userList = userDao.findAll(); map.addAttribute("userList",userList); return "user"; //類似於springmvc中的內部視圖解析器,前后綴都不用寫 } }
UserDao層:
需要繼承JpaRepository接口
package com.wf.dao; import com.wf.entity.User; import org.springframework.data.jpa.repository.JpaRepository; //<User,Integer>第一個參數是指實體類,第二個參數是指實體類的id
public interface UserDao extends JpaRepository<User,Integer> { }



