mybatis-bug:There is no getter for property named 'stu_name' in 'class java.lang.String'

异常代码:

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实现模糊查询 -->
    <select id="findStudentList4" resultType="Student">
        <bind name="stu_name" value="'%'+stu_name+'%'" />
        select * from student where stu_name like #{stu_name}
    </select>

原因:

根据报错内容,也能看出String类型的参数中没有getter方法,按自己的理解就是这个参数传过来没有用键值对的方式来传递,即这个值没有相应的键来表示它,所以要给值设定一个键。

解决:

解决方案也不少,下面说两个吧,一个是自己写的,不过违背了最初想要传递String的初衷;一个是网上搜的,通过注解,推荐第二种。

  1. 将要传递的String值封装到一个map中,传递map
具体代码实现如下:

接口StudentDao.java的代码:
可以看到,代码改动在第五个方法那里,将传递的参数改为了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实现模糊查询 -->
    <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);
    }
}

运行结果:

运行结果


  1. 通过注解,将接口中传递的String类型前加一个@Param("stu_name")
具体代码实现如下:

接口StudentDao.java的代码:
可以看到,代码改动在第五个方法那里,配置了一个注解,相当于给String的参数加了一个键。

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实现模糊查询 -->
    <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进行模糊查询
        List<Student> list = sd.findStudentList4("拐");
        System.out.println(list);
    }
}

运行结果:

运行结果


总结一下:

当在mybatis中遇到诸如此类传递参数出现问题的情况,多想想这里是否设置了键值对。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,777评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,282评论 19 139
  • 前两天在Palo Alto T4买奶茶,等待的时候听到周杰伦的《我的地盘》,那一刻之于我自己是震撼的,扫视周围的十...
    小橘灯阅读 3,325评论 0 0
  • 1 直到现在,我才开始慢慢明白,为什么无数剪不断理还乱的缘分,却又悄无声息地无疾而终。有人告诉我,分离的人终会相遇...
    杏笔阅读 3,308评论 0 3
  • 凡事皆有第一次,没有经验,难免会吃亏上当,这也是写下文字分享的好处。 第一次租房,是初来广州打工,什么也不懂,加上...
    小飞鱼20123阅读 2,563评论 0 0

友情链接更多精彩内容