html
vue
1.2 分析java
1 建立數據庫 user表mysql
2 持久層框架 spring data jpaweb
3 json jsp 靜態html freemarker spring
1.3頁面展現sql
HTML展現數據 vue.js angular.js 數據庫
動態頁面顯示 :每次請求都生成一次頁面json
jsp 本質上就是servlet 工程web 工程-springboot
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 取屬性值
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>
#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> { }