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


    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

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

    运行结果图:

     完成

     

  • 相关阅读:
    go-micro registry 服务的注册和发现
    Element UI
    VUE常用插件和依赖的安装!
    Element dialog实现拖拽功能
    JAVA
    修改windows的host文件
    Vue
    Vue
    Vue
    Vue
  • 原文地址:https://www.cnblogs.com/xinghaonan/p/11800767.html
Copyright © 2020-2023  润新知