1.实体类
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
/**
* 用户信息
*/
@Data
@TableName("cand_user")
public class CandUser implements Serializable {
private static final long serialVersionUID = 7184120552080952178L;
/**
* 用户id
*/
@TableId
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long id;
/**
* 用户名称
*/
private String userName;
/**
* 性别 0女 1男
*/
private int gender;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createdAt;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updatedAt;
/**
* 密码
*/
private String password;
}
2.mapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnyc.entity.user.CandUser;
import org.apache.ibatis.annotations.Mapper;
/**
* 用户信息表(CandUser)数据库访问层
*/
@Mapper
public interface CandUserMapper extends BaseMapper<CandUser> {
IPage<CandUser> page(Page<CandUser> page, QueryWrapper<CandUser> entity);
}
3,service
import com.baomidou.mybatisplus.extension.service.IService;
import com.hnyc.entity.user.CandUser;
/**
* 用户信息表(CandRole)服务接口
*/
public interface CandUserService extends IService<CandUser> {
/**
* 根据用户id删除用户信息
* @param id 用户id
* @return 返回结果
*/
String deleteById(Long id);
}
4.serviceImpl
import com.baomidou.mybatisplus.extension.service.IService;
import com.hnyc.entity.user.CandUser;
/**
* 用户信息表(CandRole)服务接口
*/
public interface CandUserService extends IService<CandUser> {
/**
* 根据用户id删除用户信息
* @param id 用户id
* @return 返回结果
*/
String deleteById(Long id);
}
5.controller
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnyc.entity.user.CandUser;
import com.hnyc.service.user.CandUserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 用户信息(CandUser)表控制层
*/
@RestController
@RequestMapping(value = "/api/user")
public class CandUserController {
/**
* 服务对象
*/
@Resource
private CandUserService candUserService;
/**
* 分页查询所有数据
*
* @param page 分页对象
* @param entity 查询实体
* @return 所有数据
*/
@GetMapping(value = "/page")
@ResponseBody
public IPage<CandUser> selectAll(Page<CandUser> page, CandUser entity) {
return candUserService.page(page, new QueryWrapper<>(entity));
}
/**
* 根据角色id查询角色信息
*
* @param id 角色id
* @return 角色信息
*/
@GetMapping(value = "/findById/{id}")
public CandUser findById(@PathVariable("id") Long id) {
return candUserService.getById(id);
}
/**
* 根据用户id删除用户信息
* @param id 用户id
* @return 返回结果
*/
@DeleteMapping(value = "/deleteById/{id}")
public String deleteById(@PathVariable("id") Long id) {
return candUserService.deleteById(id);
}
/**
* 增加用户信息
* @param entity 用户实体信息
* @return 增加结果 ture成功 false失败
*/
@PostMapping(value = "/add")
public Boolean add(@RequestBody CandUser entity) {
return candUserService.save(entity);
}
/**
* 修改用户信息
* @param entity 用户实体信息
* @return 增加结果 ture成功 false失败
*/
@PutMapping(value = "/edit")
public Boolean edit(@RequestBody CandUser entity) {
return candUserService.updateById(entity);
}
}
7.mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hnyc.mapper.user.CandUserMapper">
</mapper>