在比较大的项目中,存在数百个表,也就意味着有数百个实体类,实体类之间的对应关系太麻烦,所以我找了一个捷径,使用map代替实体类。
1.使用map接收参数
@RequestMapping("/querylightControlLandscape")
public JsonResult querylightControlLandscape(@RequestParam Map<String, Object> params){
try {
IPage page=lightService.querylightControlLandscape(params);
return new JsonResult(page);
}catch (Exception e){
e.printStackTrace();
Map map=new HashMap();
map.put("msg","程序异常");
map.put("flag",false);
return new JsonResult(map);
}
}
这种是比较普通的传参,如果map里面想要放数组或list,传参会出问题,需要用到数组或list的请参考下面的代码
public JsonResult addTimeTaskGroup(@RequestParam Map<String, Object> params,@RequestParam List path){
}
2.使用map的注意事项
map里面的数据类型为String,所以,使用其他数据类型的时候请参考以下代码
int start = Integer.parseInt((String)params.get("start"));
int limit = Integer.parseInt((String)params.get("limit"));
还要注意的是,在使用实体类或者全写的时候,有些参数可以不传,但使用map,如果有些参数不传,最好使用三元表达式进行判断赋值
String equipmentType=params.get("equipmentType")==null?"": (String) params.get("equipmentType");
3.mapper层接口
IPage querylightControlLandscape1(Page<Map> page, @Param("params")Map<String,Object> params);
不必在意那个page,这是自己封装的分页
4.sql的返回结果集
<select id="querylightControlLandscape1" resultType="java.util.Map">
SELECT
CUBICLE_NAME,
CUBICLE_ID
FROM
C_CUBICLE A
WHERE
A .ORG_NO IN (
SELECT
A .ORG_NO
FROM
O_ORG A
WHERE
A .ORG_TYPE = '03'
AND A .CALC_ORDER = 99 START WITH A .ORG_NO = #{params.statusCode1}
CONNECT BY PRIOR ORG_NO = P_ORG_NO
)
AND A .CUBICLE_TYPE = #{params.cubicleType}
<if test="params.cubicleName!=null and params.cubicleName!=''">
AND A.CUBICLE_NAME like '%'||#{params.cubicleName}||'%'
</if>
</select>
到这里你就得到了你需要的结果,但是,这里的结果集有问题,字段结果为null的不返回,key值也不是驼峰式命名,那如何解决?
5.对返回的map结果集进行处理
5.1.在application.yml文件中对mybatis进行配置
这个配置的作用是可以返回null
mybatis-plus:
configuration:
#开启驼峰式mm
map-underscore-to-camel-case: true
auto-mapping-behavior: partial
# mybatis返回map为空返回null
call-setters-on-nulls: true
check-config-location:
mapper-locations: classpath:/com/nari/sea/lightmonitor/mapper/*Mapper.xml
config-location: classpath:/com/nari/sea/lightmonitor/mapper/config/mybatis-config.xml
global-config:
db-config:
column-format: true
如果已经配置了mybatis,请用这段代码进行替换
5.2.添加配置类
该类的作用是进行驼峰式命名
package com.nari.sea.lightmonitor.utils;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
@Configuration
public class MybatisConfig {
/**
* mybatis resultType为map时下划线键值转小写驼峰形式插
*/
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setObjectWrapperFactory(new MapWrapperFactory());
}
static class MapWrapperFactory implements ObjectWrapperFactory {
@Override
public boolean hasWrapperFor(Object object) {
return object != null && object instanceof Map;
}
@Override
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
return new MyMapWrapper(metaObject, (Map) object);
}
}
static class MyMapWrapper extends MapWrapper {
MyMapWrapper(MetaObject metaObject, Map<String, Object> map) {
super(metaObject, map);
}
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
if (useCamelCaseMapping
&& ((name.charAt(0) >= 'A' && name.charAt(0) <= 'Z')
|| name.contains("_"))) {
return underlineToCamelhump(name);
}
return name;
}
/**
* 将下划线风格替换为驼峰风格
*
* @param inputString
* @return
*/
private String underlineToCamelhump(String inputString) {
StringBuilder sb = new StringBuilder();
boolean nextUpperCase = false;
for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
if (c == '_') {
if (sb.length() > 0) {
nextUpperCase = true;
}
} else {
if (nextUpperCase) {
sb.append(Character.toUpperCase(c));
nextUpperCase = false;
} else {
sb.append(Character.toLowerCase(c));
}
}
}
return sb.toString();
}
}
}
希望这篇文章能对你有帮助,记得点个赞