不同用戶id--相同服務id--實現查詢展示出同一張表的不同字段的表信息


最近在公司有一個需求,需求就是不同的用戶登陸系統之後,調用同樣的方法,方法裏調用同樣的mapper接口,mapper接口調用同一個mapper.xml文件,查出來的也是同一張表,但是重點是查出來的這張表的字段不一樣,比如說A登陸之後查看這張表時可以看到10個字段,B登陸之後查看這張表卻只能看到5個字段(不是在前端頁面隱藏元素實現的)

一 在數據庫先建兩張表 result表 testUser表

result表

userId serverId resultMap
1 SC_find_Name_Age name,age
10 SC_find_Name_Age name,age,address
testUser表
id name age address
1 王盼 13 鄭州
2 張浩 16 北京
3 袁夢陽 34 上海

二 建三個model實體類

Result 實體類

package cn.com.qianlong.light.model;
public class Result extends PublicStr{

    private Integer userid;
    private String serverid;
    private String resultmap;
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public String getServerid() {
        return serverid;
    }
    public void setServerid(String serverid) {
        this.serverid = serverid == null ? null : serverid.trim();
    }
    public String getResultmap() {
        return resultmap;
    }
    public void setResultmap(String resultmap) {
        this.resultmap = resultmap == null ? null : resultmap.trim();
    }
}

testUser實體類

@Data
public class TestUser {
    private Integer id;
    private String name;
    private Integer age;
    private String address;
}

PublicStr實體類

@Data
public class PublicStr {
    private String publicStr;

    public String getPublicStr() {
        return publicStr;
    }

    public void setPublicStr(String publicStr) {
        this.publicStr = publicStr;
    }
}

三 ctrl層的方法

@Controller
@RequestMapping("/look")
public class TestUserCtrl extends LightBaseCtrl {
    /**
     * 查詢結果集
     */
    @RequestMapping("/result")
    @ResponseBody
    public List<TestUser> lookResult() throws Exception {
        String userId = UserUtil.getUserId();//得到登陸的用戶id
        String serverId="SC_find_Name_Age";//設置服務id
        //根據用戶id和服務id去查詢結果集
        Result re=(Result)this.getLightServiceUtil().callServiceWithParam("SC_find_Name_Age",userId,serverId);
        //從查到的對象中得到結果集項
        String result=re.getResultmap();
        //創建公共字段對象,存放結果集
        PublicStr publicStr=new PublicStr();
        publicStr.setPublicStr(result);
        //根據結果集查詢出對應的數據
        List<TestUser> list=(List<TestUser>) this.getLightServiceUtil().callServiceWithParam("findByResultMap", publicStr);
        return list;
    }
}

四 SC層方法

@LightServiceContainer
public class TestUserSC extends LightBaseService {
@Autowired
    private TestUserMapper testUserMapper;
@Autowired
    private ResultMapper resultMapper;
    @LightService(serviceName = "SC_find_Name_Age",memo = "查詢結果集",version = "V1.0")
    public Result getResult(String userId,String serverId){
        Result result = resultMapper.getResult(userId, serverId);
        return result;
    }
    @LightService(serviceName = "findByResultMap",memo = "根據結果集查詢結果",version = "V1.0")
    public List<TestUser> getUserByResultMap(PublicStr publicStr){
        List<TestUser> userByResultMap = testUserMapper.getUserByResultMap(publicStr);
        return userByResultMap;
    }
}

五 Mapper接口方法

public interface TestUserMapper {
    TestUser getList(Integer id);
    List<TestUser> getUserByResultMap(PublicStr publicStr);
}

六 Mapper.xml文件

 <select id="getUserByResultMap" parameterType="cn.com.qianlong.light.model.PublicStr" 
 resultMap="BaseResultMap">
        select ${publicStr} from testUser
 </select>

七 把查出來的不同字段的表信息展示在頁面上

