異常代碼:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'stu_name' in 'class java.lang.String'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'stu_name' in 'class java.lang.String'
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy0.findStudentList4(Unknown Source)
at demo.cyj.Test.main(Test.java:69)
接口StudentDao.java
的代碼:
此處的異常用到的方法是第五個方法:
public List<Student> findStudentList4(String stu_name);
package demo.cyj.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import demo.cyj.pojo.Student;
public interface StudentDao {
public List<Student> findStudentList1(Student student);
public List<Student> findStudentList2(Student student);
public int editStudent1(Student student);
public List<Student> findStudentList3(Map<String,Object> map);
public List<Student> findStudentList4(String stu_name);
}
mapperStudentMapper.xml
中用到的部分代碼:
<!-- 用bind實(shí)現(xiàn)模糊查詢 -->
<select id="findStudentList4" resultType="Student">
<bind name="stu_name" value="'%'+stu_name+'%'" />
select * from student where stu_name like #{stu_name}
</select>
原因:
根據(jù)報錯內(nèi)容砸泛,也能看出String類型的參數(shù)中沒有g(shù)etter方法全度,按自己的理解就是這個參數(shù)傳過來沒有用鍵值對的方式來傳遞伍宦,即這個值沒有相應(yīng)的鍵來表示它,所以要給值設(shè)定一個鍵挑势。
解決:
解決方案也不少,下面說兩個吧,一個是自己寫的,不過違背了最初想要傳遞String的初衷而昨;一個是網(wǎng)上搜的,通過注解找田,推薦第二種歌憨。
-
將要傳遞的String值封裝到一個map中,傳遞map
具體代碼實(shí)現(xiàn)如下:
接口StudentDao.java
的代碼:
可以看到午阵,代碼改動在第五個方法那里躺孝,將傳遞的參數(shù)改為了map享扔。
package demo.cyj.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import demo.cyj.pojo.Student;
public interface StudentDao {
public List<Student> findStudentList1(Student student);
public List<Student> findStudentList2(Student student);
public int editStudent1(Student student);
public List<Student> findStudentList3(Map<String,Object> map);
public List<Student> findStudentList4(Map<String,String> map);
}
mapperStudentMapper.xml
的部分代碼:
<!-- 用bind實(shí)現(xiàn)模糊查詢 -->
<select id="findStudentList4" parameterType="map" resultType="Student">
<bind name="stu_name" value="'%'+stu_name+'%'" />
select * from student where stu_name like #{stu_name}
</select>
測試方法:
package demo.cyj;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import demo.cyj.dao.StudentDao;
import demo.cyj.dao.TeacherDao;
import demo.cyj.pojo.Student;
import demo.cyj.pojo.Teacher;
public class Test {
public static void main(String[] args) throws IOException {
String src = "config.xml";
InputStream inputStream = Resources.getResourceAsStream(src);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession(true);
//綁定
StudentDao sd = sqlSession.getMapper(StudentDao.class);
Map<String,String> map = new HashMap<>();
map.put("stu_name", "拐");
List<Student> list = sd.findStudentList4(map);
System.out.println(list);
}
}
運(yùn)行結(jié)果:
-
通過注解底桂,將接口中傳遞的String類型前加一個@Param("stu_name")
具體代碼實(shí)現(xiàn)如下:
接口StudentDao.java
的代碼:
可以看到,代碼改動在第五個方法那里惧眠,配置了一個注解籽懦,相當(dāng)于給String的參數(shù)加了一個鍵。
package demo.cyj.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import demo.cyj.pojo.Student;
public interface StudentDao {
public List<Student> findStudentList1(Student student);
public List<Student> findStudentList2(Student student);
public int editStudent1(Student student);
public List<Student> findStudentList3(Map<String,Object> map);
public List<Student> findStudentList4(@Param("stu_name")String stu_name);
}
mapperStudentMapper.xml
的部分代碼:
<!-- 用bind實(shí)現(xiàn)模糊查詢 -->
<select id="findStudentList4" resultType="Student">
<bind name="stu_name" value="'%'+stu_name+'%'" />
select * from student where stu_name like #{stu_name}
</select>
測試方法:
public class Test {
public static void main(String[] args) throws IOException {
String src = "config.xml";
InputStream inputStream = Resources.getResourceAsStream(src);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sessionFactory.openSession(true);
//綁定
StudentDao sd = sqlSession.getMapper(StudentDao.class);
//使用bind進(jìn)行模糊查詢
List<Student> list = sd.findStudentList4("拐");
System.out.println(list);
}
}
運(yùn)行結(jié)果:
總結(jié)一下:
當(dāng)在mybatis中遇到諸如此類傳遞參數(shù)出現(xiàn)問題的情況氛魁,多想想這里是否設(shè)置了鍵值對暮顺。