当前位置:   article > 正文

mysql8+mybatis-plus 查询json格式数据_mybatis-plus json字段查询

mybatis-plus json字段查询

sql 测试json表

  1. CREATE TABLE `testjson` (
  2. `id` int NOT NULL AUTO_INCREMENT,
  3. `json_obj` json DEFAULT NULL,
  4. `json_arr` json DEFAULT NULL,
  5. `json_str` varchar(100) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  8. INSERT INTO test2.testjson
  9. (id, json_obj, json_arr, json_str)
  10. VALUES(1, '{"age": "1", "sex": "123"}', '[1, 2, 4]', '12');
  11. INSERT INTO test2.testjson
  12. (id, json_obj, json_arr, json_str)
  13. VALUES(2, '{"age": "1", "sex": "123"}', '[1, 2, 4]', '12');
  14. INSERT INTO test2.testjson
  15. (id, json_obj, json_arr, json_str)
  16. VALUES(3, '{"age": "1", "sex": "123"}', '[1, 2, 4]', '12');
  17. INSERT INTO test2.testjson
  18. (id, json_obj, json_arr, json_str)
  19. VALUES(4, '{"age": "1", "sex": "123"}', '[1, 2, 4]', '12');
  20. INSERT INTO test2.testjson
  21. (id, json_obj, json_arr, json_str)
  22. VALUES(5, '{"age": "1", "sex": "123"}', '[1, 2, 4]', '12');

后台springboot 文件pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springboot-test</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.baomidou</groupId>
  26. <artifactId>mybatis-plus-boot-starter</artifactId>
  27. <version>3.3.0</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. <version>8.0.11</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

application.yml

  1. spring:
  2. dataSource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. password: root
  5. url: jdbc:mysql://localhost:3307/test2?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
  6. username: root

实体类User.java

  1. import com.baomidou.mybatisplus.annotation.IdType;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
  6. import lombok.Data;
  7. import java.io.Serializable;
  8. @Data
  9. @TableName(value = "testjson", autoResultMap=true)
  10. public class User implements Serializable {
  11. private static final long serialVersionUID = 1L;
  12. @TableId(value = "id", type = IdType.AUTO)
  13. private Long id;
  14.     @TableField(typeHandler = JacksonTypeHandler.class)
  15. private JsonObj jsonObj;
  16.     private String jsonArr;
  17.     private String jsonStr;
  18. }

JsonObj,java

  1. @Data
  2. public class JsonObj {
  3. private String sex;
  4. private Integer age;
  5. }

UserMapper.java

  1. @Mapper
  2. public interface UserMapper extends BaseMapper<User> {
  3. User selectById(Long id);
  4. User selectByLike(String sex);
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.springboot.mapper.UserMapper">
  4. <resultMap id="BaseResultMap" type="com.springboot.entity.User">
  5. <id column="id" property="id"/>
  6. <result column="json_obj" property="jsonObj"
  7.         typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
  8. <result column="json_arr" property="jsonArr"/>
  9. <result column="json_str" property="jsonStr"/>
  10. </resultMap>
  11. <select id="selectById" resultMap="BaseResultMap">
  12. select *from testjson where `id`=#{id}
  13. </select>
  14. <select id="selectByLike" resultMap="BaseResultMap">
  15. select * from testjson where json_obj->'$.sex' like '%'+#{sex}'+%'
  16. </select>
  17. </mapper>
  1. import com.springboot.mapper.UserMapper;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class TestController {
  8. @Autowired
  9. UserMapper userMapper;
  10. @GetMapping("/testJson")
  11. public String testJson(){
  12. return "User===:"+userMapper.selectById(1L);
  13. }
  14. @GetMapping("/testJsonLike")
  15. public String testJsonLike(){
  16. return "User===:"+userMapper.selectByLike("123");
  17. }
  18. }

备注:常用JSON函数

//插入和更新

JSON_SET和JSON_INSERT区别:set key存在会覆盖value,insert只会插入新的key,value

UPDATE testjson SET json_obj = JSON_SET(json_obj,'$.age','localhost','$.url','www.muscleape.com') WHERE id = 2;

UPDATE testjson SET json_obj = JSON_INSERT(json_obj,'$.url','www.test.com') WHERE id = 3;

//remove元素

UPDATE testjson SET json_obj = json_remove(json_obj, '$.age') WHERE id = 5;

//模糊查询

select * from testjson where json_obj->'$.url' like '%muscleape%'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/988933
推荐阅读
相关标签
  

闽ICP备14008679号