16.职位管理后端接口设计
- 把Position实体类里面的createdate属性的date改成大写的Date并修改该属性的getter和setter方法
public class Position implements Serializable {
private Integer id;
/**
* 职位
*/
private String name;
private Date createDate;
private Boolean enabled;
-
把PositionMapper.xml里面和createdate相关的全部修改成createDate
-
在controller包里面新建system包,再在system包里面新建basic包,再在basic包里面创建PositionController类,在定义PositionController类的接口的时候,一定要与数据库的menu中的url地址到一致,不然会出现没有权限访问的问题
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {
@Autowired
PositionService positionService;
@GetMapping("/")
public List<Position> getAllPositions(){
return positionService.getAllPositions();
}
}
PositionService类
@Service
public class PositionService {
@Autowired
PositionMapper positionMapper;
public List<Position> getAllPositions() {
return positionMapper.getAllPositions();
}
}
PositionMapper接口
@Repository
public interface PositionMapper {
int deleteByPrimaryKey(Integer id);
int insert(Position record);
int insertSelective(Position record);
Position selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Position record);
int updateByPrimaryKey(Position record);
List<Position> getAllPositions();
}
PositionMapper.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.lqg.vhr.mapper.PositionMapper">
<resultMap id="BaseResultMap" type="com.lqg.vhr.model.Position">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="createDate" jdbcType="TIMESTAMP" property="createDate" />
<result column="enabled" jdbcType="BIT" property="enabled" />
</resultMap>
<sql id="Base_Column_List">
id, `name`, createDate, enabled
</sql>
<select id="getAllPositions" resultMap="BaseResultMap">
select * from position;
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from position
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from position
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.lqg.vhr.model.Position">
insert into position (id, `name`, createDate,
enabled)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP},
#{enabled,jdbcType=BIT})
</insert>
<insert id="insertSelective" parameterType="com.lqg.vhr.model.Position">
insert into position
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
`name`,
</if>
<if test="createDate != null">
createDate,
</if>
<if test="enabled != null">
enabled,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="createDate != null">
#{createDate,jdbcType=TIMESTAMP},
</if>
<if test="enabled != null">
#{enabled,jdbcType=BIT},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lqg.vhr.model.Position">
update position
<set>
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="createDate != null">
createDate = #{createDate,jdbcType=TIMESTAMP},
</if>
<if test="enabled != null">
enabled = #{enabled,jdbcType=BIT},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.lqg.vhr.model.Position">
update position
set `name` = #{name,jdbcType=VARCHAR},
createDate = #{createDate,jdbcType=TIMESTAMP},
enabled = #{enabled,jdbcType=BIT}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
打开Postman测试查询所有的position,效果如下图:
再把position的增删改三个接口也给写一下
PositionController
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {
@Autowired
PositionService positionService;
@GetMapping("/")
public List<Position> getAllPositions(){
return positionService.getAllPositions();
}
@PostMapping("/")
public RespBean addPosition(@RequestBody Position position){
if (positionService.addPosition(position)==1){
return RespBean.ok("添加成功!");
}
return RespBean.error("添加失败!");
}
@PutMapping("/")
public RespBean updatePositions(@RequestBody Position position){
if (positionService.updatePositions(position)==1){
return RespBean.ok("修改成功!");
}
return RespBean.error("修改失败!");
}
@DeleteMapping("/{id}")
public RespBean deletePositionById(@PathVariable Integer id){
if(positionService.deletePositionById(id)==1){
return RespBean.ok("删除成功!");
}
return RespBean.error("删除失败");
}
}
PositionService
@Service
public class PositionService {
@Autowired
PositionMapper positionMapper;
public List<Position> getAllPositions() {
return positionMapper.getAllPositions();
}
public Integer addPosition(Position position) {
position.setEnabled(true);
position.setCreateDate(new Date());
return positionMapper.insertSelective(position);
}
public Integer updatePositions(Position position) {
return positionMapper.updateByPrimaryKeySelective(position);
}
public Integer deletePositionById(Integer id) {
return positionMapper.deleteByPrimaryKey(id);
}
}
PositionMapper接口和PositionMapper.xml和前面那个是一样的,测试的添加效果如下图所示: