目录
3.6用户对应的权限动态展示功能 (比如普通用户对应的就是普通角色 ,超级用户对应的就是超级角色可以做任何事)
1.广告模块
之前我肝完了课程模块,现在肝的部分是广告模块广告模块里的内容和课程模块差不多,都是一些搬砖的活,复杂点在sql语句和表的关系
1.1广告表与广告位的表关系
广告表和广告表的关系显而易见,从广告表的角度就是多对一,广告位的角度就是一对多,
1.2 广告位展示&修改&添加
广告位的展示也特别简单,就是一张表的单表操作
<!--查看所有广告位-->
<select id="findAllPromotionSpace" resultType="com.lagou.domain.PromotionSpace">
select * from promotion_space
</select>
<!-- 添加广告位-->
<insert id="savePromotionSpace" parameterType="com.lagou.domain.PromotionSpace">
insert into promotion_space
values(null,#{name},#{spaceKey},#{createTime},#{updateTime},#{isDel})
</insert>
<!--修改广告位-->
<update id="updatePromotionSpace" parameterType="com.lagou.domain.PromotionSpace">
update promotion_space set
name = #{name},updateTime = #{updateTime} where id = #{id}
</update>
<!--回显广告位-->
<select id="findPromotionSpaceById" parameterType="int" resultType="com.lagou.domain.PromotionSpace">
select id,name from promotion_space where id=#{id}
</select>
查看所有广告位就是一张单表操作,添加广告位需要在web层来判断传递过来的请求是否携带id如果没有就是添加,如果有就是修改,回显广告位一样很简单,就是根据传递过来的id来指定返回的广告位
web层代码
/*添加 广告位方法
* */
@RequestMapping("/saveOrUpdatePromotionSpace")
public ResponseResult saveOrUpdatePromotionSpace(@RequestBody PromotionSpace promotionSpace){
//这里是判断前端传递过来的请求是否是有id的如果有就是修改呗,没有就是添加
if (promotionSpace.getId()==null) {
//调用方法完成添加
promotionSpaceService.savePromotionSpace(promotionSpace);
//给出响应
ResponseResult responseResult=new ResponseResult(true,200,"添加广告ov",null);
return responseResult;
}else {
//调用方法完成修改
promotionSpaceService.updatePromotionSpace(promotionSpace);
//给出响应
ResponseResult responseResult=new ResponseResult(true,200,"修改广告ov",null);
return responseResult;
}
2.广告页分页查询与修改添加广告
这个分页查询也是挺简单的,首先需要在实体类里创建一个响应类
这里就是接收前台传递过来的页数
service层的分页方法
这里就不用代码段了,因为代码段影响我观看,这里这个分页插件用的什么原理我也不知道,就是个用,pageinfo就是封装已经分页好的数据进行返回
添加广告
添加广告这里在选择广告位置的时候就是选择广告位,这时候就是广告位主键id给到广告条,广告条根据主键id对应上具体的广告位置(首页广告,侧边广告等)
我们先来dao层
添加广告位dao层代码 这里把对应的信息都填上,广告位id,上传时间等
<!--广告的新增操作-->
<insert id="savePromotionAd" parameterType="com.lagou.domain.PromotionAd">
insert into promotion_ad VALUES(NULL,#{name},#{spaceId},#{keyword},
#{htmlContent},#{text},#{link},#{startTime},#{endTime},#{createTime},
#{updateTime},#{status},#{priority},#{img});
</insert>
Service层代码
@Override
public void savePromotoionAd(PromotionAd promotionAd) {
//补全信息
Date date=new Date();
promotionAd.setCreateTime(date);
promotionAd.setUpdateTime(date);
//完成添加
promotionAdMapper.savePromotionAd(promotionAd);
web层就不写了都是些简单的单表操作
修改广告条dao层 //(业务层和表现层代码懒得记了,具体实现一模一样)
<update id="updatePromotionAd" parameterType="com.lagou.domain.PromotionAd">
update promotion_ad
<trim prefix="SET" suffixOverrides=",">
<if test="name != null and name != ''">
name = #{name},</if>
<if test="spaceId != null and spaceId != ''">
spaceId = #{spaceId},</if>
<if test="link != null">
link=#{link},</if>
<if test="status != null and status != '' or status == 0">
status=#{status},</if>
<if test="img != null">
img=#{img},</if>
<if test="text != null">
text=#{text},</if>
<if test="startTime != null">
startTime=#{startTime},</if>
<if test="endTime != null">
endTime=#{endTime},</if>
<if test="updateTime != null">
updateTime=#{updateTime},</if>
</trim>
<where>
<if test="id != null and id != '' ">id = #{id}</if>
</where>
</update>
3.用户模块
2.1 查看所有用户
需求分析:这里需要根据用户传入过来的信息来一个动态查询,所以一定用到动态sql语句,还需要一个分页插件来查询
dao层代码,这里就是一个单表查询,很简单
<!--多条件查询-->
<select id="findAllUser" resultType="com.lagou.domain.User">
SELECT * FROM USER
<where>
<if test="true">
and is_del != 1</if>
<if test="username != null">
and name = #{username}</if>
<if test="startCreateTime != null and endCreateTime != null">
and create_time BETWEEN #{startCreateTime} AND #{endCreateTime}
</if>
</where>
3.用户权限
3.1权限关系的表关系
用户和角色 多对多 角色和菜单 多对多 角色和资源 多对多 用户对应角色,角色对应资源和菜单,用户和菜单资源没有直接的接触,是根据用户有的角色来间接对接到菜单表和资源表
3.2给角色分配菜单回显功能
要根据顶级菜单来显示出来子菜单,因为这个菜单是内连接查询
内连接查询,先查询到父菜单。父菜单查询就是parent_id=-1的就是顶级菜单,然后顶级菜单传递过来他们的id,子菜单的prent_id就是对应父菜单得id值的
dao层代码 这里resultMap第一次接收到的parent_id=id是我们给的-1,这里查询到的就是子菜单,然后再次执行这个方法传递一个父菜单的id给到子菜单prent_id这样就查询出来了父子菜单所有的信息回显上去
<resultMap id="menuResult" type="com.lagou.domain.Menu">
<id column="id" property="id"></id>
<result column="href" property="href"></result>
<result column="icon" property="icon"></result>
<result column="name" property="name"></result>
<result column="parent_id" property="parentId"></result>
<result column="description" property="description"></result>
<result column="order_num" property="orderNum"></result>
<result column="shown" property="shown"></result>
<result column="created_time" property="createdTime"></result>
<result column="updated_time" property="updatedTime"></result>
<result column="created_by" property="createdBy"></result>
<result column="updated_by" property="updatedBy"></result>
<collection property="subMenuList" ofType="com.lagou.domain.Menu" select="com.lagou.dao.MenuMapper.findSubMenuListByPid" column="id">
</collection>
</resultMap>
<!--查询所有父子菜单信息-->
<select id="findSubMenuListByPid" resultMap="menuResult">
SELECT * FROM menu WHERE parent_id = #{id}
</select>
3.3根据角色查询菜单信息
分析需要根据J角色id来获取到对应的菜单,菜单和角色都是多对多关系,所以就要三表联查来查询
dao层代码 这个功能就dao层要敲点东西,另外两层都是直接掉
<!--根据角色id查询对应的菜单 多对多查询,
先查询出来角色id=中间表的erole_id然后再来菜单表的id要等于中间表测menuy_id这样就角色对应的关系全部查询出来了-->
<select id="findMenuByRoleId" resultType="int" parameterType="int">
SELECT me.id FROM roles rs LEFT JOIN role_menu_relation rr on rs.id=rr.role_id
LEFT JOIN menu me on me.id=rr.menu_id WHERE rs.id=#{id}
</select>
3.3给角色分配对应的菜单
分析:角色表跟菜单表一直都是多对多关系,所以给角色分配菜的时候就是要给中间表添加字段,比如一个角色对应多少个菜单功能就在中间表menu_id字段无脑加就行了
但是需要先清空关系
上dao层代码
<!--根据role传递过来的id删除中间表与菜单得关系-->
<delete id="deletRoleContextMenu" parameterType="int">
delete from role_menu_relation where role_id=#{id}
</delete>
<!--为角色分配菜单-->
<insert id="RoleContextMenu" parameterType="com.lagou.domain.Role_menu_relation">
insert into
role_menu_relation value(null,#{menuId},#{roleId},#{createdTime},#{updatedTime},#{createdBy},#{updatedby})
</insert>
Service层的代码 因为传递过来的menu_id都是一个集合数组,所有要遍历他,遍历一次传递一次也就添加一次
/*根据角色id分配菜单*/
@Override
public void RoleContextMenu(RoleMenuVo roleMenuVo) {
// 先删除关系表对应的用户,然后再分配
roleMapperA.deletRoleContextMenu(roleMenuVo.getRoleId());
//再次分配
for (Integer o : roleMenuVo.getMenuIdList()) {
Role_menu_relation role_menu_relation=new Role_menu_relation();
//用户id
role_menu_relation.setRoleId(roleMenuVo.getRoleId());
//菜单id
role_menu_relation.setMenuId(o);
role_menu_relation.setCreatedTime(new Date());
role_menu_relation.setUpdatedTime(new Date());
role_menu_relation.setCreatedBy("system");
role_menu_relation.setUpdatedby("system");
roleMapperA.RoleContextMenu(role_menu_relation);
}
}
3.5用户登录
serivce
//用户登录
@Override
public User UserLogin(User user) throws Exception {
User user2 = userMapper.UserLogin(user);
//判断用户是否存在 思路就是判断用户传入进来的用户是否存在,如果用户存在,那么获取到他查询出来
//的密码然后跟用户输入进来的密码经过md5加密之后再次进行对比判断是否正确
if (user2 != null && Md5.verify(user.getPassword(), "lagou", user2.getPassword())) {
return user2;
} else {
return null;
}
}
这里用到了md5加密算法,来分别对比用户输入的密码和数据库的密码一不一致
3.6用户对应的权限动态展示功能 (比如普通用户对应的就是普通角色 ,超级用户对应的就是超级角色可以做任何事)
dao
<!--2.根据角色id查询父菜单 因为角色表和菜单表示多对多的关系,所以要三表联查 GROUP BY m.id-->
<select id="findParentMenuByRoleId" parameterType="java.util.List" resultType="com.lagou.domain.Menu">
<!-- select distinct e.* from menu e inner join role_menu_relation rm on e.id=rm.menu_id
inner join roles r on r.id=rm.role_id
where m.parent_id=-1 and r.id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
-->
SELECT
DISTINCT m.*
FROM
roles r INNER JOIN role_menu_relation rm ON r.id = rm.role_id
INNER JOIN menu m ON m.id =rm.menu_id
WHERE
m.parent_id = -1 AND r.id IN
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<!--3.根据父菜单关联的子菜单进行查询,因为子菜单parent对应的父菜单就是parent为-1的id,这就是内查询-->
<select id="findSubMenuByPid" resultType="com.lagou.domain.Menu" parameterType="int">
select * from menu where parent_id=#{pid}
</select>
<!--4.获取资源信息,角色和资源表又是一个多对多的关系,所以又要三表联查了,真的服了-->
<select id="findResourceByRoleId" parameterType="java.util.List" resultType="com.lagou.domain.Resource">
select distinct c.* from resource c inner join role_resource_relation rr on c.id=rr.resource_id
inner join roles r on r.id=rr.role_id
where r.id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
总结
还有添加修改回显动态权限这些功能不想记了,摆烂·了