Spring Boot, MyBatis 实现动态传递表名称, 字段名称 查询数据
2018-02-02 10:24
615 查看
摘要: 之前有个需求,需要动态查询某一个表的某些字段,看了下MyBatis的文档,它可以支持的,具体做法如下:
一:Controller层
package boss.portal.web.controller;
import boss.base.web.controller.BaseController;
import boss.base.web.support.ResponseModel;
import boss.portal.web.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* 描述:
* <p>
* Author: 赵新国
* Date: 2018/1/31 17:48
*/
@RestController
@Api(value = "公共接口管理", description = "公共接口管理")
@RequestMapping(value = "/common")
public class CommonController extends BaseController {
@Autowired
private CommonService commonService;
@ApiOperation(value = "根据ID获取指定实体,字段对应的返回值", notes = "根据ID获取指定实体,字段对应的返回值")
@RequestMapping(value = "/get", method = RequestMethod.GET)
public ResponseModel get(@RequestParam(required = false) String ids,
@RequestParam(required = false) String tableName,
@RequestParam(required = false) String fields) {
List<Map<String, Object>> maps = commonService.get(ids, tableName, fields);
return renderSuccess(maps);
}
}
二:Service层
package boss.portal.web.service;
import boss.auth.user.provider.IUserProvider;
import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* 描述:
* <p>
* Author: 赵新国
* Date: 2018/1/31 17:48
*/
@Component
public class CommonService {
@DubboConsumer(lazy = true)
private IUserProvider userProvider;
public List<Map<String,Object>> get(String ids, String tableName, String fields) {
List<Map<String, Object>> maps = userProvider.get(ids, tableName, fields);
return maps;
}
}
三: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集合, 这样你就可以随意查询任何你想要的表和字段了,再也不用担心其他人让你加接口了!
一:Controller层
package boss.portal.web.controller;
import boss.base.web.controller.BaseController;
import boss.base.web.support.ResponseModel;
import boss.portal.web.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* 描述:
* <p>
* Author: 赵新国
* Date: 2018/1/31 17:48
*/
@RestController
@Api(value = "公共接口管理", description = "公共接口管理")
@RequestMapping(value = "/common")
public class CommonController extends BaseController {
@Autowired
private CommonService commonService;
@ApiOperation(value = "根据ID获取指定实体,字段对应的返回值", notes = "根据ID获取指定实体,字段对应的返回值")
@RequestMapping(value = "/get", method = RequestMethod.GET)
public ResponseModel get(@RequestParam(required = false) String ids,
@RequestParam(required = false) String tableName,
@RequestParam(required = false) String fields) {
List<Map<String, Object>> maps = commonService.get(ids, tableName, fields);
return renderSuccess(maps);
}
}
二:Service层
package boss.portal.web.service;
import boss.auth.user.provider.IUserProvider;
import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* 描述:
* <p>
* Author: 赵新国
* Date: 2018/1/31 17:48
*/
@Component
public class CommonService {
@DubboConsumer(lazy = true)
private IUserProvider userProvider;
public List<Map<String,Object>> get(String ids, String tableName, String fields) {
List<Map<String, Object>> maps = userProvider.get(ids, tableName, fields);
return maps;
}
}
三: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集合, 这样你就可以随意查询任何你想要的表和字段了,再也不用担心其他人让你加接口了!
相关文章推荐
- spring多数据源的处理 mybatis实现跨库查询 实现Myibatis动态sql跨数据库的处理 Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样
- Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
- MyBatis中的动态SQL,实现不确定条件的CRUD-----使用Map来传递查询的参数
- 三种对list排序的方法,并实现动态传递字段名对list进行排序
- 学习: 实现动态查询列表视图数据(转)
- GridView 动态添加模板列并绑定数据 实现从外部直接传入控件 和 绑定字段参数
- 在Web报表中实现动态添加字段、 动态绑定查询或其它数
- 用ADO的COMMAND对象实现对WEB数据库动态数据查询的方法
- SQL中查询数据表字段名称的查询语句
- 一步步学Mybatis-怎么样实现动态SQL查询(6)
- mybatis学习笔记三(实现关联数据的查询)
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询
- SQL语句实现跨Sql server数据库操作实例 - 查询远程SQL,本地SQL数据库与远程SQL的数据传递
- 使用Python创建MySQL数据库实现字段动态增加以及动态的插入数据
- mybatis实战教程(mybatis in action)之四:实现关联数据的查询(转)
- 动态sql语句基本语法(字段名,表名,数据库名之类作为变量时,必须用动态SQL如ALTER TABLE中使用程序传递的参数)
- SQL中查询数据表字段名称的查询语句
- SQL中查询数据表字段名称的查询语句[转]
- 根据一个表中的字段属性名称作为查询条件来批量修改该表中数据
- 实现动态查询列表视图数据