前臺頁面是dataGrid寫的,使用ajax請求數據,並把請求的數據進行處理之後放到columnModel中就可以了。
因爲靜態的字段寫法是這樣的

 $('#dataGrid').dataGrid({
         searchForm: $("#searchForm"),
         lazyLoad:false,//懶加載
         //multiselect:true,//多選
         inputPageNo: $("#pageNo"),
         inputPageSize: $("#pageSize"),
         inputOrderBy: $("#orderBy"),
         columnModel:column,
         columnModel: [
             {header:'學號', name:'studentId', index:'a.student_id', width:200, align:"center", formatter: function(val, obj, row, act){
                 return '<a href="${ctxPath}/studentdemo/studentform?studentId='+row.studentId+'&op=edit" class="btnList" data-title="編輯用戶">'+(val||row.studentId)+'</a>';
             }},
             {header:'姓名', name:'studentName', index:'a.student_name', width:200, align:"center"},
             {header:'手機號', name:'mobile', index:'a.mobile', width:200, align:"center"},
             {header:'院系', name:'college', index:'a.college', width:200, align:"center"},
             {header:'專業', name:'major', index:'a.major', width:200, align:"center"},
             {header:'操作', name:'actions', width:260, sortable:false, title:false, align:"center",formatter: function(val, obj, row, act){
                 var actions = [];
                     actions.push('<a href="${ctxPath}/studentdemo/studentform?studentId='+row.studentId+'&op=edit" class="btnList" title="編輯用戶"><i class="fa fa-pencil"></i></a>&nbsp;');
                     // actions.push('<a href="${ctxPath}/studentGridDemo/editStudent?studentId='+row.studentId+'" class="btnList" title="停用用戶" data-confirm="確認要停用該用戶嗎?"><i class="glyphicon glyphicon-ban-circle"></i></a>&nbsp;');
                     actions.push('<a href="${ctxPath}/studentdemo/deletestudent?studentId='+row.studentId+'" class="btnList" title="刪除用戶" data-confirm="確認要刪除該用戶嗎?"><i class="fa fa-trash-o"></i></a>&nbsp;');
                     actions.push('<a href="javascript:" οnclick="deleteSelectRowData({gridId:\'dataGrid\',url:\'${ctxPath}/studentdemo/deletemorestudent\',paramField:\'studentIdList\',info:\'確認要刪除選中的學生信息嗎?\',resultHandle:handleFunction,resultCallBack:callBackFuncion});" title="刪除"><i class="fa fa-trash-o"></i></a>&nbsp;');
                     //deleteSelectRowData({gridId:'dataGrid',url:'${ctxPath}/studentdemo/deletemorestudent',paramField:'studentIdList',info:'確認要刪除選中的學生信息嗎?',resultHandle:handleFunction(ele),resultCallBack:callBackFuncion(gridId)});
                     return actions.join('');
             }}
         ],
		// 加載成功後執行事件
		ajaxSuccess: function(data){

        }
    });

所以動態字段就這樣寫

 $(function () {
        $.ajax({
            url:"/look/result",
            type:"post",
            dataType:"json",
            async:false,
            success:function (data) {
                alert(JSON.stringify(data));
                //聲明一個空數組column,column要賦值給columnModel,columnModel是dataGrid的表格的字段顯示
                // 因爲data裏面是[{},{},{}]這樣的形式,所以取出data的第一個{}即可,對data[0]進行遍歷
                var column=[];
                $.each(data[0],function (key,value) {//參數key和value是data[0]中的key和value
                    if(value!==null){   //查出來的包括null的value,要把null的value排除掉,只顯示有值的部分
                        column.push({   //給數組裏面添加{}
                            header:key,
                            name:key,
                            index:"a."+key,
                            width:100,
                            align:"center"
                        });

                    }
                });
                $('#dataGrid').dataGrid({
                    searchForm: $("#searchForm"),
                    lazyLoad:false,//懶加載
                    //multiselect:true,//多選
                    inputPageNo: $("#pageNo"),
                    inputPageSize: $("#pageSize"),
                    inputOrderBy: $("#orderBy"),
                    columnModel:column,     //把column數組賦值給columnModel
                    ajaxSuccess: function(result){

                    }
                });
            }
        });
    });

最後就實現了上面這個需求,代碼是運行成功之後的源碼。

八 效果展示

admin用戶登陸查看錶信息
在這裏插入圖片描述
jn_jsb用戶登陸查看錶信息
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章