springboot整合mybatis_plus进行增删改查
时间: 2023-11-06 17:56:48 浏览: 337
回答:非常好的问题!SpringBoot和MyBatis-Plus的整合可以大大简化我们的开发工作。在增删改查方面,MyBatis-Plus提供了非常方便的CRUD操作,可以让我们轻松地进行数据操作。同时,MyBatis-Plus还提供了很多高级功能,比如分页查询、乐观锁、逻辑删除等等。总的来说,SpringBoot和MyBatis-Plus的整合非常值得我们去学习和使用。
相关问题
vue 整合springboot整合mybatis-plus做增删改查
首先,需要在后端环境中搭建好 Spring Boot 和 MyBatis Plus 的开发环境,可以使用 Maven 或 Gradle 等构建工具来引入所需的依赖。
接下来,需要创建一个数据表对应的 Java 实体类,并使用 MyBatis Plus 提供的注解来标识表名、主键、字段等信息。例如:
```
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
}
```
然后,创建一个 Mapper 接口,继承自 MyBatis Plus 提供的 BaseMapper 接口,用于定义增删改查等基本操作。例如:
```
public interface UserMapper extends BaseMapper<User> {
}
```
在 Spring Boot 的配置文件中,配置数据库连接信息和 MyBatis Plus 相关配置,例如:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis-plus.configuration.map-underscore-to-camel-case=true
```
最后,在 Spring Boot 中创建一个 Controller 类,定义对应的请求处理方法,例如:
```
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/{id}")
public User getUserById(@PathVariable("id") Long id) {
return userMapper.selectById(id);
}
@PostMapping("")
public void createUser(@RequestBody User user) {
userMapper.insert(user);
}
@PutMapping("/{id}")
public void updateUser(@PathVariable("id") Long id, @RequestBody User user) {
user.setId(id);
userMapper.updateById(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable("id") Long id) {
userMapper.deleteById(id);
}
}
```
这样,就完成了 Vue 和 Spring Boot 整合 MyBatis Plus 做增删改查的基本流程。在前端中,可以使用 Axios 等 HTTP 请求库来调用后端的接口。
SpringBoot整合MyBatis Plus实现增删改查的全部代码,包括html的
这里给出一个简单的示例代码,包括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来实现与后端的交互。
这是一个非常简单的示例,如果需要更复杂的功能,可以根据需求进行扩展。
阅读全文