There is no getter for property named 'xxx' in class 'aaa. BBB. CCC '(Solution to the ultimate cool Operation)

Source: Internet
Author: User

When springboot was used to integrate mybatis, an exception like the title was encountered. The blogger used Bean class objects as parameters, So Baidu had no constructive answer for a long time. Paste the error file xxxdao and xxxmapper. xml:

package com.cjs.dao;import com.cjs.bean.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.*;@Mapperpublic interface UserDao {    public int insertUser(User user);}

 

<?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.cjs.dao.UserDao">    <insert id="insertUser" parameterType="user">        insert into tb_users(username, loginname, password, phone, address)        values (#{user.username},#{user.loginname}, #{user.password}, #{user.phone}, #{user.address})    </insert></mapper>

 

 

 

Hmm ~ According to the general SSM operation, the above Code will not be abnormal, but actually it will report the nested exception is Org. apache. ibatis. reflection. reflectionexception. The complete title statement is there is no getter for property named 'user' in class 'com. CJS. bean. user ', which means that the user object does not have the user's getter method. What ??? ( .Jpg) the user is an object. How can I get a getter method ?! As a result, the blogger wrote a simple method. The parameter is of the string type. According to the good coding habits, add the parameter in the DAO method@ Param("Username"). Normally, a piece of data is inserted perfectly, so you can add it in the original method.@ Param ("user "), Run, yo ~ The insert operation is successful. This is a magic operation.

The modified Dao file code:

package com.cjs.dao;import com.cjs.bean.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.*;@Mapperpublic interface UserDao {    public int insertUser(@Param("user") User user);}

It is actually a simple operation. Let's analyze it now and add@ Param ("user ")In xxxmapper. the XML file can accurately find this parameter, parametertype = "user". Here, the user is the type, indicating that the input parameter type is user, which is actually a user object, here, the user alias is set to user; # {user. username} Here, the user is the parameter name, and the problem lies here (hit the blackboard). Integrate mybatis in springboot. If you do not set@ Param ("user "), It will put # {user. the user in username} is treated as a property value of the parameter you passed in, that is, the user. user. username, the user object naturally has no user attribute. On the contrary, I indicated # {user. the user in username} is the parameter I passed in, indicating@ Param ("user "). This solution can be promoted to the case where the parameter is not a bean object.

According to the analysis just now, if # {user. Username} is used, add@ Param ("user ")To mark the USER parameter. In other words, if the parameter is not added, the user in # {user. Username} is treated as an attribute in the parameter (note that there is an underline in the field ).Method 2:Replace # {user. Username} with # {username}

package com.cjs.dao;import com.cjs.bean.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.*;@Mapperpublic interface UserDao {    public int insertUser(User user);}
<?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.cjs.dao.UserDao">    <insert id="insertUser" parameterType="user">        insert into tb_users(username, loginname, password, phone, address)        values (#{username},#{loginname}, #{password}, #{phone}, #{address})    </insert></mapper>

 

There is no getter for property named 'xxx' in class 'aaa. BBB. CCC '(Solution to the ultimate cool Operation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.