ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot第六天---SpringBoot使用spring data jpa,并将数据显示到页面上

2019-11-05 18:52:13  阅读:339  来源: 互联网

标签:springboot jpa spring springframework user import com 页面


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.添加依赖:

https://www.cnblogs.com/xinghaonan/p/11798238.html

前边博客已写,请自行添加

2: 创建模板文件 (必须: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>

 

创建Controller接口

package com.xhn.controller;

import com.xhn.dao.UserDao;
import com.xhn.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层:

https://www.cnblogs.com/xinghaonan/p/11799854.html

前一天笔记中有写,不再重复

运行结果图:

 

 完成

 

标签:springboot,jpa,spring,springframework,user,import,com,页面
来源: https://www.cnblogs.com/xinghaonan/p/11800767.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有