美文网首页
mybatis-bug:There is no getter f

mybatis-bug:There is no getter f

作者: 09c72470861c | 来源:发表于2018-07-25 23:06 被阅读0次

    异常代码:

    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中遇到诸如此类传递参数出现问题的情况,多想想这里是否设置了键值对。

    相关文章

      网友评论

          本文标题:mybatis-bug:There is no getter f

          本文链接:https://www.haomeiwen.com/subject/smnmmftx.html