09—mybatis注解配置join查询
Posted 金子开发者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了09—mybatis注解配置join查询相关的知识,希望对你有一定的参考价值。
今天来聊mybatis的join查询,怎么说呢,有的时候,join查询确实能提升查询效率,今天举个left join的例子,来看看mybatis的join查询。
就不写的很细了,把主要代码贴出来了。
创建数据表:admin_user、admin_groups
CREATE TABLE `admin_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘用户编号‘, `user_name` varchar(255) NOT NULL COMMENT ‘用户登录账号‘, `pwd` varchar(255) DEFAULT NULL COMMENT ‘登录密码‘, `create_date` datetime DEFAULT NULL COMMENT ‘创建时间‘, `enable_flag` int(20) DEFAULT NULL COMMENT ‘用户状态 0:可用 1:不可用‘, `group_id` int(20) DEFAULT NULL COMMENT ‘分组ID,0超级管理员,1一般管理员‘, `last_login_ip` varchar(255) DEFAULT NULL COMMENT ‘最后一次登录IP地址‘, `last_login_time` datetime DEFAULT NULL COMMENT ‘最后登录时间‘, `login_count` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `user_name` (`user_name`) USING BTREE ) --------------------------------------------------------------------------------- CREATE TABLE `admin_groups` ( `id` int(11) NOT NULL AUTO_INCREMENT, `group_name` varchar(255) DEFAULT NULL, `create_by_user_name` varchar(255) DEFAULT NULL, `create_time` datetime DEFAULT NULL, `update_time` datetime DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, `remarks` varchar(1000) NOT NULL DEFAULT ‘无‘ COMMENT ‘描述‘, `enable_flag` int(2) NOT NULL DEFAULT ‘1‘, PRIMARY KEY (`id`) )
创建实体类:AdminUserJoinGroups.java
package com.palmble.entity; import lombok.Data; import javax.persistence.Table; @Table(name = "admin_user") @Data public class AdminUserJoinGroups { private Long id; private String userName; private String groupName; private String createByUserName; }
service层:AdminUserService.java
@Service public class AdminUserService { @Autowired private AdminUserMapper adminUserMapper; public List<AdminUserJoinGroups> selectAllList() { return adminUserMapper.selectAllList(); } }
mapper层:AdminUserMapper.java
@Mapper public interface AdminUserMapper { @Select(" SELECT u.id,user_name,g.group_name,g.create_by_user_name FROM admin_user u left join admin_groups g on u.group_id=g.id;") @Results(id = "userGroupMap", value = { @Result(id = true, column = "id", property = "id"), @Result(column = "user_name", property = "userName"),
@Result(column = "group_name", property = "groupName"), @Result(column = "create_by_user_name", property = "createByUserName") }) List<AdminUserJoinGroups> selectAllList(); }
测试:
@RestController @RequestMapping("/admin") public class AdminController { @Autowired private AdminUserService adminUserService; @RequestMapping("/getUserGroup") public String getUserGroup() { List<AdminUserJoinGroups> adminUserJoinGroups = adminUserService.selectAllList(); StringBuilder sb = new StringBuilder(); for (AdminUserJoinGroups item : adminUserJoinGroups) { sb.append("id=" + item.getId() + ",username=" + item.getUserName() + ",groupname=" + item.getGroupName() + ",createByUserName=" + item.getCreateByUserName()); } return sb.toString(); }
以上是关于09—mybatis注解配置join查询的主要内容,如果未能解决你的问题,请参考以下文章
[MyBatis黑魔法] 用纯注解实现联合查询(JOIN)的结果映射
阶段3 1.Mybatis_12.Mybatis注解开发_7 Mybatis注解开发一对多的查询配置