我这里用的是springboot、thymeleaf和mybatis-plus。
首先要在pom文件中加入相应依赖,如thymeleaf依赖,数据库驱动依赖等。
html文件中要将html标签改为
<html xmlns:th="http://www.thymeleaf.org"></html>
表示要使用thymeleaf
html主要代码:
<table class="table">
<thead>
<tr>
<th>序号</th>
<th>ip</th>
<th>visitorName</th>
<th>state</th>
<th>visitTime</th>
<th>customerServiceId</th>
</tr>
</thead>
<tbody>
<!--/*@thymesVar id="visitor_list" type=""*/-->
<tr th:each="cs,rowInfo : ${visitor_list}">
<th>[[${rowInfo.count}]]</th>
<td>[[${cs.ip}]]</td>
<td>[[${cs.visitorname}]]</td>
<td>[[${cs.state}]]</td>
<td>[[${cs.visittime}]]</td>
<td>[[${cs.customerserviceid}]]</td>
</tr>
</tbody>
</table>
controller下对应的代码
@Controller
@RequestMapping("/visitorInfo")
public class VisitorInfoController {
@Autowired
private IVisitorInfoService iVisitorInfoService;
@RequestMapping("/visitor")
public String visitor(Model m){
// 查询所有
// List<T> list();
ArrayList<VisitorInfo> arrayList= new ArrayList<>();
arrayList= (ArrayList<VisitorInfo>) iVisitorInfoService.list();
m.addAttribute("visitor_list",arrayList);//将arrayList隐藏到visitor_list中
return "forward:/tables";
}
}
pojo下的实体类
@Data
@EqualsAndHashCode(callSuper = false)
public class VisitorInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String ip;
@TableField("visitorName")
private String visitorname;
private String terminal;
private String browser;
@TableField("screenSize")
private String screensize;
private String device;
@TableField("customerServiceId")
private Integer customerserviceid;
private String state;
@TableField("visitDuration")
private Integer visitduration;
@TableField("visitTime")
private LocalDateTime visittime;
}
注意:html页面下的变量名要与实体类对应,而不是与数据库属性名对应。