因为我的项目里边已经设置返回Map的时候,将Map内的Key转换为驼峰的命名表达式,但是使用BaseMapper时,发现接口传递过来的却是全部小写不符合驼峰的写法,然后我就去对比代码,发现造成这样的结果是因为EntityWrapper没有设置sqlSelect
{
"msg": "查询成功",
"code": 0,
"data": {
"list": [
{
"fdaltertime": null,
"fdalterid": null,
"fdstatus": 1,
"fdmaxusers": null,
"fddiscountamount": 100,
"fdcreateid": null,
"fdname": "满50减10",
"fdcreatetime": null,
"fdhaveusers": null,
"fdid": "1",
"fdminamount": 10,
"fddiscountration": null,
"fdtype": 1,
"fdstartdate": null,
"fdenddate": null
},
{
"fdaltertime": null,
"fdalterid": null,
"fdstatus": 1,
"fdmaxusers": null,
"fddiscountamount": null,
"fdcreateid": null,
"fdname": "满500打8折",
"fdcreatetime": null,
"fdhaveusers": null,
"fdid": "2",
"fdminamount": 10,
"fddiscountration": 20,
"fdtype": 2,
"fdstartdate": null,
"fdenddate": null
}
]
}
}
public Map query(Map params) {
int status = Integer.parseInt(String.valueOf(params.get("fdStatus")));
String studentId = String.valueOf(params.get("fdStudentId"));
EntityWrapper wrapper = new EntityWrapper();
if (2==status) {//已使用
List list = couponDao.getCouponIdListByStudentId(studentId);
wrapper.in("FD_ID", list);
}else {
wrapper.eq("FD_STATUS", status);
}
Map map = new HashMap();
map.put("list", couponDao.selectMaps(wrapper));
return map;
}
后台打印的sql
设置 了sqlSelect
public Map query(Map params) {
int status = Integer.parseInt(String.valueOf(params.get("fdStatus")));
String studentId = String.valueOf(params.get("fdStudentId"));
EntityWrapper wrapper = new EntityWrapper();
if (2==status) {//已使用
List list = couponDao.getCouponIdListByStudentId(studentId);
wrapper.in("FD_ID", list);
}else {
wrapper.eq("FD_STATUS", status);
}
wrapper.setSqlSelect("FD_ID,FD_NAME,FD_END_DATE,FD_TYPE,FD_MIN_AMOUNT,FD_DISCOUNT_AMOUNT,FD_DISCOUNT_RATION");
Map map = new HashMap();
map.put("list", couponDao.selectMaps(wrapper));
return map;
}
接口结果
{
"msg": "查询成功",
"code": 0,
"data": {
"list": [
{
"fdMinAmount": 10,
"fdEndDate": null,
"fdDiscountRation": null,
"fdId": "1",
"fdDiscountAmount": 100,
"fdName": "满50减10",
"fdType": 1
},
{
"fdMinAmount": 10,
"fdEndDate": null,
"fdDiscountRation": 20,
"fdId": "2",
"fdDiscountAmount": null,
"fdName": "满500打8折",
"fdType": 2
}
]
}
}
追本溯源
发现Wrapper.class的源代码
/**
*
* 使用字符串数组封装sqlSelect,便于在不需要指定 AS 的情况下通过实体类自动生成的列静态字段快速组装 sqlSelect,
* 减少手动录入的错误率
*
* @param columns 字段
* @return
*/
public Wrapper setSqlSelect(String... columns) {
StringBuilder builder = new StringBuilder();
for (String column : columns) {
if (StringUtils.isNotEmpty(column)) {
if (builder.length() > 0) {
builder.append(",");
}
builder.append(column);
}
}
this.sqlSelect = builder.toString();
return this;
}
/**
*
* 使用对象封装的setsqlselect
*
*
* @param column 字段
* @return
*/
public Wrapper setSqlSelect(Column... column) {
if (ArrayUtils.isNotEmpty(column)) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < column.length; i++) {
if (column[i] != null) {
String col = column[i].getColumn();
String as = column[i].getAs();
if (StringUtils.isEmpty(col)) {
continue;
}
builder.append(col).append(as);
if (i < column.length - 1) {
builder.append(",");
}
}
}
this.sqlSelect = builder.toString();
}
return this;
}
/**
*
* 使用对象封装的setsqlselect
*
*
* @param columns 字段
* @return
*/
public Wrapper setSqlSelect(Columns columns) {
Column[] columnArray = columns.getColumns();
if (ArrayUtils.isNotEmpty(columnArray)) {
setSqlSelect(columnArray);
}
return this;
}
当没有指定sqlSelect时,会去调用Column.class的getAs()方法
public String getAs() {
if (StringUtils.isEmpty(getColumn()) || StringUtils.isEmpty(as)) {
return StringUtils.EMPTY;
}
String quote = null;
if (isEscape() && SqlRunner.FACTORY != null) {
GlobalConfiguration globalConfig = GlobalConfigUtils.getGlobalConfig(SqlRunner.FACTORY.getConfiguration());
quote = globalConfig.getIdentifierQuote() == null ? globalConfig.getDbType().getQuote() : globalConfig.getIdentifierQuote();
}
return AS + (StringUtils.isNotEmpty(quote) ? String.format(quote, as) : as);
}