使用SSM框架完成用户信息查询的示例. 可以写前端页面只返回json信息 要求3:在代码中需要体现控制层(controller)调用服务层(service),服务层(service)调用dao层
时间: 2024-05-01 11:19:08 浏览: 109
假设我们要查询用户信息,具体实现如下:
前端页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息查询</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<label for="userId">用户ID:</label>
<input type="text" id="userId">
<br>
<button onclick="getUserInfo()">查询</button>
<br>
<br>
<table id="userInfoTable" border="1">
<tr>
<th>用户ID</th>
<th>用户名</th>
<th>性别</th>
<th>年龄</th>
</tr>
</table>
</body>
<script>
function getUserInfo() {
var userId = $('#userId').val();
$.ajax({
url: "/user/info",
data: {
userId: userId
},
success: function (result) {
$("#userInfoTable tr:not(:first)").empty();
if (result.code == "200") {
var data = result.data;
for (var i = 0; i < data.length; i++) {
var tr = $("<tr></tr>");
tr.append("<td>" + data[i].userId + "</td>");
tr.append("<td>" + data[i].username + "</td>");
tr.append("<td>" + data[i].gender + "</td>");
tr.append("<td>" + data[i].age + "</td>");
$("#userInfoTable").append(tr);
}
} else {
alert(result.msg);
}
}
});
}
</script>
</html>
```
控制层(controller):
```java
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/info")
@ResponseBody
public Result<List<User>> getUserInfo(@RequestParam("userId") Integer userId) {
List<User> userList = userService.getUserInfo(userId);
if (userList != null && userList.size() > 0) {
return new Result<>(200, "查询成功", userList);
} else {
return new Result<>(500, "查询失败", null);
}
}
static class Result<T> {
private int code;
private String msg;
private T data;
public Result(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
}
```
服务层(service):
```java
package com.example.service;
import com.example.dao.UserDao;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> getUserInfo(Integer userId) {
return userDao.getUserInfo(userId);
}
}
```
持久层(dao):
```java
package com.example.dao;
import com.example.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserDao {
List<User> getUserInfo(@Param("userId") Integer userId);
}
```
以上就是一个基于SSM框架实现的用户信息查询的示例,其中控制层(controller)调用服务层(service),服务层(service)调用dao层。前端页面只返回json信息。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)