本文写给自己看,但是有需要的小伙伴,可以创建springboot项目,从头创建项目,全部代码贴上了。这是加分页的增删改查,在上一个增删改查项目加了分页功能,下一个功能接会增加图片的上传,显示图片的功能
项目架构
构建springboot项目需要的依赖
分页插件依赖
<!-- pagehelper分页插件依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
bean
@Data
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
mapper
@Mapper
public interface UserMapper {
//查询所有User
@Select("select * from user")
public List<User> findAll();
@Select("select * from user where id = #{id}")
//查询一个User
public User findUserById(Integer id);
@Update("update user set username = '${username}',birthday = #{birthday},sex = #{sex},address = #{address} where id = #{id}")
//修改User
public int updateUser(User user);
@Insert({
"insert into user(username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})"})
//添加一个User
public int addUser(User user);
//删除一个User
@Delete("delete from user where id = #{id}")
public int deleteUser(Integer id);
}
controller
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
/**
* 查询所有user
* @return
*/
@GetMapping("/user")
public PageInfo<User> findAll(@RequestParam(defaultValue = "1",value = "pn")Integer pageNum){
PageHelper.startPage(pageNum, 5);
List<User> allUser = userMapper.findAll();
PageInfo<User> pageInfo = new PageInfo<>(allUser);
return pageInfo ;
}
/**
* 根据id查询一个user
* @param id
* @return
*/
@GetMapping("/user/{id}")
public User findUserById(@PathVariable("id") Integer id){
return userMapper.findUserById(id);
}
/**
* 修改user
* @param id
* @param username
* @param birthday
* @param sex
* @param address
* @return
* @throws ParseException
*/
@PutMapping("/user")
public String update(@RequestParam("id") String id,
@RequestParam("username") String username,
@RequestParam("birthday") String birthday,
@RequestParam("sex") String sex,
@RequestParam("address") String address) throws ParseException {
User user = new User();
user.setId(Integer.parseInt(id));
user.setUsername(username);
//对日期进行格式转换
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(birthday);
user.setBirthday(date);
user.setSex(sex);
user.setAddress(address);
return userMapper.updateUser(user) == 1 ? "success" : "failed";
}
/**
* 新增一个user
* @param username
* @param birthday
* @param sex
* @param address
* @return
*/
@PostMapping("/user")
public String insertUser(@RequestParam("username") String username,
@RequestParam("birthday") String birthday,
@RequestParam("sex") String sex,
@RequestParam("address") String address) throws ParseException {
User user = new User();
user.setUsername(username);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(birthday);
user.setBirthday(date);
user.setSex(sex);
user.setAddress(address);
return userMapper.addUser(user) == 1 ? "success":"failed";
}
/**
* 删除一个user
* @param id
* @return
*/
@DeleteMapping("/user/{id}")
public String deleteUser(@PathVariable("id") Integer id){
return userMapper.deleteUser(id) == 1 ? "success" : "failed";
}
}
application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/springdb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
username: root
password: root
driver-class-namecom: com.mysql.cj.jdbc.Driver
##pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="bt/css/bootstrap.css">
<link rel="stylesheet" href="bt/css/bootstrap-theme.css">
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>