Dynamic fetching-query the table first when adding data

For example, when we add employee information, we need to select the department name assigned by the employee. This department name cannot be written in the form for adding employee information. Once the department is withdrawn, it must be modified on the front desk page to add employee information. The department information in the form form, so we need to dynamically obtain the currently opened department from the department table, so that the department information is automatically displayed in the form to add employee information. 

How to achieve this function? When the user clicks on the new employee information, the order of background execution is to first query the department number and department name in the department table, and then encapsulate the query results into List<Dept>, and then the The result is returned to the form page where the employee information is added.

We still write based on the previous article:  https://blog.csdn.net/weixin_42629433/article/details/83379538

Let's do specific demonstration operations below:

1. The user clicks on the new employee information request:

<button type="button" class="btn btn-default" title="新建" onclick="location.href='${pageContext.request.contextPath}/dept/findDeptIdsAndDeptNames.do'">
    <i class="fa fa-file-o"></i> 新建
</button>

2. The background receives the request to query the department number and department name in the department table

controller layer:

@RequestMapping("/findDeptIdsAndDeptNames.do")
public ModelAndView findDeptId(){
    ModelAndView mv = new ModelAndView();
    List<Dept> deptIdsAndDeptNames = deptService.findDeptIdsAndDeptNames();
    System.out.println(deptIdsAndDeptNames);
    mv.addObject("deptIdsAndDeptNamesList", deptIdsAndDeptNames);
    mv.setViewName("emp-add");
    return mv;
}

Service layer interface:

//查询所有 deptId 和对应的 deptName
public List<Dept> findDeptIdsAndDeptNames();

Service layer interface implementation class:

public List<Dept> findDeptIdsAndDeptNames() {
    return deptDao.findDeptIdsAndDeptNames();
}

Dao layer interface:

//查询所有 deptId 和对应的 deptName
@Select("select id,deptName from dept where deptStatus = 1")
public List<Dept> findDeptIdsAndDeptNames();

3. Add employee information page

The front desk obtains the ModelAndView view of the back-end controller layer, and carries the set of department number and department name queried in the view to jump to the view page that specifies adding employee information. The changes in the front desk selection department are as follows:

<div class="col-md-2 title">部门</div>
<div class="col-md-4 data">
    <select id="deptId" class="form-control select2" style="width: 100%" name="deptId">
        <option value="noSelect" selected="selected"><--请选择--></option>
        <c:forEach items="${deptIdsAndDeptNamesList}" var="Dept">
	    <option value=${Dept.id}>${Dept.deptName}</option>
	</c:forEach>
    </select>
</div>

 

 

Code download:  https://pan.baidu.com/s/1M_VVsq4oWbucaESeqmxjug

***This code needs to be run and used under jdk1.8***

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42629433/article/details/83501865