• Mybatis 模糊查询 中文问题


    IDE编码:GBK ,换成UTF-8也不行!


    @Test
    public void testSearchStudents() {
    logger.info("查询学生(带条件)");
    Map<String,Object> map=new HashMap<String,Object>();
    //map.put("gradeId", 2);
    map.put("name", "王"); // 中文

    //map.put("name", "a");  英文

    //map.put("age", 17);
    List<Student> studentList=studentMapper.searchStudents(map);
    for(Student student:studentList){
    logger.info(student);
    }
    }


    <select id="searchStudents" parameterType="Map" resultMap="StudentResult">
    select * from t_student 
    <where>
    <if test="gradeId!=null">
    gradeId=#{gradeId}
    </if>
    <if test="name!=null">
    <!-- and name like '%${name}%' -->
    and name like CONCAT('%','${name}','%' )
    </if>
    <if test="age!=nulll">
    and age=#{age}
    </if>
    </where>
    </select>


    假设模糊查询name=英文,可以打印学生信息;但是模糊查询name=中文,查询不到信息。

    我的解决方案是:原来是数据库编码的问题,简单的方法是:在.properties配置文件中的jdbc.url值后面跟上?useUnicode=true&characterEncoding=utf8

    注意:前提环境:Mybatis没有和SpringMvc整合,整合后不需要这样设置,可以有其他更便捷的方法。(纯粹个人理解,希望可以得到更多人的指教)

  • 相关阅读:
    C++编写ATM(2)
    【Python排序搜索基本算法】之Dijkstra算法
    Java中List转换为数组,数组转List
    [置顶] 亚信联创实习笔记
    PL/SQL 异常处理程序
    CSS position财产
    malloc()与calloc差异
    Qt5官方demo分析集10——Qt Quick Particles Examples
    栈和堆之间的差
    深入浅出JMS(一)——JMS简要
  • 原文地址:https://www.cnblogs.com/LEARN4J/p/5332357.html
Copyright © 2020-2023  润新知