SpringBoot整合MyBatis Plus实现增删改查的全部代码,包括html的
时间: 2023-06-19 20:05:02 浏览: 158
这里给出一个简单的示例代码,包括SpringBoot整合MyBatis Plus实现增删改查的全部代码和一个简单的HTML页面。
前置条件:
1. 已经安装好了SpringBoot和MyBatis Plus。
2. 已经配置好了数据库连接。
1. 创建一个实体类
```java
@Data
@EqualsAndHashCode(callSuper = false)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
```
2. 创建Mapper接口
```java
public interface UserMapper extends BaseMapper<User> {
}
```
3. 创建Service接口和实现类
```java
public interface UserService {
List<User> list();
User getById(Long id);
boolean save(User user);
boolean updateById(User user);
boolean removeById(Long id);
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public List<User> list() {
return baseMapper.selectList(null);
}
@Override
public User getById(Long id) {
return baseMapper.selectById(id);
}
@Override
public boolean save(User user) {
return baseMapper.insert(user) > 0;
}
@Override
public boolean updateById(User user) {
return baseMapper.updateById(user) > 0;
}
@Override
public boolean removeById(Long id) {
return baseMapper.deleteById(id) > 0;
}
}
```
4. 创建Controller
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public List<User> list() {
return userService.list();
}
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
return userService.getById(id);
}
@PostMapping("/")
public String save(@RequestBody User user) {
return userService.save(user) ? "success" : "error";
}
@PutMapping("/")
public String updateById(@RequestBody User user) {
return userService.updateById(user) ? "success" : "error";
}
@DeleteMapping("/{id}")
public String removeById(@PathVariable Long id) {
return userService.removeById(id) ? "success" : "error";
}
}
```
5. 创建HTML页面
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Management</title>
</head>
<body>
<h1>User Management</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Email</th>
<th>Actions</th>
</tr>
<tbody id="user-list"></tbody>
</table>
<h2>Add User</h2>
<form id="add-form" onsubmit="return addUser()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<button type="submit">Add</button>
</form>
<script>
function loadUsers() {
var userList = document.getElementById("user-list");
fetch("/user/list")
.then(response => response.json())
.then(users => {
userList.innerHTML = "";
users.forEach(user => {
var tr = document.createElement("tr");
var idTd = document.createElement("td");
idTd.innerText = user.id;
tr.appendChild(idTd);
var nameTd = document.createElement("td");
nameTd.innerText = user.name;
tr.appendChild(nameTd);
var ageTd = document.createElement("td");
ageTd.innerText = user.age;
tr.appendChild(ageTd);
var emailTd = document.createElement("td");
emailTd.innerText = user.email;
tr.appendChild(emailTd);
var actionsTd = document.createElement("td");
var editButton = document.createElement("button");
editButton.innerText = "Edit";
editButton.onclick = function() { editUser(user) };
actionsTd.appendChild(editButton);
var deleteButton = document.createElement("button");
deleteButton.innerText = "Delete";
deleteButton.onclick = function() { deleteUser(user) };
actionsTd.appendChild(deleteButton);
tr.appendChild(actionsTd);
userList.appendChild(tr);
});
});
}
function addUser() {
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
var email = document.getElementById("email").value;
var user = { name: name, age: age, email: email };
fetch("/user/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(user)
})
.then(response => response.text())
.then(result => {
if (result == "success") {
loadUsers();
document.getElementById("add-form").reset();
} else {
alert("Failed to add user.");
}
});
return false;
}
function editUser(user) {
var name = prompt("Name:", user.name);
if (name == null) {
return;
}
var age = prompt("Age:", user.age);
if (age == null) {
return;
}
var email = prompt("Email:", user.email);
if (email == null) {
return;
}
user.name = name;
user.age = age;
user.email = email;
fetch("/user/", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(user)
})
.then(response => response.text())
.then(result => {
if (result == "success") {
loadUsers();
} else {
alert("Failed to update user.");
}
});
}
function deleteUser(user) {
if (confirm("Are you sure to delete user " + user.name + "?")) {
fetch("/user/" + user.id, {
method: "DELETE"
})
.then(response => response.text())
.then(result => {
if (result == "success") {
loadUsers();
} else {
alert("Failed to delete user.");
}
});
}
}
loadUsers();
</script>
</body>
</html>
```
这个HTML页面展示了所有用户的列表,并且可以添加、编辑和删除用户。它通过JavaScript调用RESTful API来实现与后端的交互。
这是一个非常简单的示例,如果需要更复杂的功能,可以根据需求进行扩展。
阅读全文