package com.ruoyi.project.system.controller; import java.util.List; import…
- User
package com.ruoyi.project.system.controller;
import java.util.List; import javax.servlet.http.HttpServletResponse; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.project.system.domain.SysDept; import com.ruoyi.project.system.service.ISysDeptService; import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.web.page.TableDataInfo;
/
- 部门Controller
- @author ruoyi
- @date 2023-06-08 */ @RestController @RequestMapping(“/system/dept”) public class SysDeptController extends BaseController { @Autowired private ISysDeptService sysDeptService;
/
- 查询部门列表 */ @PreAuthorize(“@ss.hasPermi(‘system:dept:list’)”) @GetMapping(“/list”) public TableDataInfo list(SysDept sysDept) { startPage(); List list = sysDeptService.selectSysDeptList(sysDept); return getDataTable(list); }
/
- 导出部门列表 */ @PreAuthorize(“@ss.hasPermi(‘system:dept:export’)”) @Log(title = “部门”, businessType = BusinessType.EXPORT) @PostMapping(“/export”) public void export(HttpServletResponse response, SysDept sysDept) { List list = sysDeptService.selectSysDeptList(sysDept); ExcelUtil util = new ExcelUtil(SysDept.class); util.exportExcel(response, list, “部门数据”); }
/
- 获取部门详细信息 */ @PreAuthorize(“@ss.hasPermi(‘system:dept:query’)”) @GetMapping(value = “/{deptId}”) public AjaxResult getInfo(@PathVariable(“deptId”) Long deptId) { return success(sysDeptService.selectSysDeptByDeptId(deptId)); }
/
- 新增部门 */ @PreAuthorize(“@ss.hasPermi(‘system:dept:add’)”) @Log(title = “部门”, businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody SysDept sysDept) { return toAjax(sysDeptService.insertSysDept(sysDept)); }
/
- 修改部门 */ @PreAuthorize(“@ss.hasPermi(‘system:dept:edit’)”) @Log(title = “部门”, businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody SysDept sysDept) { return toAjax(sysDeptService.updateSysDept(sysDept)); }
/**
- 删除部门 */ @PreAuthorize(“@ss.hasPermi(‘system:dept:remove’)”) @Log(title = “部门”, businessType = BusinessType.DELETE) @DeleteMapping(“/{deptIds}”) public AjaxResult remove(@PathVariable Long[] deptIds) { return toAjax(sysDeptService.deleteSysDeptByDeptIds(deptIds)); } } 解释上述代码
- Assistant
该代码实现了部门信息的增删改查功能,使用Restful风格的API。其中,使用了Spring Security的注解来实现对部门信息的权限控制。具体实现如下:
-
@PreAuthorize注解:对方法进行权限控制,只有具有相应角色或权限的用户才能执行该方法。
-
@Log注解:记录方法的操作日志,包括操作类型等信息。
-
@Autowired注解:自动注入ISysDeptService接口的实现类,从而使用其提供的方法。
-
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping注解:分别对应HTTP协议中的GET、POST、PUT、DELETE请求。
-
@PathVariable注解:获取请求路径中的参数值,用于数据的查询和删除操作。
-
TableDataInfo类:封装了分页查询的返回结果。
-
AjaxResult类:封装了响应结果,包括响应码、响应信息等。
总之,该代码实现了基本的CRUD功能,并且通过Spring Security实现了对部门信息的权限控制,可以提高系统的安全性。
-
- User
解释下面代码 package com.ruoyi.project.system.mapper;
import java.util.List; import com.ruoyi.project.system.domain.SysDept;
/**
-
部门Mapper接口
-
@author ruoyi
-
@date 2023-06-08 / public interface SysDeptMapper { /*
- 查询部门
- @param deptId 部门主键
- @return 部门 */ public SysDept selectSysDeptByDeptId(Long deptId);
/**
- 查询部门列表
- @param sysDept 部门
- @return 部门集合 */ public List selectSysDeptList(SysDept sysDept);
/**
- 新增部门
- @param sysDept 部门
- @return 结果 */ public int insertSysDept(SysDept sysDept);
/**
- 修改部门
- @param sysDept 部门
- @return 结果 */ public int updateSysDept(SysDept sysDept);
/**
- 删除部门
- @param deptId 部门主键
- @return 结果 */ public int deleteSysDeptByDeptId(Long deptId);
/**
- 批量删除部门
- @param deptIds 需要删除的数据主键集合
- @return 结果 */ public int deleteSysDeptByDeptIds(Long[] deptIds); }
-
- Assistant
该代码为部门Mapper接口的定义,主要包括查询、新增、修改、删除等方法。其中,使用了Mybatis的注解方式来实现数据库操作。具体解释如下:
-
@Select注解:标识该方法为查询操作,其中,使用了SQL语句获取数据。返回值为SysDept类型,参数为部门的主键deptId。
-
@Insert注解:标识该方法为插入操作,其中,使用了SQL语句将数据插入到数据库中。返回值为int类型,表示插入操作的结果。参数为SysDept类型的部门对象。
-
@Update注解:标识该方法为更新操作,其中,使用了SQL语句更新数据库中的数据。返回值为int类型,表示更新操作的结果。参数为SysDept类型的部门对象。
-
@Delete注解:标识该方法为删除操作,其中,使用了SQL语句删除数据库中的数据。返回值为int类型,表示删除操作的结果。参数为部门的主键deptId。
-
部门Mapper接口中还定义了一个查询部门列表的方法selectSysDeptList和一个批量删除部门的方法deleteSysDeptByDeptIds。其中,selectSysDeptList方法的参数为SysDept类型的部门对象,返回值为SysDept类型的部门集合;deleteSysDeptByDeptIds方法的参数为Long类型的部门主键deptIds数组,返回值为int类型,表示删除操作的结果。
总之,该代码为部门Mapper接口的定义,提供了基本的数据库操作方法,通过Mybatis的注解方式实现了增删改查的功能。
-
- User
<resultMap type="SysDept" id="SysDeptResult"> <result property="deptId" column="dept_id" /> <result property="parentId" column="parent_id" /> <result property="ancestors" column="ancestors" /> <result property="deptName" column="dept_name" /> <result property="orderNum" column="order_num" /> <result property="leader" column="leader" /> <result property="phone" column="phone" /> <result property="email" column="email" /> <result property="status" column="status" /> <result property="delFlag" column="del_flag" /> <result property="createBy" column="create_by" /> <result property="createTime" column="create_time" /> <result property="updateBy" column="update_by" /> <result property="updateTime" column="update_time" /> </resultMap> <sql id="selectSysDeptVo"> select dept_id, parent_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag, create_by, create_time, update_by, update_time from sys_dept </sql> <select id="selectSysDeptList" parameterType="SysDept" resultMap="SysDeptResult"> <include refid="selectSysDeptVo"/> <where> <if test="parentId != null "> and parent_id = #{parentId}</if> <if test="ancestors != null and ancestors != ''"> and ancestors = #{ancestors}</if> <if test="deptName != null and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if> <if test="orderNum != null "> and order_num = #{orderNum}</if> <if test="leader != null and leader != ''"> and leader = #{leader}</if> <if test="phone != null and phone != ''"> and phone = #{phone}</if> <if test="email != null and email != ''"> and email = #{email}</if> <if test="status != null and status != ''"> and status = #{status}</if> </where> </select> <select id="selectSysDeptByDeptId" parameterType="Long" resultMap="SysDeptResult"> <include refid="selectSysDeptVo"/> where dept_id = #{deptId} </select> <insert id="insertSysDept" parameterType="SysDept" useGeneratedKeys="true" keyProperty="deptId"> insert into sys_dept <trim prefix="(" suffix=")" suffixOverrides=","> <if test="parentId != null">parent_id,</if> <if test="ancestors != null">ancestors,</if> <if test="deptName != null">dept_name,</if> <if test="orderNum != null">order_num,</if> <if test="leader != null">leader,</if> <if test="phone != null">phone,</if> <if test="email != null">email,</if> <if test="status != null">status,</if> <if test="delFlag != null">del_flag,</if> <if test="createBy != null">create_by,</if> <if test="createTime != null">create_time,</if> <if test="updateBy != null">update_by,</if> <if test="updateTime != null">update_time,</if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="parentId != null">#{parentId},</if> <if test="ancestors != null">#{ancestors},</if> <if test="deptName != null">#{deptName},</if> <if test="orderNum != null">#{orderNum},</if> <if test="leader != null">#{leader},</if> <if test="phone != null">#{phone},</if> <if test="email != null">#{email},</if> <if test="status != null">#{status},</if> <if test="delFlag != null">#{delFlag},</if> <if test="createBy != null">#{createBy},</if> <if test="createTime != null">#{createTime},</if> <if test="updateBy != null">#{updateBy},</if> <if test="updateTime != null">#{updateTime},</if> </trim> </insert> <update id="updateSysDept" parameterType="SysDept"> update sys_dept <trim prefix="SET" suffixOverrides=","> <if test="parentId != null">parent_id = #{parentId},</if> <if test="ancestors != null">ancestors = #{ancestors},</if> <if test="deptName != null">dept_name = #{deptName},</if> <if test="orderNum != null">order_num = #{orderNum},</if> <if test="leader != null">leader = #{leader},</if> <if test="phone != null">phone = #{phone},</if> <if test="email != null">email = #{email},</if> <if test="status != null">status = #{status},</if> <if test="delFlag != null">del_flag = #{delFlag},</if> <if test="createBy != null">create_by = #{createBy},</if> <if test="createTime != null">create_time = #{createTime},</if> <if test="updateBy != null">update_by = #{updateBy},</if> <if test="updateTime != null">update_time = #{updateTime},</if> </trim> where dept_id = #{deptId} </update> <delete id="deleteSysDeptByDeptId" parameterType="Long"> delete from sys_dept where dept_id = #{deptId} </delete> <delete id="deleteSysDeptByDeptIds" parameterType="String"> delete from sys_dept where dept_id in <foreach item="deptId" collection="array" open="(" separator="," close=")"> #{deptId} </foreach> </delete> - Assistant
该代码为Mybatis的映射文件,用于将数据库中的数据映射到Java程序的实体类中。具体解释如下:
-
mapper标签:根节点,定义了该XML文件对应的Java接口。
-
resultMap标签:定义了结果集的映射关系,将查询出来的数据库表中的字段与Java实体类中的属性进行映射。
-
sql标签:定义了一个查询SQL语句的模板。
-
select标签:定义了查询操作的SQL语句,以及查询参数的映射关系。
-
insert标签:定义了插入操作的SQL语句,以及插入参数的映射关系。
-
update标签:定义了更新操作的SQL语句,以及更新参数的映射关系。
-
delete标签:定义了删除操作的SQL语句,以及删除参数的映射关系。
通过这些标签的定义,Mybatis可以将查询操作返回的结果,自动映射到Java实体类中,并且在插入、更新、删除等操作时,也可以将Java实体类中的属性自动映射到对应的数据库表字段中。从而实现了对象关系映射(ORM)的功能。
总之,该代码为Mybatis映射文件的定义,用于将数据库表中的数据与Java实体类进行映射,可以方便地完成数据库的操作。
-
- User
mapper.java与mapper.xml的关系
- Assistant
mapper.java与mapper.xml是Mybatis框架中的两个重要概念,它们之间有着紧密的关系。
mapper.java是定义Java接口,用于定义数据库操作的方法。mapper.java通过注解或XML方式来描述与数据库表的关系。在定义mapper.java时,需要使用@Mapper或@MapperScan注解来声明Mapper接口。
mapper.xml则是用于定义SQL语句和映射关系的XML文件。它包含了数据库表的字段与Java实体类属性之间的映射关系,以及CRUD操作所需的SQL语句。在定义mapper.xml时,需要使用mapper标签来声明该XML文件对应的Java接口,并且定义各种SQL语句和映射关系。
由于Java接口和XML文件之间存在映射关系,因此需要通过一个桥梁来实现Java代码与SQL语句之间的映射。这个桥梁就是Mybatis的SqlSession,它是一个面向对象的数据访问接口,用于执行mapper.xml定义的SQL语句。SqlSession会将SQL语句和Java方法进行匹配,并将查询结果自动映射为Java对象,以便于Java代码的使用。
在实际开发中,我们通常会将mapper.java和mapper.xml放在同一个包中,使它们之间更加紧密地联系在一起。当需要实现与数据库的数据交互时,我们就可以通过mapper