涉及到很多xml配置没写:只写具体实现的所有类
1、实体类
对应数据表SYS_SBGL,
主键是SBBM,主键是自动生成的uuid
数据表内容如下(有图有真相):
package com.jinhetech.yogurt.sbgl.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; import org.springframework.cache.annotation.Cacheable; @Entity @Table(name="SYS_SBGL") @PrimaryKeyJoinColumn(name = "SBBM") @Cacheable(value = "baseCache") public class Sbgl implements Serializable{ private static final long serialVersionUID = -1423899016746755470L; //@SequenceGenerator(name = "MY_SUQUENCE", sequenceName = "SEQ_SYS_SBGL") //@GeneratedValue(generator="MY_SUQUENCE") //@GeneratedValue(generator="system_uuid") //@GenericGenerator(name="system_uuid", strategy="uuid") @Id private String sbbm; private String sbmc; private String sblx; private String sssx; private String ssjd; private String azsj; private String azry; private String sbzt; private String sbjd; private String sbwd; private String wxlxr; private String wxlxdh; private String sbywxcs; private String jzpylyz; private String mqsbcyr; private String bzsm; public String getSbbm() { return sbbm; } public void setSbbm(String sbbm) { this.sbbm = sbbm; } public String getSbmc() { return sbmc; } public void setSbmc(String sbmc) { this.sbmc = sbmc; } public String getSblx() { return sblx; } public void setSblx(String sblx) { this.sblx = sblx; } public String getSssx() { return sssx; } public void setSssx(String sssx) { this.sssx = sssx; } public String getSsjd() { return ssjd; } public void setSsjd(String ssjd) { this.ssjd = ssjd; } public String getAzsj() { return azsj; } public void setAzsj(String azsj) { this.azsj = azsj; } public String getAzry() { return azry; } public void setAzry(String azry) { this.azry = azry; } public String getSbzt() { return sbzt; } public void setSbzt(String sbzt) { this.sbzt = sbzt; } public String getSbjd() { return sbjd; } public void setSbjd(String sbjd) { this.sbjd = sbjd; } public String getSbwd() { return sbwd; } public void setSbwd(String sbwd) { this.sbwd = sbwd; } public String getWxlxr() { return wxlxr; } public void setWxlxr(String wxlxr) { this.wxlxr = wxlxr; } public String getWxlxdh() { return wxlxdh; } public void setWxlxdh(String wxlxdh) { this.wxlxdh = wxlxdh; } public String getSbywxcs() { return sbywxcs; } public void setSbywxcs(String sbywxcs) { this.sbywxcs = sbywxcs; } public String getJzpylyz() { return jzpylyz; } public void setJzpylyz(String jzpylyz) { this.jzpylyz = jzpylyz; } public String getMqsbcyr() { return mqsbcyr; } public void setMqsbcyr(String mqsbcyr) { this.mqsbcyr = mqsbcyr; } public String getBzsm() { return bzsm; } public void setBzsm(String bzsm) { this.bzsm = bzsm; } }
2、JSP页面(sbgl_list.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> <!doctype html> <c:set var="ctx" value="${pageContext.request.contextPath}" scope="session"/> <html> <head> <title>设备管理</title> <!-- zTree需要的样式和js --> <link rel="stylesheet" href="${ctx }/yogurtResource/assembly/ztree/css/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="${ctx }/yogurtResource/assembly/ztree/jquery.ztree.core-3.5.js"></script> <script type="text/javascript" src="${ctx }/yogurtResource/assembly/ztree/jquery.ztree.excheck-3.5.js"></script> <!-- 验证框架 --> <script type="text/javascript" src="${ctx }/yogurtResource/assembly/yogurt/user/formValidate.js"></script> <!-- 修改表单验证错误提示样式 --> <style type="text/css"> .table-bordered-ul li{ margin:0 5px; float:left} label.error { padding-left: 16px; padding-bottom: 2px; font-weight: bold; color: #F00; } </style> <script type="text/javascript"> function console(consoleTag, userId, stateTag) { // 添加用户。 if("add" == consoleTag) { $('#formid').prop("action", "${ctx}/sbgl/sbgl_add_list"); } } </script> </head> <body> <div class="clear"> <div class="panel panel-default yogurt_route" id="yogurt_route"> <a href="${ctx }/home/home_list">首页</a> > <a href="javascript:void(0);" onclick="navigate();">设备管理</a> > 设备列表 </div> </div> <ul id="bqnum"> <li><a href="javascript:void(0);" onclick="navigate();">导航</a> </li> <li><a href="javascript:void(0);" onclick="orgTree();">机构</a> </li> </ul> <!-- 根据条件查询 --> <div class="clear ppd10" style="line-height:32px"> <form action="${ctx }/sbgl/sbgl_list" method="post" id="selForm"> <div style="float:left; width:200px"> 设备名称: <input type="text" name="sbmc" id="sbmc" > </div> <div class="floatleft"> 设备类型: <input type="text" name="sblx" id="sblx" > </div> <div class="floatleft"> 所属市县: <input type="text" name="sssx" id="sssx" > </div> <div class="floatleft"> 基地名称: <input type="text" name="jdmc" id="jdmc" > </div> <div class="floatleft"> 设备状态: <select class="form-control" style="display:inline-block; width:80px" name="sbzt" id="sbzt"> <option value="">全部</option> <option value="Y">正常</option> <option value="N">异常</option> </select> </div> <button type="submit" class="btn btn-success yogurt_search_button" onclick="search();"> <img src="${ctx }/yogurtResource/images/Panel/search.png" width="18" height="18" /> </button> </form> </div> <!-- 新增、删除 --> <div class="panel panel-default" style="margin-top:10px"> <div class="yogurt_panel_title_core"> <span class="state_bold">共查询出数据结果:<span class="state_red">28</span>条</span> </div> <div class="more_core3"> <!-- <button type="button" class="btn btn-success yogurt_search_button2" onclick="console('add','');">新增设备</button> --> <button type="button" class="btn btn-success yogurt_search_button2" onclick="window.location.href='${ctx }/sbgl/sbgl_add_list' ">新增设备</button> <button type="button" class="btn btn-success yogurt_search_button2" onclick="console('delete','');">删除设备</button> </div> </div> <!-- 列表 --> <div class="yogurt_elementsbox_core clear"> <div class="yogurt_elementsbox_table_article" id="yogurt_elementsbox_table_article"> <table id="tableBody" border="0" cellspacing="0" cellpadding="0" class="table yogurt_elementsbox_table"> <thead> <tr class="yogurt_tab_back" onmouseover="this.className='yogurt_tab_trhover'" onmouseout="this.className='yogurt_tab_back'"> <th width="3%"><input type="checkbox" onClick="allchecked(this)"></th> <th width="10%">设备名称</th> <th width="10%">设备类型</th> <th width="10%">所属市县</th> <th width="10%">基地名称</th> <th width="10%">设备状态</th> <th width="10%">维修联系人</th> <th width="10%">联系电话</th> <th width="10%">设备持有人</th> <th width="17%" style="text-align:center;">操作</th> </tr> </thead> <tbody id="userTb"> <c:forEach var="sbgl" items="${lst}" varStatus="status"> <tr> <td><input type="checkbox" ></td> <td>${sbgl.sbmc }</td> <td>${sbgl.sblx }</td> <td>${sbgl.sssx }</td> <td>${sbgl.ssjd }</td> <td>${sbgl.sbzt }</td> <td>${sbgl.wxlxr }</td> <td>${sbgl.wxlxdh }</td> <td>${sbgl.mqsbcyr }</td> <td style="text-align:center"> <a href="javascript:void(0);" onclick="console('update', '${user.userId}')"> 修改</a> <a href="javascript:void(0);" onclick="showRoles('${user.userId}')">| 查看</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> <!--分页--> <div style="clear:both; height:50px"> <!--加载分页组件--> <div id="kkpager"></div> <script type="text/javascript"> var param = ""; $(function() { var totalPage = 5; var totalRecords = 5; var pageSize = 5; var pageNum = 5; //初始化分页控件 //有些参数是可选的,比如lang,若不传有默认值 kkpager.init({ pno: pageNum, //总页码 total: 5, //总数据条数 totalRecords: totalRecords, //链接前部 hrefFormer: 'user_list', //链接尾部 hrefLatter: '', getLink: function(n) { return getInitParam() + "&pageNum=" + n + "&pageSize=" + pageSize; }, lang: { prePageText: '上一页', nextPageText: '下一页', totalPageBeforeText: '共', totalPageAfterText: '页', totalRecordsAfterText: '条数据', gopageBeforeText: '转到', gopageButtonOkText: '确定', gopageAfterText: '页', buttonTipBeforeText: '第', buttonTipAfterText: '页' } }); //生成 kkpager.generPageHtml(); $('#mykkpagerselect').val(pageSize); }); function returnoption(pageSize) { window.location.href = getInitParam() + "&pageNum=1&pageSize=" + pageSize; } function getInitParam() { var serUserName = $('#ser_username').val(); var serActiveState = $('#ser_activeState').val(); var serOrgIds = $('#ser_orgId').val(); var serRoleIds = $('#ser_roleIds').val(); var attr = "?serUserName=" + encodeURI(encodeURI(serUserName)) + "&serActiveState=" + serActiveState + "&serOrgIds=" + serOrgIds + "&serRoleIds=" + serRoleIds; return "${ctx}/user/user_list" + attr; } </script> <!--分页结束--> </div> <!--分页end--> </body> </html>
3、Controller(SbglController.java)
@Controller("sbglController") @RequestMapping("/sbgl") public class SbglController extends BaseController { @Autowired private SbglDao dao; /** * 查询设备信息列表 * (在这里查询数据表所有数据,用Spring Data JPA框架自带的findAll()方法查询所有数据)。 */ @RequestMapping("sbgl_list") @RequiresRoles("sbgl/sbgl_list") public String getUserList(Model model) throws Exception {
//这两行就能查询到Sbgl类对应的数据表的所有数据库,并放在List集合里,供JSP页面调用 List<Sbgl> lst=new ArrayList<Sbgl>(); lst=(List<Sbgl>) dao.findAll(); System.out.println("aaa"); model.addAttribute("lst", lst); return "sbgl/sbgl_list"; } }
4、DAO层(Spring Data JPA只需要extends继承Spring Data JPA接口,不需要写SQL从数据表查询,上面提到的自带的findAll()方法就可以查到对应实体类数据表的所有数据 )
package com.jinhetech.yogurt.sbgl.dao; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; import com.jinhetech.yogurt.sbgl.entity.Sbgl; public interface SbglDao extends CrudRepository<Sbgl, String> { }