SpringBoot + MyBatis + Thymeleaf 之 HelloWorld

15 篇文章 0 订阅
5 篇文章 0 订阅

目录

■相关知识

■代码

UserController2

helloThymeleafMyBatis.html

■效果

■DB数据

■其他(@RestController) 没有使用【Thymeleaf】



■相关知识

SpringBoot + MyBatis 之 Hello World_sun0322-CSDN博客

SpringBoot + Thymeleaf 之 HelloWorld_sun0322-CSDN博客

■代码

UserController2

package com.sxz.test.one.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sxz.test.one.entity.User;
import com.sxz.test.one.service.UserService;

// @RestController // 它是无效的。不支持模板引擎
@Controller
@RequestMapping("/user")
public class UserController2 {

    @Autowired
    UserService userService;

    @RequestMapping("/findAll2")
    public String findAll(Model model){
    	
    	List<User> userList = userService.findAll();
    	model.addAttribute("userList",userList);
    	
    	return "helloThymeleafMyBatis.html";
    	
    }
}

helloThymeleafMyBatis.html

<html xmlns:th="http://www.thymeleaf.org">

<html>
<head>
    <meta charset="UTF-8">
    <title>helloThymeleafMyBatis页面</title>
    <style type="text/css">
       table { background:#DCDFE6; width: 100%; }
       table th { background:#DDEBF7; }
   	   table tbody td { background:#FFFFFF; }
    </style>
</head>
<body bgcolor="#FFFF99">

	<table border="0" cellspacing="1" cellpadding="0">
		<tr>
			<th>id</th>
			<th>password</th>
			<th>权限</th>
		</tr>
	
		<tbody th:unless="${userList.isEmpty()}" >
			<tr th:each="userInfo : ${userList}">
				<td th:text="${userInfo.luId}"></td>
				<td th:text="${userInfo.luPass}"></td>
				<td th:text="${userInfo.luPermission}"></td>
			</tr>
		</tbody>
	</table>
	
</body>
</html>

■效果

https://10.10.10.194/user/findAll2

■DB数据

---

■其他(@RestController) 没有使用【Thymeleaf】

---

package com.sxz.test.one.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxz.test.one.entity.User;
import com.sxz.test.one.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
}

---

https://10.10.10.194/user/findAll

@RestController 返回 JSON 数据

[{"luId":"admin","luPass":"admin","luPermission":"3"},{"luId":"test001","luPass":"123456","luPermission":"1"},{"luId":"test002","luPass":"123456","luPermission":"1"},{"luId":"test003","luPass":"123456","luPermission":"1"},{"luId":"test005","luPass":"123456","luPermission":"1"},{"luId":"test006","luPass":"123456","luPermission":"1"},{"luId":"123456","luPass":"123456","luPermission":"1"}]

---

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值