数据库MySQL之查询

springboot mysql json 使用

  1. 添加@TableName(value = "table_name",autoResultMap = true)

  2. json实体添加@TableField(typeHandler = FastjsonTypeHandler.class)

    1
    2
    3
    4
    5
    6
    7
    @TableName(value = "table_name",autoResultMap = true)
    pulic class TableName{
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private ExtendConfig extendConfig ;//扩展配置
    @TableField(typeHandler = MTableCellListTypeHandler.class)
    private List<Row> rows;
    }

常见问题

  1. 查询时找不到对应字段,因为json序列话问题,不是驼峰,可以通过@JsonProperty(value = "isFiled")解决

  2. 当json数据是jsonArray时,需要自定义json解析器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class MTableCellListTypeHandler extends JacksonTypeHandler {
    public MTableCellListTypeHandler(Class<?> type) {
    super(type);
    }
    @Override
    protected Object parse(String json) {
    try {
    return getObjectMapper().readValue(json, new TypeReference<List<Row>>() {});
    } catch (IOException e) {
    throw new RuntimeException(e);
    }
    }
    }

查询(SELECT)

查询employee_id为1,2,3的结果
1
2
3
4
5
6
7
8
9
10
#多条件查询
SELECT * FROM employee WHERE employee.employee_id =1 OR employee.employee_id=2 OR employee.employee_id=3
#范围(1~3)查询
SELECT * FROM employee WHERE employee.employee_id BETWEEN 1 and 3
#半开范围查询
SELECT * FROM employee WHERE employee.employee_id<=3
#截取
SELECT SUBSTRING_INDEX('0-21-3-4', '-', 2); -- 结果:0-21
#截取
SELECT SUBSTRING(SUBSTRING_INDEX('0-21-3-4', '-', 2),3); -- 结果:21

更新(UPDATE)

1
2
3
4
5
-- 更新子查询左连接语句模版
update table_name p left join (
select * from xxx
) m on p.id= m.pp_id
set p.app_id=m.app_id where 1=1 ;