用Spring Boot+Vue做微人事項目第十一天
前兩天做了微人事登錄的前端頁面和後端接口,第三天則實現了前後端接口的對接,輸入正確的用戶名和密碼之後,成功的跳轉到home頁。第四天做了Home頁的Title製作和下拉菜單,下拉菜單有三個選項,個人中心、設置和註銷登錄,還做了註銷登錄,點擊註銷登錄會出現提示:“此操作將註銷登錄,是否繼續”,點是就重新跳轉到登錄頁面,第五天做的是左邊的導航菜單,第六天是做的服務端菜單接口的設計,第七天是Vuex的介紹、安裝和配置、第八天是不寫代碼,第九天談一談前後端分離開發,權限管理的一些思路,是後端接口權限設計,第十天寫業務代碼,從系統管理的基礎信息設置開始寫,先寫前端頁面,今天開始寫系統管理的基礎信息設置的後端接口了
①:把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和前面那個是一樣的,測試的添加效果如下圖所示:
測試的修改如下圖所示:
測試的刪除如下圖所示:
至此: 系統管理的基礎信息設置的後端接口已寫完