1.数据库中表的字段名是userid,username时,Model类中对应的属性名应对应为: getUsername() getUserid(),而不是getUserName() getUserId()
2.返回单个数值:
int count = ss.selectOne("com.study.mapper.UserMapper.getUserListCount");
response.getWriter().println(count);
3.返回Map类型
Map<Integer, User> map = ss.selectMap("com.study.mapper.UserMapper.getUserList", "userid");
Set<Integer> set = map.keySet();
for (Integer k : set) {
response.getWriter().println(map.get(k).getUserid());
response.getWriter().println(map.get(k).getUsername());
}
4.数据库中存储及提取List类型数据:
mybatis plus将对象list转换为string存储&mybatisplus存储list字符串_mybatis plus list<string>存储-CSDN博客
首先数据库字段为Varchar 或 text 类型
然后定义一个Handler:
@MappedJdbcTypes(JdbcType.VARCHAR) @MappedTypes(List.class) public class ListTypeHandler extends BaseTypeHandler<List<String>> { @Override public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException { // 这里根据实际情况将 List 转换成字符串存储到数据库中 String listAsString = String.join(",", parameter); ps.setString(i, listAsString); } @Override public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException { // 这里从数据库读取字符串,并将其解析为 List String value = rs.getString(columnName); if (value != null) { return Arrays.asList(value.split(",")); } return null; } @Override public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 同上 String value = rs.getString(columnIndex); if (value != null) { return Arrays.asList(value.split(",")); } return null; } @Override public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 同上 String value = cs.getString(columnIndex); if (value != null) { return Arrays.asList(value.split(",")); } return null; } }
其次,实体类写法:
@TableField(value="resourceApplySceneList",typeHandler = ListTypeHandler.class) private ArrayNode resourceApplySceneList; // 只写此写法可以save数据。读取数据还要在类上加上:autoResultMap @TableName(value = "p_pol",autoResultMap = true)
最后,在vue页面初始化数据:
if (record.cabList) { specialPolicy.cabList= record.cabList.split(','); } else { specialPolicy.cabList= []; }