关于Mybatis的mapper.xml文件中从pojo获取属性值的问题

在实际开发项目中,我们查询条件不可能很单一,查询字段可能包括很多字段,比如:查询条件可以有用户信息,商品信息,订单信息等。

这里我们的基础实体类是用户类,我在这个基础上扩展他,之后包装他作为我们的查询条件。

 
/**
 * 用户po类
 */
import java.util.Date;
 
public class User {
    private int id;
    private String username;
    private String sex;
    private Date birthday;
    private String address;
    //set...  get...
}
 
/**
 * User扩展对象
 * 用于继承user类,直接获取user对象的字段
 * 并添加一些扩展字段
 *
 */
public class UserCustom extends User{
    //可以添加一些user类中没有的字段
    //...
}

包装类:用于parameterType

public class UserQueryVo {
    private UserCustom userCustom;
    public UserCustom getUserCustom() {
        return userCustom;
    }
    public void setUserCustom(UserCustom userCustom) {
        this.userCustom = userCustom;
    }
}

UserMapper接口

public interface UserMapper {
    public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;
}

mapper配置文件编写sql:

 
    <select id="findUserList" parameterType="userQueryVo" resultType="userCustom">
        select * from user where username like '%${userCustom.username}%'
    </select>


未标题-2.jpg

上图中可看到在mapper.xml配置文件中用userCustom.username获取父类中的username私有成员变量,实际上是通过userCustom.getUsername()方法获得的.

说明:mybatis底层通过ognl从pojo中获取属性值:#{user.username},user即是传入的包装对象的属性。queryVo是别名,即上边定义的包装对象类型。

以下文章通过一个简单的例子来看看ognl的用法

Ognl访问对象属性

另还有一篇文章展示了mybatis中的ognl教程

MyBatis中的OGNL教程

通过以上的了解,现在把User类中的getUsername方法注释掉

public class User implements Serializable{
    private int id;
    private String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    
    private List<Orders> ordersList;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
//  public String getUsername() {
//      return username;
//  }
    public void setUsername(String username) {
        this.username = username;
    }

再次运行程序,得到以下报错:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'userCustom.username!=null and userCustom.username!='''. Cause: org.apache.ibatis.ognl.NoSuchPropertyException: cn.itcast.mybatis.po.UserCustom.username
### Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'userCustom.username!=null and userCustom.username!='''. Cause: org.apache.ibatis.ognl.NoSuchPropertyException: cn.itcast.mybatis.po.UserCustom.username
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    at com.sun.proxy.$Proxy3.findUserList(Unknown Source)
    at cn.itcast.mybatis.mapper.UserMapperTest.testFindUserList(UserMapperTest.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

接着,我们仍旧保留对getUsername方法的注释,但是现在把username设置为public

public class User implements Serializable{
    private int id;
    public String username;// 用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    
    private List<Orders> ordersList;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
//  public String getUsername() {
//      return username;
//  }
    public void setUsername(String username) {
        this.username = username;
    }

再次运行测试程序,得到以下正常的结果显示


image.png

以上就证明了在mapper.xml中,userCustom.username实际上是通过调用了userCustom.getUsername()方法获得了user中的username属性,而不是userCustom直接访问父类User中的private修饰的username成员变量。

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

相关阅读更多精彩内容

友情链接更多精彩内容