SpringBoot使用spring data jpa及在頁面yaml中顯示


spring boot 整合spring Data JPA 頁面 yaml

做測試或者項目之前先捋一遍思路在下手,這樣出錯可以迅速查找到哪一步代碼出錯

1.1 需求 :查詢數據庫 ---》數據------》展示到頁面上

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 取屬性值

@Value("${key2}")

注意:

1,創建的bean對象里面只能用getset方法不要用注解會有沖突

2,創建模板文件 (必須:springboot約束大於配置)保存位置resources/templates 目錄下 文件后綴名.ftl

3,Controller文件中只能用Controller不能用RestController

按照思路走:

 

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', '李四');
 
        

 

 

 

2.添加依賴:

<!--添加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
復制代碼

 

3: 創建模板文件 (必須:springboot約束大於配置)保存位置resources/templates 目錄下 文件后綴名.ftl

 

 

代碼書寫:

復制代碼
<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;
//這個里面只能用getset方法不要用注解會有沖突
 
復制代碼

創建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> { }

 

 

 

運行結果圖:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM