007 springboot整合mybatis-plus 增删改查 ajax(修改部分是) jquery 分页
整合Spring Boot与MyBatis-Plus进行增删改查操作,并使用Ajax和jQuery进行前后端分离,同时加入分页功能的示例代码如下:
1. 引入MyBatis-Plus依赖
在pom.xml
中添加MyBatis-Plus的依赖:
<dependencies>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.x.x</version>
</dependency>
<!-- jQuery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.x.x</version>
</dependency>
</dependencies>
2. 配置MyBatis-Plus
在application.properties
或application.yml
中配置数据库信息:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3. 创建实体类和Mapper
// 实体类 User.java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
// Mapper 接口 UserMapper.java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
4. 控制器Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
// 分页查询
@GetMapping("/list")
public IPage<User> getUserPage(Page<User> page) {
return userMapper.selectPage(page);
}
// 新增用户
@PostMapping("/add")
public boolean addUser(User user) {
return userMapper.insert(user) > 0;
}
// 删除用户
@DeleteMapping("/delete/{id}")
public boolean deleteUser(@PathVariable Long id) {
return userMapper.deleteById(id);
}
// 更新用户
@PutMapping("/update")
public boolean updateUser(User user) {
return userMapper.updateById(user);
}
}
5. 前端页面
<!DOCTYPE html>
<html>
<head>
<title>User Management</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/jquery.
评论已关闭