在实际的项目中,为了保证数据的简洁和查询的效率,通常会到采用多表联查。
那么什么是多表联查呢?
一般查询语句都是针对一个表的,但是在关系型数据库中,表与表之间是有联系的,所以在实际应用中,经常使用多表查询。多表查询就是同时查询两个或两个以上的表。
在 MySQL 中,多表查询主要有交叉连接、内连接、外连接、分组查询与子查询等5种。
而在项目中的具体应用,如用户信息表采用多表联查,user用户表新增用户一进去就触发方法自动生成账号,账号会在后端进行查重验证,还以手动刷新,自动查询所有部门,选择部门后可查找对应职位展示出来供操作者选择,调用角色表查找所有角色可供选择,新增成功后会分页按时间倒序输出。
代码部分:
userMapper↓
查询部门表、职位表、角色表
@Select("select * from tb_sys_user order by update_time desc")
@Results(id="userMap",value = {
//column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键
@Result(column = "user_id",property = "userId",jdbcType = JdbcType.INTEGER,id = true),
@Result(column = "user_account",property = "userAccount",jdbcType = JdbcType.VARCHAR),
@Result(column = "user_name",property = "userName",jdbcType = JdbcType.VARCHAR),
@Result(column = "user_gender",property = "userGender",jdbcType = JdbcType.VARCHAR),
@Result(column = "user_phone",property = "userPhone",jdbcType = JdbcType.VARCHAR),
@Result(column = "user_email",property = "userEmail",jdbcType = JdbcType.VARCHAR),
@Result(column = "post_id",property = "postId"),
@Result(property = "tbSysPost",column = "post_id",one = @One(select = "com.enterprisedigitization.mapper.IPostMapper.findByPostId",fetchType = FetchType.DEFAULT)),
@Result(property = "tbSysRole",column = "user_id",one = @One(select = "com.enterprisedigitization.mapper.IRoleMapper.findByUserId",fetchType = FetchType.DEFAULT))
})
List findAllUser();
DepartmentMapper↓
查询部门
@Mapper
public interface IDepartmentMapper extends BaseMapper {
@Select("select dt_id,dt_name from tb_sys_department where dt_id = #{dtId}")
@Results(id="dtMap",value = {
//column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键
@Result(column = "dt_id",property = "dtId",jdbcType = JdbcType.INTEGER,id = true),
@Result(column = "dt_name",property = "dtName",jdbcType = JdbcType.VARCHAR),
})
TbSysDepartment findBydtId(@Param("dt_id")String dtId);
}
PostMapper↓
查询部门及其下所设职位
@Select("select post_id,post_name,dt_id from tb_sys_post where post_id = #{postId}")
@Results(id = "postMap", value = {
//column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键
@Result(column = "post_id", property = "postId", jdbcType = JdbcType.INTEGER, id = true),
@Result(column = "post_name", property = "postName", jdbcType = JdbcType.VARCHAR),
@Result(column = "dt_id", property = "dtId", jdbcType = JdbcType.VARCHAR),
@Result(property = "department", column = "dt_id", one = @One(select = "com.enterprisedigitization.mapper.IDepartmentMapper.findBydtId", fetchType = FetchType.DEFAULT))
})
TbSysPost findByPostId(@Param("post_id") Integer postId);
用户实体类
@TableName(value ="tb_sys_user")
@Data
public class TbSysUser implements Serializable {
@TableId(type = IdType.AUTO)//指定实体类的属性为对应的主键 IdType.AUTO数据库ID自增
private Integer userId;//用户id
private Integer postId;//职位id
@TableField(exist = false)
//@TableField(exist = false) 用来解决实体类中有的属性但是数据表中没有的字段 //默认为true
private TbSysPost tbSysPost;//一个用户对应一个职位,1对1
@TableField(exist = false)
private Integer roleId;//角色id
@TableField(exist = false)
private TbSysRole tbSysRole;//一个用户对应一个角色,1对1
@TableField(exist = false)
private List userRoleList;//关联用户角色表
@TableField(exist = false)
private String postName; //审核时根据职位名获取用户名单
private String userAccount;//用户账号
private String userName;//用户名
private String userPassword;//用户密码
private String userPhone;//电话号码
private String userGender;//性别
private String userEmail;//邮箱
private String userAvatar;//用户头像
@TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
private Date updateTime;//修改时间
}
部门实体类:
@Data
@TableName("tb_sys_department")
public class TbSysDepartment {
@TableId(value = "dt_id" ,type = IdType.AUTO)
private Integer dtId;//部门id
private String dtName;//部门名称
private String dtRemark;//部门描述
@TableField(fill = FieldFill.INSERT)
private Date createTime;//成立时间
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;//更新时间
}
职位实体类:
@TableName(value ="tb_sys_post")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TbSysPost implements Serializable {
@TableId(type = IdType.AUTO)
private Integer postId;//职位ID
private String postName;//职位名称
private Integer partNameId;//上级职位ID
private Integer dtId;//所属部门ID
@TableField(exist = false)
private TbSysDepartment department;
@TableField(exist = false)
private TbSysPost tbSysPost;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;//更新时间
}
实际效果↓
点进部门会展示下拉列表↓
选择好部门会展示对应职位↓
点击选择角色↓
mybatis中@Results,@ResultMap注解使用→http://t.csdn.cn/daUhR