mybatis中当参数只有一个且是自定义对象类型时为什么也要使用@param注解

imiMi_ 2017-06-11 04:56:13
只有一个参数的时候反射机制不需要知道参数名就可以实现赋值了吧?为什么自定义的一个javabean类型作为单独参数却要使用@Param注解
...全文
2141 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
诺浅 2020-03-25
  • 打赏
  • 举报
回复
引用 18 楼 诺浅 的回复:
真可怜 csdn的回答办款真是垃圾 这么久没有一个人理楼主
回答板块
诺浅 2020-03-25
  • 打赏
  • 举报
回复
真可怜 csdn的回答办款真是垃圾 这么久没有一个人理楼主
  • 打赏
  • 举报
回复
已经看到了,之前我也碰到过,本来values我没有加bean的名字,我们其他同事偏偏添加,搞得最后五花八门的写法,真的很二。
编程写手 2019-10-17
  • 打赏
  • 举报
回复
一般mybatis的参数在两个或者以上的时候,都会用map集合,有需要的就用注解,不用的话就报错
  • 打赏
  • 举报
回复
最后怎么改的通过了?
ws199199 2018-05-17
  • 打赏
  • 举报
回复
怎么都是你在自言自语,怎么通过的啊
ws199199 2018-05-17
  • 打赏
  • 举报
回复
奥,看到了,不过楼上确定是对应的数据库属性而不是对象的属性名吗?
qq_38115987 2017-06-24
  • 打赏
  • 举报
回复
能告知么?我也很奇怪这个问题
imiMi_ 2017-06-18
  • 打赏
  • 举报
回复
测试通过了,是我写法有问题,还是对反射理解的不够透彻,非常感谢
qq_38301876 2017-06-17
  • 打赏
  • 举报
回复
按照楼主的写法,肯定错误,还有insert语句有问题。插入语句的values属性不需要加student.name,应该直接写成对应的数据库属性,可以改成如下: <insert id="insert" parameterType="com.hd.model.Student" > insert into t_student (stuid, name, sex, grade) values (#{stuid }, #{name }, #{sex }, #{grade }) </insert> 本人已测试通过,请楼主把values里面的student.去掉即可不需要加注释
imiMi_ 2017-06-17
  • 打赏
  • 举报
回复
如下代码所示,dao接口中insertStudent方法没有使用@Param注解: public interface StudentDao { int insertStudent(Student student); int delStudent(int id); int updateStudent(@Param("student") Student student); Student queryById(int id); List<Student> queryAll(@Param("offset") int offset,@Param("limit") int limit); } mapper文件中insertStudent方法的实现,并在parameterType中指定了Student的具体类型: <insert id="insertStudent" parameterType="org.student.entity.Student"> insert ignore into student (name,sex,grade) VALUES (#{student.name},#{student.sex},#{student.grade}); </insert> junit测试代码: @Test public void testInsertStudent() throws Exception { System.out.println(studentDao.insertStudent(new Student("李四",0,62))); } 测试报错: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'student' in 'class org.student.entity.Student'
imiMi_ 2017-06-17
  • 打赏
  • 举报
回复
这个试过了...去掉@Param注解 在mapper文件中定义parameteType为Student的具体包路径 然后junit测试仍然报上述错误。。。
xiaovhao 2017-06-16
  • 打赏
  • 举报
回复
是你没定义parameterType,单个的话,如果是基本类型的话,如果你不加@param的话,我记得默认是value,如果是String的话,默认的是_parameter
imiMi_ 2017-06-16
  • 打赏
  • 举报
回复
dao接口代码: import org.student.entity.Student; public interface StudentDao { int insertStudent(@Param("student")Student student); int delStudent(int id); int updateStudent(@Param("student") Student student); Student queryById(int id); List<Student> queryAll(@Param("offset") int offset,@Param("limit") int limit); } mapper实现代码: <mapper namespace="org.student.dao.StudentDao"> <insert id="insertStudent"> insert ignore into student (name,sex,grade) VALUES (#{student.name},#{student.sex},#{student.grade}); </insert> <delete id="delStudent"> delete from student where id=#{id}; </delete> <update id="updateStudent"> update student set name=#{student.name},sex=#{student.sex},grade=#{student.grade} where id=#{student.id}; </update> <select id="queryById" resultType="Student"> select id,name,sex,grade from student where id=#{id}; </select> <select id="queryAll" resultType="Student"> select id,name,sex,grade from student order by id limit #{offset},#{limit}; </select> insertStudent 方法的student参数不加Param注解junit测试就会报 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'student' in 'class org.student.entity.Student'
  • 打赏
  • 举报
回复
bean参数?lz能贴代码出来看看
imiMi_ 2017-06-13
  • 打赏
  • 举报
回复
bean对象作为单独参数时不加@param注解的话测试就报错,加了就对了。。很神奇
qq_24597963 2017-06-12
  • 打赏
  • 举报
回复
一个参数的话不需要吧
a_b_a_b_a_b_a_b 2017-06-12
  • 打赏
  • 举报
回复
一个参数不需要用@Param吧,在mybatis中定义parameterType就行吧。
qq_38624220 2017-06-12
  • 打赏
  • 举报
回复
多个参数才需要吧,单个的应该不需要的

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