mybatis+mysql用中文做查询条件返回值无结果

来源:互联网 发布:软件开放平台 编辑:程序博客网 时间:2024/06/08 05:37

这个报错起源于我在测试mybatsi查询传入多个查询条件的两种方法(注解和map),但是当我传入一个中文查询条件的时候就是无法返回查询结果,传英文的就可以,百思不得其解。

下面看代码:

AJAX

//多个参数查询$.ajax({type : 'post',url : "${pageContext.request.contextPath}/mybatis/queryEmp",data:{queryAge:"28",queryNme:"李大嘴",deptno:"20"},dataType : "json",success : function(data) {console.log(data);$("#data").text(JSON.stringify(data));},error : function() {alert("查询失败");}});

Controller

/** * 多个参数查询 * 1.注解 * 2.传map */@RequestMapping("/queryEmp")//public @ResponseBody Emp queryEmp(String queryAge, String queryNme,Integer deptno){Emp emp=empService.selectEmp(queryAge, queryNme, deptno);System.out.println("controller:"+emp);return emp;}
mapper.java

public interface EmpMapper {public List<Emp> selectEmpByAge(String age);public Emp selectEmp(@Param("age") String queryAge,@Param("name") String queryNme,@Param("deptno") Integer deptno);}

mapper.xml

<!--多个参数查询(3个以上)--><select id="selectEmp"  resultMap="EmpResultMap">SELECT * FROM EMP WHERE AGE = #{age,jdbcType=VARCHAR} AND NAME=#{name,jdbcType=VARCHAR}  AND DEPTNO=#{deptno,jdbcType=DECIMAL}</select>

报错如下:

2017-09-21 11:55:10,884 [http-apr-8080-exec-6] DEBUG [com.wonders.dao.EmpMapper.selectEmp] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@d3955d3]
2017-09-21 11:55:10,892 [http-apr-8080-exec-6] DEBUG [com.wonders.dao.EmpMapper.selectEmp] - ==>  Preparing: SELECT * FROM EMP WHERE AGE = ? AND NAME=? AND DEPTNO=? 
2017-09-21 11:55:10,950 [http-apr-8080-exec-6] DEBUG [com.wonders.dao.EmpMapper.selectEmp] - ==> Parameters: 28(String), 李大嘴(String), 20(Integer)
2017-09-21 11:55:11,036 [http-apr-8080-exec-6] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2d85cc79]
2017-09-21 11:55:11,036 [http-apr-8080-exec-6] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
controller:null


解决办法 数据库配置文件上设置字符集

jdbc_url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8


这样就可以传入汉字“李大嘴”进行查询啦。



阅读全文
0 0
原创粉丝点击