1.1 后台的数据库由于历史原因音频的的名字存储添加了许多的标题,如:67.宝宝睡觉觉,13_神灯,4_13 The Flying Vet,02、两只老虎,25 de Diciembre,04 _ Peek_a_Boo等等。现在运营的需求批量的将名字前面的序号或者下划线去掉,如:67.宝宝睡觉觉 改成 宝宝睡觉觉,13_神灯 改成 神灯......
2.1 后台数据几万条,运营一条一条改的话不现实,只能是我们程序猿弄咯。字符串、正则.....完美。赶紧百度一把:mysql 正则表达式。mysql使用正则表达式使用关键字:REGEXP ,开头用:^,结尾:$,区间用:[ ] ,长度用:{}.....花个几分钟把我要的正则安排一下:数字开头,紧接着下划线或者点或者顿号.......
2.2 几个实际案例:
2.2.1 两个数字开头后面是一个点:SELECT * from tb_smusic WHERE name REGEXP "^[0-9]{1,3}[.]"
2.2.2 两个数字开头后,面空格,下划线,空格:SELECT * from tb_smusic WHERE name REGEXP "^[0-9]{1,3}[ ][_][ ]"
2.2.3 接下来就是把原来的名字换成去掉目标字符之后的名字了。这里可能要用到存储函数或者存储过程,这一块用之前有学过,但是一直没用过。所以最后选择java+jdbc来做。
3.1 主要的逻辑就是按照前面的正则匹配的方式把目标字符找出来,然后按照按照正则的规则切割,再然后就是更新就好了。就是一个select和一个update。上代码:
import com.booyue.spider.spider.mapper.baby_mvc.TbSmusicMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BabyMvcTest {
@Autowired
private TbSmusicMapper tbSmusicMapper;
@Autowired
private JdbcTemplate jdbcTemplate;
String pattern1 = "^[0-9]{1,5}[.]";
String pattern2 = "^[0-9]{1,5}[、]";
String pattern3 = "^[0-9]{1,3}[ ][_][ ]";
String pattern4 = "^[0-9]{1,5}[ ]";
String pattern5 = "^[0-9]{1,5}[_]";
String pattern6 = "^[0-9]{1,5}";
final String defaultPattern=pattern1;
@Test
public void getTbSmusicItem() {
String selectSql=
"select tb_smusic.id,tb_smusic.name \n" +
"from \n" +
"tb_smusic,tb_sspecial,tb_sspecial_music \n" +
"WHERE\n" +
" tb_sspecial.id=tb_sspecial_music.special_id \n" +
"AND\n" +
" tb_sspecial_music.music_id=tb_smusic.id\n" +
"AND tb_sspecial.id IN (\n" +
"1010,1011,1012,1014,1019,1028,1037,1038,1043,1048,1049,1050,1060,\n" +
"1061,1062,1064,1065,1066,1067,1101,1102,1103,1104,1105,1106,\n" +
"1111,1112,1113,1114,1115,1116,1117,1118,1120,1121,1122,\n" +
"1123,1124,1125,1126,1127,1128,1130,1151,1157,111174,1175,1176,\n" +
"1181,1182,1183\n" +
")";
String updateSql="update tb_smusic set name =? ,origin_name=? where id=?";
SqlRowSet set = jdbcTemplate.queryForRowSet(selectSql);
while (set.next()) {
Integer id = set.getInt(1);
String name = set.getString(2);
Pattern pattern = Pattern.compile(defaultPattern);
Matcher matcher = pattern.matcher(name);
if(matcher.find())
{
System.out.println(id);
System.out.println(name);
int end = matcher.end();
String[] ss = name.split(defaultPattern, end);
for (String s:ss) {
jdbcTemplate.update(updateSql,s,name,id);
}
}
}
}
}