Spring Boot, MyBatis 实现动态传递表名称, 字段名称 查询数据

摘要: 之前有个需求,需要动态查询某一个表的某些字段,看了下MyBatis的文档,它可以支持的,具体做法如下:

一:Controller层

 1 package boss.portal.web.controller;
 2  
 3 import boss.base.web.controller.BaseController;
 4 import boss.base.web.support.ResponseModel;
 5 import boss.portal.web.service.CommonService;
 6 import io.swagger.annotations.Api;
 7 import io.swagger.annotations.ApiOperation;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.bind.annotation.RestController;
13  
14 import java.util.List;
15 import java.util.Map;
16  
17 /**
18  * 描述:
19  * <p>
20  * Author: 赵新国
21  * Date: 2018/1/31 17:48
22  */
23 @RestController
24 @Api(value = "公共接口管理", description = "公共接口管理")
25 @RequestMapping(value = "/common")
26 public class CommonController extends BaseController {
27  
28     @Autowired
29     private CommonService commonService;
30  
31     @ApiOperation(value = "根据ID获取指定实体,字段对应的返回值", notes = "根据ID获取指定实体,字段对应的返回值")
32     @RequestMapping(value = "/get", method = RequestMethod.GET)
33     public ResponseModel get(@RequestParam(required = false) String ids,
34                              @RequestParam(required = false) String tableName,
35                              @RequestParam(required = false) String fields) {
36         List<Map<String, Object>> maps = commonService.get(ids, tableName, fields);
37         return renderSuccess(maps);
38     }
39  
40 }

二:Service层

 1 package boss.portal.web.service;
 2  
 3 import boss.auth.user.provider.IUserProvider;
 4 import com.alibaba.boot.dubbo.annotation.DubboConsumer;
 5 import org.springframework.stereotype.Component;
 6  
 7 import java.util.List;
 8 import java.util.Map;
 9  
10 /**
11  * 描述:
12  * <p>
13  * Author: 赵新国
14  * Date: 2018/1/31 17:48
15  */
16 @Component
17 public class CommonService {
18  
19     @DubboConsumer(lazy = true)
20     private IUserProvider userProvider;
21  
22     public List<Map<String,Object>> get(String ids, String tableName, String fields) {
23         List<Map<String, Object>> maps = userProvider.get(ids, tableName, fields);
24         return maps;
25     }
26 }

三:Provider层

@Override
public List<Map<String, Object>> get(String ids, String tableName, String fields) {
    List<Map<String, Object>> maps = userMapper.get(ids, tableName, fields);
    if (maps != null && !maps.isEmpty()) {
        return maps;
    }
  return null;
}

四:Mapper层

List<Map<String,Object>> get(@Param("ids") String ids, @Param("tableName") String tableName, @Param("fields") String fields);

五:Mapper.xml

<!-- 根据指定ID获取指定数据表的指定字段的数据集 -->
    <select id="get" resultType="java.util.Map" statementType="STATEMENT" >
        select ${fields} from ${tableName} where id in ( ${ids} )
    </select>

附录: 最后说明一下,fields代表你要查询的字段,tableName代表你要查询的表名称,ids代表你要查询的id集合, 这样你就可以随意查询任何你想要的表和字段了,再也不用担心其他人让你加接口了!

————————————————
版权声明:本文为CSDN博主「迷彩的博客」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sxdtzhaoxinguo/java/article/details/79235998

原文地址:https://www.cnblogs.com/it-deepinmind/p/13085033.html