持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第9天,点击查看活动详情
获取登录的用户部门角色和用户名字
1.新建entity
新建:Employee
package com.imooc.oa.entity;
public class Employee {
private Long employeeId;
private String name;
private Long departmentId;
private String title;
private Long level;
//getter和setter
}
新建Department:
package com.imooc.oa.entity;
public class Department {
private Long departmentId;
private String departmentName;
//getter和setter
}
2.mappers
新增employee.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="com.imooc.oa.dao.EmployeeDao">
<select id="selectById" parameterType="Long" resultType="com.imooc.oa.entity.Employee">
select * from adm_employee where employee_id=#{value}
</select>
</mapper>
新建department.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="com.imooc.oa.dao.DepartmentDao">
<select id="selectById" parameterType="Long" resultType="com.imooc.oa.entity.Department">
select * from adm_department where department_id=#{value}
</select>
</mapper>
3.mybatis-confit.xml
引入配置:
4.dao
新增EmployeeDao接口
package com.imooc.oa.dao;
import com.imooc.oa.entity.Employee;
public interface EmployeeDao {
public Employee selectById(Long employeeId);
}
新建DepartmentDao:
package com.imooc.oa.dao;
import com.imooc.oa.entity.Department;
public interface DepartmentDao {
public Department selectById(Long departmentId);
}
5.service
定义selectById实现类
新建EmployeeService:
package com.imooc.oa.service;
import com.imooc.oa.dao.EmployeeDao;
import com.imooc.oa.entity.Employee;
import com.imooc.oa.utils.MybatisUtils;
public class EmployeeService {
public Employee selectById(Long employeeId){
return (Employee)MybatisUtils.executeQuery(sqlSession -> {
EmployeeDao employeeDao =sqlSession.getMapper(EmployeeDao.class);
return employeeDao.selectById(employeeId);
});
}
}
新建departmentService
package com.imooc.oa.service;
import com.imooc.oa.dao.DepartmentDao;
import com.imooc.oa.entity.Department;
import com.imooc.oa.utils.MybatisUtils;
public class DepartmentService {
public Department selectById(Long departmentId){
return (Department)MybatisUtils.executeQuery(sqlSession -> {
DepartmentDao departmentDao = sqlSession.getMapper(DepartmentDao.class);
return departmentDao.selectById(departmentId);
});
}
}
6.controller
public class IndexServlet extends HttpServlet {
RbacDao rbacDao = new RbacDao();
EmployeeService employeeService = new EmployeeService();
DepartmentService departmentService = new DepartmentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("login_user");
Employee employee = employeeService.selectById(user.getEmployeeId());
Department department = departmentService.selectById(employee.getDepartmentId());
List<Node> nodeList = rbacDao.selectNodeByUserid(user.getUserId());
session.setAttribute("current_employee",employee);
session.setAttribute("current_department",department);
request.setAttribute("node_list",nodeList);
request.getRequestDispatcher("/index.ftl").forward(request,response);
}
代码说明:
- Employee employee = employeeService.selectById(user.getEmployeeId()):通过employeeId获取employee对象
- Department department = departmentService.selectById(employee.getDepartmentId()):获取部门信息
- session.setAttribute("current_employee",employee); session.setAttribute("current_department",department);:把employee和department信息写入session