熟悉表结构
![image]()
熟悉项目配置
![image]()
熟悉数据返回值
1.分页数据:TableDataInfo
总记录数total,列表数据rows,消息状态码code,消息内容msg
public class TableDataInfo implements Serializable
{
private static final long serialVersionUID = 1L;
/** 总记录数 */
private long total;
/** 列表数据 */
private List<?> rows;
/** 消息状态码 */
private int code;
/** 消息内容 */
private String msg;
/**
* 表格数据对象
*/
public TableDataInfo()
{
}
/**
* 分页
*
* @param list 列表数据
* @param total 总记录数
*/
public TableDataInfo(List<?> list, int total)
{
this.rows = list;
this.total = total;
}
}
2.正常数据:AjaxResult
data(数据) msg(消息) code(状态码)以后要是需要用成功还是失败直接调用ajax的success和error就行
public class AjaxResult extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
}
3.导出/下载:void --> 相当于我们不使用 SpringMVC 做响应了。需要一个形参 HttpServlerResponse
SpringMvc是对Servlet的二次封装.
他为什么要部署到tomcat服务器中?
tomcat会创建servlet和jsp
jsp:用于给前端用户展示数据
servlet:他是运行在服务器当中的一个类
他有两个形态一个是httpservletRequst(请求)一个数httpServletresponse(响应)
他有两个形态一个是httpservletRequst(请求)一个数httpServletresponse(响应)--可以接收浏览器的请求给浏览器做响应
MyBatis是对JDBC二次封装 ----- 他是封装了jdbc代码操作的数据库
关于servlet和tomcat的回顾
1.servlet必须部署到Tomcat中才能运行
2.Tomcat是Servlet的运行容器
Java Web 最经典、最基础、最标准的架构?
MVC 模式 + 三层架构(Servlet+JSP+Service+Dao)
MVC是啥?
M(Model):JavaBean / 实体类 + Service + Dao
V(View):JSP 页面
C(Controller):Servlet
如何在Tomcat中配置Servlet
方法一:注解方式
打开Servlet类在类上面加@WebServlet("/访问路径")
方法二:web.xml方式
WEB-INF/web.xml 里配置。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="4.0">
<!-- 1. 给 Servlet 起名字 + 指定类 -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<!-- 2. 绑定访问路径 -->
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
查询分析
响应结果做了分页需要接收参数
接参数可能在形参列表中接(MVC中)
如果不在MVC中呢可能用request请求
首先看com.ruoyi.common.core.controller.BaseController
/**
* 岗位控制器
*/
@Controller
@RequestMapping("system/post")
public class PostController {
/**
* 获取岗位列表
*/
@PreAuthorize("@ss.hasPermi('system:post:list')")
@GetMapping("/list")
public TableDataInfo list(SysPost post)
{
startPage();
List<SysPost> list = postService.selectPostList(post);
return getDataTable(list);
}
}
点进去startPage()进入到PageUtils中
public class PageUtils extends PageHelper
{
/**
* 设置请求分页数据
*/
public static void startPage()
{
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
Boolean reasonable = pageDomain.getReasonable();
PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
}
/**
* 清理分页的线程变量
*/
public static void clearPage()
{
PageHelper.clearPage();
}
}
上面是接收参数的过程
![image]()
就找到了pageNum和pageSize
在项目中查找浏览器中的地址
![image]()
然后我要找java文件
接下来下一句是高级查询
List<SysPost> list = postService.selectPostList(post);
一直点点到xml里
<sql id="selectPostVo">
select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark
from sys_post
</sql>
<select id="selectPostList" parameterType="SysPost" resultMap="SysPostResult">
<include refid="selectPostVo"/>
<where>
<if test="postCode != null and postCode != ''">
AND post_code like concat('%', #{postCode}, '%')
</if>
<if test="status != null and status != ''">
AND status = #{status}
</if>
<if test="postName != null and postName != ''">
AND post_name like concat('%', #{postName}, '%')
</if>
</where>
</select>
新增和编辑分析
新增
/**
* 新增岗位
*/
@PreAuthorize("@ss.hasPermi('system:post:add')")
@Log(title = "岗位管理", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody SysPost post)
{
if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
{
return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败,岗位名称已存在");
}
else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
{
return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败,岗位编码已存在");
}
post.setCreateBy(getUsername());
return toAjax(postService.insertPost(post));
}
@Validated:参数验证
前端是以json字符串传的 后端要用@requestBody接收
UserConstants封装了NOT_UNIQUE=1不是 UNIQUE=0是
下面这个代码服务新增和编辑 他俩共用滴
@Override
public String checkPostNameUnique(SysPost post)
{
//服务于新增和编辑
//新增id==null-->数据库一定没有岗位重名数据
//编辑id!=null-->数据库中可能有一个重名数据,有用户可能没有修改岗位名称
//postId=(post.getId()==null?-1L:post.post.getId())
Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
//info=根据岗位名称查岗位对象
SysPost info = postMapper.checkPostNameUnique(post.getPostName());
//不等于空或者我修改的和本来只能躺着的id不一样
if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
{
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
新增id==null-->数据库一定没有岗位重名数据
编辑id!=null-->数据库中可能有一个重名数据,有用户可能没有修改岗位名称
前端传过来的postId,若为空则赋值为-1,若不为空则获取当前值。
回顾
新增默认返回值为受影响行数
动态sql中的多态set是啥作用:去掉多余逗号
编辑比添加多发了一条根据id把数据查出来SysPostController里
/**
* 根据岗位编号获取详细信息
*/
@PreAuthorize("@ss.hasPermi('system:post:query')")
@GetMapping(value = "/{postId}")
public AjaxResult getInfo(@PathVariable Long postId)
{
return AjaxResult.success(postService.selectPostById(postId));
}
删除
/**
* 删除岗位
*/
@PreAuthorize("@ss.hasPermi('system:post:remove')")
@Log(title = "岗位管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{postIds}")
public AjaxResult remove(@PathVariable Long[] postIds)
{
return toAjax(postService.deletePostByIds(postIds));
}
点击service后的方法
/**
* 批量删除岗位信息
*
* @param postIds 需要删除的岗位ID
* @return 结果
*/
@Override
public int deletePostByIds(Long[] postIds)
{
for (Long postId : postIds)
{
SysPost post = selectPostById(postId);
if (countUserPostById(postId) > 0)
{
throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
}
}
return postMapper.deletePostByIds(postIds);
}
如果岗位里有人则不能删除这个岗位,没人就能删.