版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/meijipingchao629/article/details/81150180
问题描述:
mapper:
<!-- 模糊查询 -->
<select id="getByLikeUsername" parameterType="java.lang.String" resultType="zml.pojo.User">
select * from user t where t.username like #{username}
</select>
查询:
@Test
public void TestLike(){
SqlSession sqlSession = null;
List<User> userList = null;
try{
sqlSession= sqlF.openSession();
userList = sqlSession.selectList("user.getByLikeUsername", "%小明");
}catch(Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
System.out.println(userList);
}
运行结果:
原因:jdbc的url没有添加字符编码
解决方案:
将jdbc.url=jdbc:mysql://localhost:3306/test
改为:
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
jdbc.username=root
jdbc.password=xxx