帮我写一份基于ssm用ajax写的管理员查询所有部门编号为6的用户,有dao层,service层,coutroller层,mapper文件,level是等级,固定为6,可
时间: 2024-06-13 09:08:43 浏览: 171
好的,以下是基于SSM框架使用Ajax查询所有部门编号为6的用户的代码示例:
1. 前端页面代码(使用jQuery实现Ajax请求):
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询所有部门编号为6的用户</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<table id="userTable" border="1">
<thead>
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>部门编号</th>
<th>等级</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
$(function() {
$.ajax({
url: "getUserList",
type: "POST",
data: {deptId: 6},
dataType: "json",
success: function(data) {
if (data.code == 200) {
var userList = data.data;
for (var i = 0; i < userList.length; i++) {
var user = userList[i];
var tr = $("<tr></tr>");
tr.append("<td>" + user.id + "</td>");
tr.append("<td>" + user.name + "</td>");
tr.append("<td>" + user.deptId + "</td>");
tr.append("<td>" + user.level + "</td>");
$("#userTable tbody").append(tr);
}
} else {
alert(data.msg);
}
},
error: function() {
alert("请求失败!");
}
});
});
</script>
</body>
</html>
```
2. Controller层代码:
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/getUserList", method = RequestMethod.POST)
@ResponseBody
public Result getUserList(@RequestParam("deptId") Integer deptId) {
List<User> userList = userService.getUserListByDeptId(deptId);
return Result.success(userList);
}
}
```
3. Service层代码:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserListByDeptId(Integer deptId) {
return userMapper.getUserListByDeptId(deptId);
}
}
```
4. Dao层代码:
```java
public interface UserMapper {
List<User> getUserListByDeptId(Integer deptId);
}
```
5. Mapper文件代码:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserListByDeptId" parameterType="java.lang.Integer" resultType="com.example.entity.User">
SELECT * FROM user WHERE dept_id = #{deptId}
</select>
</mapper>
```
以上是基于SSM框架使用Ajax查询所有部门编号为6的用户的代码示例,希望能够帮到你。
阅读全文