idea初步利用thymeleaf展現(xiàn)列表
上一篇文章簡單展現(xiàn)自己寫的列表;
這篇文章連接mysql數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)展現(xiàn)
主要三個(gè)文件
controller指定html界面
package com.example.appledemo.controller;
import com.example.appledemo.mapper.UserMapper;
import com.example.appledemo.pojo.User;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
import java.util.ArrayList;
import java.util.List;
@Controller
public class TestController {
@Resource
UserMapper userMapper;
@RequestMapping("/login")
public String login(Model model){
List<User> user = userMapper.findAll();
model.addAttribute("user",user);
return "login";
}
}
mapper寫數(shù)據(jù)庫sql查詢語句
package com.example.appledemo.mapper;
import com.example.appledemo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> findAll();
}
pojo中的user寫具體數(shù)據(jù)庫中的表包含哪些字段(這部分最好的方式寫出變量名字然后alt+insert自動生成getter和setter不容易出錯(cuò))
package com.example.appledemo.pojo;
import lombok.Getter;
@Getter
public class User {
private Integer userId;
private String userName;
private String userPass;
public void setUserName(String userName) {
this.userName = userName;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userPass='" + userPass + '\'' +
'}';
}
}
最后寫個(gè)login.html展現(xiàn)數(shù)據(jù)文章來源:http://www.zghlxwxcb.cn/news/detail-754874.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>springboot-thymeleaf demo</title>
</head>
<body>
<table border="1" width="1000">
<tr th:each="item,eee: ${user}">
<td th:text="${item.userId}"></td>
<td th:text="${item.userName}"></td>
<td th:text="${item.userPass}"></td>
</tr>
</table>
</body>
</html>
</html>
給出文件列表:
最后給出運(yùn)行結(jié)果:文章來源地址http://www.zghlxwxcb.cn/news/detail-754874.html
到了這里,關(guān)于idea利用spring框架整合thymeleaf展現(xiàn)數(shù)據(jù)庫數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!