码迷,mamicode.com
首页 > 其他好文 > 详细

09—mybatis注解配置join查询

时间:2019-10-19 17:36:58      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:span   图片   flag   comm   unique   code   entity   alt   using   

今天来聊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查询

标签:span   图片   flag   comm   unique   code   entity   alt   using   

原文地址:https://www.cnblogs.com/itmu89/p/11704559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!