代码
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.first.pet.platform.entity.PlatformAddress;
import com.first.pet.platform.mapper.PlatformAddressMapper;
import com.first.pet.platform.service.IPlatformAddressServiceTest;
import com.first.pet.threadPool.ThreadPoolUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
*
* 服务类
*
*
* @author yangquan
* @since 2023-09-26
*/
@Service
public class IPlatformAddressServiceTestImpl extends ServiceImpl implements IPlatformAddressServiceTest {
@Resource
private PlatformAddressMapper platformAddressMapper;
//每次查询500条数据操作
/**
* 组装数据
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void assembleAddressData() throws InterruptedException {
// 1.创建任务计数器 参数为设置任务数量
//开三个线程,一个线程执行500条
CountDownLatch countDownLatch = new CountDownLatch(4);
// 2.开启三个线程 分别执行三个查询
// 查询 性别信息
ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {
@Override
public void run() {
QueryWrapper addressWrapper = new QueryWrapper<>();
addressWrapper.last("limit 1500,500");
List platformAddresses = platformAddressMapper.selectList(addressWrapper);
List platformAddress = getPlatformAddress(platformAddresses);
platformAddressMapper.updateDataById(platformAddress);
// 得到查询结果
// 计数器减一
countDownLatch.countDown();
}
});
// 查询 地区分布
ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {
@Override
public void run() {
QueryWrapper addressWrapper = new QueryWrapper<>();
addressWrapper.last("limit 2000,500");
List platformAddresses = platformAddressMapper.selectList(addressWrapper);
List platformAddress = getPlatformAddress(platformAddresses);
platformAddressMapper.updateDataById(platformAddress);
// 得到查询结果
// 计数器减一
countDownLatch.countDown();
}
});
// 查询 注册量
ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {
@Override
public void run() {
QueryWrapper addressWrapper = new QueryWrapper<>();
addressWrapper.last("limit 2500,500");
List platformAddresses = platformAddressMapper.selectList(addressWrapper);
List platformAddress = getPlatformAddress(platformAddresses);
platformAddressMapper.updateDataById(platformAddress);
// 得到查询结果
// 计数器减一
countDownLatch.countDown();
}
});
// 查询 注册量
ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {
@Override
public void run() {
QueryWrapper addressWrapper = new QueryWrapper<>();
addressWrapper.last("limit 3000,500");
List platformAddresses = platformAddressMapper.selectList(addressWrapper);
List platformAddress = getPlatformAddress(platformAddresses);
platformAddressMapper.updateDataById(platformAddress);
// 得到查询结果
// 计数器减一
countDownLatch.countDown();
}
});
// await() 当计数器为0的时候 主线程向下执行 没有这一步的话,如果一旦主线程向下执行
// return map map中可能有的开启的线程还没有执行完毕,即返回的不是线程执行后的结果
countDownLatch.await();
}
private List getPlatformAddress(List platformAddresses) {
platformAddresses.stream().forEach(e -> {
e.setInitials(ToFirstChar(e.getAddressName()).toUpperCase());
e.setCompleteSpelling(ToPinyin(e.getAddressName()));
});
return platformAddresses;
}
public static void main(String[] args) {
String ss = ToFirstChar("安徽省");
System.out.println(ss);
}
/**
* 获取字符串拼音的第一个字母
*
* @param chinese
* @return
*/
public static String ToFirstChar(String chinese) {
String pinyinStr = "";
// char[] newChar = chinese.toCharArray(); //转为单个字符
char[] newChar = new char[]{chinese.toCharArray()[0]}; //转为单个字符
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < newChar.length; i++) {
if (newChar[i] > 128) {
try {
pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinStr += newChar[i];
}
}
return pinyinStr;
}
/**
* 汉字转为拼音
*
* @param chinese
* @return
*/
public static String ToPinyin(String chinese) {
String pinyinStr = "";
char[] newChar = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < newChar.length; i++) {
if (newChar[i] > 128) {
try {
pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinStr += newChar[i];
}
}
return pinyinStr;
}
}
批量更改数据sql
update platform_address set initials=#{item.initials},complete_Spelling = #{item.completeSpelling}
where id =#{item.id}
数据库连接 必须配置,否则不能批量更改,以下是参考链接
https://blog.csdn.net/carbuser_xl/article/details/127045359
url: jdbc:mysql://rm:5888/first_pet_dev?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
拼音依赖
com.belerweb
pinyin4j
2.5.0
线程池工具类
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
* 描述:
* 线程池工具类
* - 所有线程的创建与使用请调用此类的方法
*
* @author zhaofeng
* @date 2023-08-29
*/
public class ThreadPoolUtils {
/**
* http异步请求
* 耗时相对较长,取核心数*2
* 阻塞时间60秒
* 空闲时间超过60秒后销毁线程
*/
public static final ThreadPoolExecutor httpThreadPool = new ThreadPoolImpl(
8, 16, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1024), new ThreadPoolExecutor.AbortPolicy()
);
/**
* 数据库操作请求
* 相较http请求耗时较短
* 取核心线程数*1
* 阻塞时间60秒
*/
public static final ThreadPoolExecutor sqlThreadPool = new ThreadPoolImpl(
4, 8, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1024), new ThreadPoolExecutor.AbortPolicy()
);
}