The field is too long when dropping the database , Two solutions :
If the information is not particularly important , For example, log information does not need to be extended to the database . Recently, I made a small optimization , It is to intercept and drop the super long fields , avoid mybatis Report errors , This field is defined in the database as 255 Characters .
substring I should think of the most direct way , The code in my mind is like this ( It turns out that he is the same .
public static void main(String[] args) {
String a = "\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219\\u89c4\\u5219";
System.out.println(a);
if (a.length() > 255) {
System.out.println(a.substring(0, 255));
} else {
System.out.println(a);
}
}
First, I write a very long field directly to the library , It is found that it can be directly dropped into the warehouse , And automatically intercept my super long fields , It doesn't matter whether it's a visualization tool or mysql The version of may cause automatic interception , Interested students can start the local database to try .
But when I tested it, I found that , The length of the data directly dropped into the database is different from the length I intercepted with the above code . The intermediate process is omitted , Draw a conclusion directly , Because the data we have stored is unicode code , I'm copying string To idea When testing the above code in idea Will automatically help me add translation characters , Note that after a little processing, the result will be normal .
One more thing to note is ,mybatis Maybe because of different versions , Cause interception 255 The length of , Automatically add some data ( To be examined ), So if it must be increased to 255 Words ( The maximum length of the field in the database ), Be sure to go through local tests first .
summary :
Finally, I recommend two that I use in this process python Method :
def rawUnicode():
s1 = '\u54c8'
# It can be counted directly uni After decoding str length
print(len(s1))
# How to use python obtain uni code
s2 = ' Ha '
ss = s2.encode('raw_unicode_escape')
sss = ss.decode()
print(sss)
rawUnicode()