1.导入依赖:
<!-- 拼音包 --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency>
2.创建主类 pyname
/** * 拼音名字 */ private String pyName; public String getPyName() { return pyName; } public void setPyName(String pyName) { this.pyName = pyName; }
3.实现转化方法
public class PinYinUtil { /** * 将中文转换为拼音的首字母 * * * @return 拼音首字母 */ public static String getPingYin(String userName) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); StringBuilder pinyinFirstLetter = new StringBuilder(); char[] nameChar = userName.toCharArray(); for (char c : nameChar) { try { String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format); if (pinyin != null && pinyin.length > 0) { pinyinFirstLetter.append(pinyin[0].charAt(0)); } else { pinyinFirstLetter.append(c); } } catch (Exception e) { pinyinFirstLetter.append(c); } } return pinyinFirstLetter.toString(); } }
4.在新增 修改等service内调用工具类的转化方法
/** * 新增保存用户信息 * * @param user 用户信息 * @return 结果 */ @Override @Transactional public int insertUser(User user) { user.randomSalt(); user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt())); user.setCreateBy(ShiroUtils.getLoginName()); //新增用户拼音转化 user.setPyName(PinYinUtil.getPingYin(user.getUserName())); // 新增用户信息 int rows = userMapper.insertUser(user); // 新增用户岗位关联 insertUserPost(user); // 新增用户与角色管理 insertUserRole(user.getUserId(), user.getRoleIds()); return rows; }
5.创建定时任务 将转化数据填入数据库中
/** * 名称转拼音 */ public void getPinyin(){ List<User> userList = userMapper.selectUserList(new User()); for (User user : userList) { String pinyin = PinYinUtil.getPingYin(user.getUserName()); // 将用户名转换为拼音 user.setPyName(pinyin); // 将拼音设置到用户对象中 userMapper.updateUser(user); // 更新用户信息 } }
6.修改sql 实现模糊查询 注意运行sql的具体语句
<select id="selectUserList" parameterType="User" resultMap="UserResult"> select u.user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.salt, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark ,u.ven_dor_id,u.wx_name,u.py_name, d.dept_name, d.leader,GROUP_CONCAT(r.role_name) role_names from sys_user u left join sys_dept d on u.dept_id = d.dept_id left join sys_user_role ur on u.user_id = ur.user_id left join sys_role r on r.role_id = ur.role_id where u.del_flag = '0' <if test="userName != null and userName != ''"> AND (u.user_name like concat('%', #{userName}, '%') or u.py_name like concat('%', #{userName}, '%')) </if> <if test="loginName != null and loginName != ''"> AND u.login_name like concat('%', #{loginName}, '%') </if> <if test="status != null and status != ''"> AND u.status = #{status} </if> <if test="phonenumber != null and phonenumber != ''"> AND u.phonenumber like concat('%', #{phonenumber}, '%') </if> <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 --> AND date_format(u.create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d') </if> <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 --> AND date_format(u.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d') </if> <if test="deptId != null and deptId != 0"> AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE FIND_IN_SET (#{deptId},ancestors) )) </if> <!-- 数据范围过滤 --> ${params.dataScope} group by user_id </select>
7.下拉框模糊查询 找到前台页面
<div class="form-group reply1"> <label class="col-sm-3 control-label is-required">接收人:</label> <div class="col-sm-8"> <select id="recipients" class="form-control" required name="recipients"> <option value="">--请选择--</option> <option th:each="recipients:${recipientsList}" th:value="${recipients.userId}" th:text="${recipients.userName}"></option> </select> </div>
8.去xml文件找对应sql
部门和名称拼接 加上拼音拼接 (后缀) 直接添加上拼音的模糊查询
<sql id="selectRecipientsVo"> SELECT rec.id, rec.userId, concat(user1.user_name, '(', sd.dept_name, ')','(', user1.py_name, ')') userName, rec.createBy, rec.createTime, rec.updateBy, rec.updateTime, rec.recipient, rec.personInCharge, user2.user_name createByName FROM recipients rec LEFT JOIN sys_user user1 ON user1.user_id = rec.userId LEFT JOIN sys_dept sd ON user1.dept_id = sd.dept_id LEFT JOIN sys_user user2 ON user2.user_id = rec.createBy </sql> <select id="selectRecipientsList" parameterType="Recipients" resultMap="RecipientsResult"> <include refid="selectRecipientsVo"/> <where> rec.status=0 <if test="userId != null ">and rec.userId like concat('%', #{userId},'%')</if> <if test="userName != null and userName != '' "> and (user1.user_name like concat('%', #{userName},'%') or user1.py_name like concat('%', #{userName},'%')) </if> <if test="recipient != null and recipient != '' ">and rec.recipient = #{recipient}</if> <if test="personInCharge != null and personInCharge != '' ">and rec.personInCharge = #{personInCharge}</if> </where> </select>