<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[还在用Text类型吗？Mysql8.0增强的JSON类型,它不好吗？]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Tue, 07 Dec 2021 02:45:05 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	一、前言<br/>MySQL 支持由 RFC 7159 定义的原生JSON 数据类型，该数据类型可以有效访问 JSON（JavaScript Object Notation）中的元素数据。与将JSON 格式的字符串存储为单个字符串类型相比，JSON 数据类型具有以下优势：<br/><br/>自动验证存储在JSON列中的JSON数据格式。无效格式会报错。<br/>优化的存储格式。存储在JSON列中的JSON文档被转换为允许快速读取访问文档元素的内部格式。内部是以二进制格式存储JSON数据。<br/>对JSON文档元素的快速读取访问。当服务器读取JSON文档时，不需要重新解析文本获取该值。通过key或数组索引直接查找子对象或嵌套值，而不需要读取整个JSON文档。<br/>存储JSON文档所需的空间，大致与LONGBLOB或LONGTEXT相同<br/>存储在JSON列中的任何JSON文档的大小都仅限于设置的系统变量max_allowed_packet的值<br/>MySQL 8.0.13之前，JSON列不能有非null的默认值。<br/>在 MySQL 8.0 中，优化器可以对 JSON 列执行部分就地更新，而不是删除旧JSON串并将新串完整地写入列。<br/>MYSQL 8.0，除了提供JSON 数据类型，还有一组 SQL 函数可用于操作 JSON 的值，例如创建JSON对象、增删改查JSON数据中的某个元素。<br/><br/>二、常用JSON函数<br/>首先，创建表列时候，列要设置为JSON类型：<br/><br/>1<br/>CREATE TABLE t1 (content JSON);<br/>插入数据，可以像插入varchar类型的数据一样，把json串添加单引号进行插入：<br/><br/>1<br/>INSERT INTO t1 VALUES(&#039;&#123;&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;&#125;&#039;);<br/>当然mysql也提供了创建JSON对象的函数：<br/><br/>1<br/>INSERT INTO t1 VALUES(JSON_OBJECT(&quot;key1&quot;,&quot;value1&quot;,&quot;key2&quot;,&quot;value2&quot;));<br/>使用JSON_EXTRACT函数查询JSON类型数据中某个元素的值：<br/><br/><br/>mysql&gt; SELECT&nbsp;&nbsp;JSON_EXTRACT(content,&quot;$.key1&quot;) from t1;<br/><br/>+--------------------------------+<br/>&#124; JSON_EXTRACT(content,&quot;$.key1&quot;) &#124;<br/>+--------------------------------+<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;<br/>+--------------------------------+<br/>2 rows in set (0.00 sec)<br/>lamba表达式风格查询：<br/><br/><br/>mysql&gt; SELECT content-&gt;&quot;$.key1&quot; from t1;<br/>+-------------------+<br/>&#124; content-&gt;&quot;$.key1&quot; &#124;<br/>+-------------------+<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>&#124; &quot;value1&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+-------------------+<br/>2 rows in set (0.00 sec)<br/>使用JSON_SET函数更新JSON中某个元素的值，如果不存在则添加：<br/><br/><br/>mysql&gt; update t1 set content=JSON_SET(content,&quot;$.key1&quot;,&#039;value111&#039;);<br/>Query OK, 2 rows affected (0.00 sec)<br/><br/>Rows matched: 2&nbsp;&nbsp;Changed: 2&nbsp;&nbsp;Warnings: 0<br/>更多JSON类型数据操作函数，可以参考：https://dev.mysql.com/doc/refman/8.0/en/json.html<br/><br/>三、MyBatis中使用JSON类型及其操作函数<br/>比如Device表里面有个JSON类型的content字段，其中含有名称为name的元素，我们来修改和查询name元素对应的值。<br/><br/>ExtMapper中定义修改和查询接口：<br/><br/>@Mapper<br/>public interface DeviceDOExtMapper extends com.zlx.user.dal.mapper.DeviceDOMapper &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;//更新JSON串中名称为name的key的值<br/>&nbsp;&nbsp;&nbsp;&nbsp;int updateName(@Param(&quot;name&quot;) String name, @Param(&quot;query&quot;) DeviceQuery query);<br/>&nbsp;&nbsp;&nbsp;&nbsp;//查询JSON串中名称为name的key的值<br/>&nbsp;&nbsp;&nbsp;&nbsp;String selectName(DeviceQuery query);<br/>&#125;<br/>ExtMapper.xml中定义查询sql:<br/><br/>&lt;mapper namespace=&quot;com.zlx.user.dal.mapper.ext.DeviceDOExtMapper&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--更新JSON串中名称为name的key的值--&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;update id=&quot;updateName&quot; parameterType=&quot;map&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;update device<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;set&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if test=&quot;name != null&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content = JSON_SET(content, &#039;$.name&#039;, #&#123;name,jdbcType=VARCHAR&#125;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/set&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if test=&quot;_parameter != null&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;include refid=&quot;Update_By_Example_Where_Clause&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/update&gt;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--查询JSON串中名称为name的key的值--&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;select id=&quot;selectName&quot; parameterType=&quot;com.zlx.user.dal.model.DeviceQuery&quot; resultType=&quot;java.lang.String&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`content`-&gt;&#039;$.name&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from device<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;if test=&quot;_parameter != null&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;include refid=&quot;Example_Where_Clause&quot;/&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/if&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;<br/>&lt;/mapper&gt;<br/>总结<br/>虽然我们实践上不建议把所有扩展字段都放到一个大字段里面。但是即使有原因一定到放，那么也建议选择JSON类型，而不是varcahr和Text类型。<br/><br/><br/>参考：<br/><a href="https://dev.mysql.com/doc/refman/8.0/en/json.html" target="_blank">https://dev.mysql.com/doc/refman/8.0/en/json.html</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] 还在用Text类型吗？Mysql8.0增强的JSON类型,它不好吗？]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>