@PostMapping("/updateDormitory") public String update(Dormitory dormitory) { int i = dormitoryService.update(dormitory); if (i >0){ return "success"; } else { return "error"; } }
时间: 2024-02-14 21:32:11 浏览: 101
这段代码是一个Spring Boot的Controller方法,使用了@PostMapping注解,表示该方法处理POST请求,并映射到"/updateDormitory"路径上。
该方法接受一个参数dormitory,类型为Dormitory对象,用于接收客户端提交的宿舍信息。
方法内部通过调用dormitoryService的update方法来更新宿舍信息,并将返回值赋给变量i。
如果更新成功(i大于0),则返回字符串"success",表示更新成功。否则,返回字符串"error",表示更新失败。
请注意,该代码段仅包含了更新宿舍信息的逻辑,并未包含任何与前端交互和视图渲染的代码。
相关问题
<input type="text" id="dormitory" name="dormitory" readonly="readonly" value="${userinfo.dormitory==null?"待入住":userinfo.dormitory}" class="layui-input"/>如何更改实现在分配后重新读宿舍并写入信息,请给出具体例子
如果你想实现在分配后重新读宿舍并写入信息,你可以在前端页面上添加一个修改宿舍的按钮,当用户点击该按钮时,前端会发送一个Ajax请求到后端,后端会先查询该用户的宿舍信息,然后将查询结果返回给前端,前端再将该宿舍信息显示在一个表单中,用户可以在表单中修改宿舍信息,然后再次点击“保存”按钮,前端会将修改后的宿舍信息发送到后端进行更新。下面是一个具体的例子:
前端代码:
```html
<div class="layui-form-item">
<label class="layui-form-label">宿舍号</label>
<div class="layui-input-inline">
<input type="text" id="dormitory" name="dormitory" readonly="readonly" value="${userinfo.dormitory==null?'待入住':userinfo.dormitory}" class="layui-input"/>
</div>
<div class="layui-form-mid layui-word-aux">
<button class="layui-btn layui-btn-xs" id="changeDormBtn">修改</button>
</div>
</div>
<div class="layui-form-item layui-hide" id="changeDormForm">
<label class="layui-form-label">宿舍号</label>
<div class="layui-input-inline">
<input type="text" id="newDorm" name="newDorm" class="layui-input"/>
</div>
<div class="layui-form-mid layui-word-aux">
<button class="layui-btn layui-btn-xs" id="saveDormBtn">保存</button>
</div>
</div>
<script>
$(function() {
// 显示修改宿舍表单
$('#changeDormBtn').click(function() {
$('#dormitory').parent().addClass('layui-hide');
$('#changeDormForm').removeClass('layui-hide');
});
// 保存修改后的宿舍信息
$('#saveDormBtn').click(function() {
var newDorm = $('#newDorm').val();
$.ajax({
url: '/user/updateDorm',
type: 'POST',
data: {newDorm: newDorm},
success: function(data) {
if (data.code == 0) {
$('#dormitory').val(newDorm);
$('#newDorm').val('');
$('#changeDormForm').addClass('layui-hide');
$('#dormitory').parent().removeClass('layui-hide');
layer.msg('修改成功');
} else {
layer.msg('修改失败');
}
},
error: function() {
layer.msg('网络错误');
}
});
});
});
</script>
```
后端代码:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/updateDorm")
@ResponseBody
public Result updateDorm(@RequestParam("newDorm") String newDorm, HttpSession session) {
User user = (User)session.getAttribute("user");
user.setDormitory(newDorm);
int count = userService.updateUser(user);
if (count > 0) {
return Result.success();
} else {
return Result.fail();
}
}
}
```
这里假设你使用了SpringMVC框架来搭建你的后端服务,并且你已经实现了UserService中的updateUser方法用于更新用户信息。在上面的代码中,我们使用了@RequestParam注解来获取前端发送过来的newDorm参数,然后更新用户信息并返回一个Result对象,表示更新是否成功。在前端,我们使用了jQuery来发送Ajax请求,并且使用了layer插件来显示提示信息。
Java宿舍管理系统hsqldb:1.宿舍楼的增删改查和浏览;2.宿舍的增删改查和浏览;3.学生入学宿舍入住和退住;4.学生调换宿舍;5.根据学生信息查询这些功能的代码实现
好的,这是一个比较复杂的问题。我会尽可能地简洁明了地回答你。
我们可以使用Java来开发宿舍管理系统,同时使用hsqldb作为数据库。下面是实现这些功能的代码示例:
1. 宿舍楼的增删改查和浏览
添加宿舍楼:
```java
public void addBuilding(Building building) {
String sql = "INSERT INTO building (building_name, building_address) VALUES (?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, building.getName());
pstmt.setString(2, building.getAddress());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
删除宿舍楼:
```java
public void deleteBuilding(int id) {
String sql = "DELETE FROM building WHERE building_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
更新宿舍楼:
```java
public void updateBuilding(Building building) {
String sql = "UPDATE building SET building_name=?, building_address=? WHERE building_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, building.getName());
pstmt.setString(2, building.getAddress());
pstmt.setInt(3, building.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
查询宿舍楼:
```java
public List<Building> getBuildings() {
List<Building> buildings = new ArrayList<>();
String sql = "SELECT * FROM building";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Building building = new Building();
building.setId(rs.getInt("building_id"));
building.setName(rs.getString("building_name"));
building.setAddress(rs.getString("building_address"));
buildings.add(building);
}
} catch (SQLException e) {
e.printStackTrace();
}
return buildings;
}
```
2. 宿舍的增删改查和浏览
添加宿舍:
```java
public void addDormitory(Dormitory dormitory) {
String sql = "INSERT INTO dormitory (building_id, dormitory_name, capacity) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, dormitory.getBuildingId());
pstmt.setString(2, dormitory.getName());
pstmt.setInt(3, dormitory.getCapacity());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
删除宿舍:
```java
public void deleteDormitory(int id) {
String sql = "DELETE FROM dormitory WHERE dormitory_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
更新宿舍:
```java
public void updateDormitory(Dormitory dormitory) {
String sql = "UPDATE dormitory SET building_id=?, dormitory_name=?, capacity=? WHERE dormitory_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, dormitory.getBuildingId());
pstmt.setString(2, dormitory.getName());
pstmt.setInt(3, dormitory.getCapacity());
pstmt.setInt(4, dormitory.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
查询宿舍:
```java
public List<Dormitory> getDormitories() {
List<Dormitory> dormitories = new ArrayList<>();
String sql = "SELECT * FROM dormitory";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Dormitory dormitory = new Dormitory();
dormitory.setId(rs.getInt("dormitory_id"));
dormitory.setBuildingId(rs.getInt("building_id"));
dormitory.setName(rs.getString("dormitory_name"));
dormitory.setCapacity(rs.getInt("capacity"));
dormitories.add(dormitory);
}
} catch (SQLException e) {
e.printStackTrace();
}
return dormitories;
}
```
3. 学生入学宿舍入住和退住
添加学生入住记录:
```java
public void addCheckInRecord(CheckInRecord checkInRecord) {
String sql = "INSERT INTO check_in_record (student_id, dormitory_id, check_in_time) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, checkInRecord.getStudentId());
pstmt.setInt(2, checkInRecord.getDormitoryId());
pstmt.setTimestamp(3, new Timestamp(checkInRecord.getCheckInTime().getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
添加学生退住记录:
```java
public void addCheckOutRecord(CheckOutRecord checkOutRecord) {
String sql = "INSERT INTO check_out_record (student_id, dormitory_id, check_out_time) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, checkOutRecord.getStudentId());
pstmt.setInt(2, checkOutRecord.getDormitoryId());
pstmt.setTimestamp(3, new Timestamp(checkOutRecord.getCheckOutTime().getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
4. 学生调换宿舍
更新学生入住记录:
```java
public void updateCheckInRecord(CheckInRecord checkInRecord) {
String sql = "UPDATE check_in_record SET dormitory_id=?, check_in_time=? WHERE student_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, checkInRecord.getDormitoryId());
pstmt.setTimestamp(2, new Timestamp(checkInRecord.getCheckInTime().getTime()));
pstmt.setInt(3, checkInRecord.getStudentId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
```
5. 根据学生信息查询
根据学生ID查询学生入住记录:
```java
public List<CheckInRecord> getCheckInRecordsByStudentId(int studentId) {
List<CheckInRecord> checkInRecords = new ArrayList<>();
String sql = "SELECT * FROM check_in_record WHERE student_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, studentId);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
CheckInRecord checkInRecord = new CheckInRecord();
checkInRecord.setStudentId(rs.getInt("student_id"));
checkInRecord.setDormitoryId(rs.getInt("dormitory_id"));
checkInRecord.setCheckInTime(rs.getTimestamp("check_in_time"));
checkInRecords.add(checkInRecord);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return checkInRecords;
}
```
根据学生ID查询学生退住记录:
```java
public List<CheckOutRecord> getCheckOutRecordsByStudentId(int studentId) {
List<CheckOutRecord> checkOutRecords = new ArrayList<>();
String sql = "SELECT * FROM check_out_record WHERE student_id=?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, studentId);
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
CheckOutRecord checkOutRecord = new CheckOutRecord();
checkOutRecord.setStudentId(rs.getInt("student_id"));
checkOutRecord.setDormitoryId(rs.getInt("dormitory_id"));
checkOutRecord.setCheckOutTime(rs.getTimestamp("check_out_time"));
checkOutRecords.add(checkOutRecord);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return checkOutRecords;
}
```
以上就是一个简单的宿舍管理系统的实现,代码示例仅供参考。如果你需要更详细的代码实现,可以参考一些开源的宿舍管理系统,如Dormitory Management System。
阅读全文