作者 朱兆平

用户的权限配置 加入事务

... ... @@ -18,6 +18,7 @@
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.BUILD-SNAPSHOT</spring-cloud.version>
<fastjson_version>1.2.28</fastjson_version>
<lombok_sersion>1.18.6</lombok_sersion>
</properties>
<dependencies>
... ... @@ -86,6 +87,13 @@
</dependency>
<!--spring boot-->
<!-- tools-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok_sersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
... ...
... ... @@ -20,6 +20,6 @@
* 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能
* 集成Spring Cloud
* 集成websocket
* 将会集成lombok,简化部分代码录入,比如实体类,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html)
* 集成lombok,简化部分代码录入,比如实体类,方便实体及表结构修改,敏捷开发必用,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html)
)
... ...
... ... @@ -11,9 +11,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableScheduling
//@EnableEurekaClient
@EnableTransactionManagement
@MapperScan("com.tianbo.warehouse.dao")
public class WarehouseApplication {
... ...
... ... @@ -5,6 +5,7 @@ import com.tianbo.warehouse.annotation.LogAnnotation;
import com.tianbo.warehouse.annotation.UserPasswordMd5;
import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.model.UserRole;
import com.tianbo.warehouse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
... ... @@ -43,12 +44,8 @@ public class UserController {
public ResultJson updateUserById(@RequestBody @Valid USERS user){
int i = userService.updateByPrimaryKeySelective(user);
ResultJson resultJson = new ResultJson();
if (1==i){
resultJson = new ResultJson("200","success");
}else {
resultJson = new ResultJson("500","update faild");
}
return resultJson;
return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild");
}
@UserPasswordMd5
... ... @@ -62,12 +59,8 @@ public class UserController {
int i = userService.insertSelective(user);
ResultJson resultJson = new ResultJson();
if (1==i){
resultJson = new ResultJson("200","新建账户成功");
}else {
resultJson = new ResultJson("500","insert faild");
}
return resultJson;
return i==1 ? new ResultJson("200","新建账户成功") :new ResultJson("500","insert faild");
}
@LogAnnotation(moduleName = "用户管理",operate = "用户删除")
... ... @@ -76,12 +69,17 @@ public class UserController {
//String username = getusername();
int i = userService.deleteByPrimaryKey(user.getUserId());
ResultJson resultJson = new ResultJson();
if (1==i){
resultJson = new ResultJson("200","删除账户成功");
}else {
resultJson = new ResultJson("500","delete faild");
}
return resultJson;
return i==1 ? new ResultJson("200","删除账户成功") :new ResultJson("500","delete faild");
}
@PutMapping("/user/roleset")
public ResultJson roleSet(@RequestBody UserRole userRole){
int i = userService.setUserRole(userRole);
return i==1 ? new ResultJson("200","设置权限成功") :new ResultJson("500","设置权限失败");
}
}
... ...
package com.tianbo.warehouse.dao;
import com.tianbo.warehouse.model.UserRole;
public interface UserRoleMapper {
int deleteByPrimaryKey(Integer id);
int deleteByUserId(Integer userId);
int insert(UserRole record);
int insertSelective(UserRole record);
UserRole selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(UserRole record);
int updateByPrimaryKey(UserRole record);
}
\ No newline at end of file
... ...
package com.tianbo.warehouse.model;
import lombok.Data;
import java.util.List;
@Data
public class UserRole {
private Integer id;
private Integer userId;
private Integer roleId;
private List<Integer> roleIds;
public UserRole(){
}
public UserRole(Integer userId,Integer roleId){
this.roleId=roleId;
this.userId = userId;
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.tianbo.warehouse.service;
import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.model.UserRole;
import java.util.List;
... ... @@ -15,4 +16,6 @@ public interface UserService {
int insertSelective(USERS record);
int deleteByPrimaryKey(Integer userId);
int setUserRole(UserRole userRole);
}
... ...
... ... @@ -6,12 +6,15 @@ import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.dao.PERMISSIONMapper;
import com.tianbo.warehouse.dao.ROLEMapper;
import com.tianbo.warehouse.dao.USERSMapper;
import com.tianbo.warehouse.dao.UserRoleMapper;
import com.tianbo.warehouse.model.PERMISSION;
import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.model.USERS;
import com.tianbo.warehouse.model.UserRole;
import com.tianbo.warehouse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
... ... @@ -27,6 +30,9 @@ public class UserServiceImpl implements UserService{
@Autowired
private PERMISSIONMapper permissionMapper;
@Autowired
private UserRoleMapper userRoleMapper;
@Override
public USERS loadByUsername(String username){
List<USERS> userList = usersMapper.selectByUsername(username);
... ... @@ -90,4 +96,32 @@ public class UserServiceImpl implements UserService{
return 0;
}
}
/**重置用户的权限
*这里需要开启事务,删除 或者插入不生效就回滚
* @param userRole
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int setUserRole(UserRole userRole){
try{
Integer userId = userRole.getUserId();
List<Integer> ids = userRole.getRoleIds();
userRoleMapper.deleteByUserId(userId);
if (null!=ids && !ids.isEmpty()){
for (Integer id:ids) {
UserRole ur = new UserRole(userId,id);
userRoleMapper.insertSelective(ur);
}
}
return 1;
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
}
... ...
... ... @@ -45,6 +45,6 @@
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="log" domainObjectName="LOG" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
\ No newline at end of file
... ...
<?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.tianbo.warehouse.dao.UserRoleMapper" >
<resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.UserRole" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="user_id" property="userId" jdbcType="INTEGER" />
<result column="role_id" property="roleId" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, user_id, role_id
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user_role
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user_role
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByUserId" parameterType="java.lang.Integer" >
delete from user_role
where user_id = #{userId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.tianbo.warehouse.model.UserRole" >
insert into user_role (id, user_id, role_id
)
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{roleId,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.tianbo.warehouse.model.UserRole" >
insert into user_role
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="userId != null" >
user_id,
</if>
<if test="roleId != null" >
role_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="userId != null" >
#{userId,jdbcType=INTEGER},
</if>
<if test="roleId != null" >
#{roleId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.UserRole" >
update user_role
<set >
<if test="userId != null" >
user_id = #{userId,jdbcType=INTEGER},
</if>
<if test="roleId != null" >
role_id = #{roleId,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.UserRole" >
update user_role
set user_id = #{userId,jdbcType=INTEGER},
role_id = #{roleId,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...