Spring MVC框架下 將數據庫內容前臺頁面顯示完整版【獲取數據庫人員參與的事件列表】
1.書寫jsp頁面包括要顯示的內容【people.jsp】
<!--
此處包括三個方面內容:
1.包含 文本輸入框 查詢按鈕 查詢結果顯示位置 (paging)
2.包括對按鈕(button)的操作
-->
<div>
<div class="m-b-md2">
<form class="form-inline" id="searchForm2"
name="searchForm2">
<div class="form-group">
<div class="padding">
<span class="h4">人員最近事件查詢:</span> <input class="input-medium" id="author" name="author">
</div>
</div>
<a href="javascript:void(0)" class="btn btn-info" id="search2
</form>
</div>
<div class="doc-buttons" style="padding: 10px 0">
<c:forEach items="${res}" var="key">
<button type="button" id="${fn:split(key.btn,‘,‘)[0]}" name="${fn:split(key.btn,‘,‘)[1]}" class="${fn:split(key.btn,‘,‘)[2]}">${fn:split(key.btn,‘,‘)[3]}</button>
</c:forEach>
</div>
<div id="paging2
</div>
2.新建查詢操作的實體類 entity 放在entity包中【PeopleForMap.java】
/**
*此處包括要訪問數據庫的表名 和表的主鍵 對數據庫做映射
*/
@TableSeg(tableName = "test", id="id")
public class PeopleFormMap extends FormMap<String,Object>{
/**
*
*/
private static final long serialVersionUID = 1L;
}
3.新建Service層 service層主要包括對增刪改查的實現【PeopleService.java】
public interface PeopleService extends BaseService{
}
4.新建Service的具體實現【PeopleServiceImpl.java】
@Service("peopleService")
public class PeopleServiceImpl extends BaseServiceImpl implements PeopleService{
}
5.新建Controller層 將service註入controller【PeopleController.java】
此處書寫要調用PeopleService執行的具體操作
@Controller
@RequestMapping("/people")
public class PeopleController extends BaseController {
@Inject
private PeopleService peopleService;
@ResponseBody
@RequestMapping("peoplelist")
public String peoplelist(String txtSelect) throws Exception {
return Common.BACKGROUND_PATH+"/system/riskforcast/people";
}
@ResponseBody
@RequestMapping("/findPeopleEvents")
public PageView findPeopleEvents(){
Map<String,Object> result = new HashMap<>();
PeopleFormMap peopleFormMap = getFormMap(PeopleFormMap.class);
PageView pageView = getPageView(peopleFormMap);
peopleFormMap.put("paging2",pageView);
peopleFormMap.put("mapper_id", "PeopleService.findPeopleEvents"); //根據mapper_id執行mapper中id=findPeopleEvents的sql操作
List<PeopleFormMap> re = peopleService.findByPage(peopleFormMap); //通過peopleService中的findByPage實現數據庫查詢
pageView.setRecords(re);
//result.put("pageView",pageView);
return pageView;
}
6.新建mapper表 主要包括對數據庫執行的Sql語句【people-mapper】
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="PeopleService">
<select id="findPeopleEvents" resultType="com.lanyuan.entity.PeopleFormMap">
select * from test
where author is not null and author != ‘‘ //查詢test表中author字段不為空的所有數據
<if test="author != null and author != ‘‘">
and author like ‘%${author}%‘
</if>
</select>
</mapper>
7.書寫js頁面 【people.js】
<!-- js頁面內容1.書寫查詢方法 並初始化 2.你要調用的sql語句路徑 3.你想查詢的字段
-->
$(document).ready(function() {
initEventList(); //初始化方法
function initEventList() {
grid = lyGrid({
pagId : ‘paging2‘,
l_column : [
{
colkey : "author",
name :"事件傳播者",
}, {
colkey : "page_title",
name : "文章標題"
}, {
colkey :"refer_url",
name :"轉載路徑"
}, {
colkey : "release_date",
name : "更新時間"
}
],
jsonUrl : rootPath + ‘/people/findPeopleEvents.shtml‘,
checkbox : true,
serNumber : true
});
$("#search2").click("click", function() {// 綁定查詢按扭
var searchParams = $("#searchForm2").serializeJSON();// 初始化傳參數
grid.setOptions({
data : searchParams
});
});
}
Spring MVC框架下 將數據庫內容前臺頁面顯示完整版【獲取數據庫人員參與的事件列表】