Java研学-RBAC权限控制(六)

七 员工管理

1 数据库表 – 员工

CREATE TABLE `employee` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `admin` bit(1) DEFAULT NULL,
  `dept_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

2 Employee 实体类

@Data
public class Employee {
   
    private Long id;
    private String username;
    private String name;
    private String password;
    private String email;
    private Integer age;
    private boolean admin;
    private Department dept;
}

3 EmployeeMapper接口

@Repository
public interface EmployeeMapper {
   
    int deleteByPrimaryKey(Long id);
    int insert(Employee record);
    Employee selectByPrimaryKey(Long id);
    List<Employee> selectAll();
    int updateByPrimaryKey(Employee record);
    List<Employee> selectForList(QueryObject qo);
    void insertRelationBatch(@Param("employeeId") Long id, @Param("roleIds") Long[] roleIds);
    void deleteRelation(Long employeeId);
    Integer checkUsername(String username);
    Employee getByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
}

4 EmployeeMapper.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="cn.tj.mapper.EmployeeMapper" >
  <resultMap id="BaseResultMap" type="cn.tj.domain.Employee" >
    <id column="id" property="id" />
    <result column="username" property="username" />
    <result column="name" property="name" />
    <result column="password" property="password" />
    <result column="email" property="email" />
    <result column="age" property="age" />
    <result column="admin" property="admin" />
    <association columnPrefix="d_" property="dept" javaType="department">
      <result column="id" property="id" />
      <result column="name" property="name" />
      <result column="sn" property="sn" />
    </association>
  </resultMap>
  <delete id="deleteByPrimaryKey" >
    delete from employee
    where id = #{
   id}
  </delete>
  <delete id="deleteRelation">
    delete from employee_role where employee_id = #{
   employeeId}
  </delete>
  <insert id="insert" useGeneratedKeys="true" keyProperty="id" >
    insert into employee (username, name, password, email, age, admin, dept_id
      )
    values (#{
   username}, #{
   name}, #{
   password}, #{
   email}, #{
   age}, #{
   admin}, #{
   dept.id}
      )
  </insert>
    <insert id="insertRelationBatch">
      insert into employee_role(employee_id, role_id) values
      <foreach collection="roleIds" separator="," item="roleId">
        (#{
   employeeId},#{
   roleId})
      </foreach>
    </insert>
    <update id="updateByPrimaryKey" >
    update employee
    set
      name = #{
   name},
      email = #{
   email},
      age = #{
   age},
      admin = #{
   admin},
      dept_id = #{
   dept.id}
    where id = #{
   id}
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" >
    select e.id, e.username, e.name, e.password, e.email, e.age, e.admin,d.id d_id,d.name d_name,d.sn d_sn
    from employee e left join department d on e.dept_id = d.id
    where e.id = #{
   id}
  </select>
  <select id="selectAll" resultMap="BaseResultMap" >
      select id, username, name, password, email, age, admin, dept_id
      from employee
  </select>

/*显示数据1查询2封装3存作用域4展示*/
  <sql id="where_sql">
    <where>
      <if test="keyword != null and keyword != ''">
        and (e.name like concat('%',#{
   keyword},'%') or e.email like concat('%',#{
   keyword},'%'))
      </if>
      <if test="deptId != null">
        and e.dept_id = #{
   deptId}
      </if>
    </where>
  </sql>

  <select id="selectForList" resultMap="BaseResultMap">/*左外可以查出没有部门的员工信息*/
    select e.id, e.username, e.name, e.password, e.email, e.age, e.admin,d.id d_id,d.name d_name,d.sn d_sn
    from employee e left join department d on e.dept_id = d.id
    <include refid="where_sql"/>
  </select>
    <select id="checkUsername" resultType="java.lang.Integer">
      select count(*) from employee where username=#{
   username}
    </select>
  <select id="getByUsernameAndPassword" resultMap="BaseResultMap">
    select e.id, e.username, e.name, e.password, e.email, e.age, e.admin,d.id d_id,d.name d_name,d.sn d_sn
    from employee e left join department d on e.dept_id = d.id
    where username=#{
   username} and password=#{
   password}
  </select>
</mapper>

5 EmployeeController

@Controller
@RequestMapping("/employee")
public class EmployeeController {
   
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    // 处理员工查询所有方法
    @RequestMapping("/list")
    @RequirePermission(name
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰勒疯狂展开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值