用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查詢,

不知道原因,日后研究

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM