用mybatis进行模糊查询总是查不到结果!

//IStudentDao.xml

@Override
public List<Student> selectStudentByName(String name) { SqlSession sqlSession = null; List<Student> list = null; try { sqlSession = MySqlSession.getSqlSession(); list = sqlSession.selectList("selectList",name); } catch (IOException e) { e.printStackTrace(); }finally { if(sqlSession!=null){ sqlSession.close(); } } return list; }

test类里

    @Test
    public void test08()
    {
        IStudentDao studentDao = new IStudentDaoImpl();
        List<Student> students = studentDao.selectStudentByName("三");
        System.out.println("查找成功!");
        for (Student student:
             students) {
            System.out.println(student);
        }

    }

IStudentDao.xml(映射文件)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.abc.dao.IStudentDao">
    <select id="selectList"  resultType="com.abc.beans.Student">
        select * from student where name like '%' #{name} '%'
    </select>

</mapper>

为了进行模糊查询,结果总是查询不到结果!!!

分析:

代码没问题,表中也有数据,sql语句放在数据库中也能查询到结果也没问题,

那就是连接的问题,仔细查了一遍也没问题!但是第二天本来要放弃,又找了个帮我看,没看出来,他想要走的时候我试试在连接处添加一个?characterEncoding=utf8,因为我的数据库原本就有编码问题

 <environments default="development">
        <environment id="development">
        <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/><!---->
                <property name="url" value="${jdbc.url}?characterEncoding=utf8"/>
           ...

最初的时候插入数据就乱码。

这个地方出错源自于我潜意识里觉得查询时乱码不会影响查询结果,最后我错了。

最后数据库编码问题会影响插入,修改,模糊查询,复合条件查询中带有like '%x%' 的。但不影响进行所有查询,根据id查询,

不知道原因,日后研究

猜你喜欢

转载自www.cnblogs.com/hyjh/p/11865229.html