七 员工管理
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>
<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