• Mybatis映射实体改造和异常问题


    现在WEB开发经常使用 Mybatis 作为持久化框架,在开发过程中,会在Java代码中构建实体类与数据库表字段相互映射,

    下面提出一个关于映射实体优化的方案:通过链式编程实现给实例对象赋值。

    参考代码:

    public class UserEntity{
        private int userId;
        private String userName;
        private long lastLogin;
        
        public int getUserId() {
            return userId;
        }
    
        public UserEntity setUserId(int userId) {
            this.userId = userId;
            return this;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public UserEntity setUserName(String userName) {
            this.userName = userName;
            return this;
        }
    
        public long getLastLogin() {
            return lastLogin;
        }
    
        public UserEntity setLastLogin(long lastLogin) {
            this.lastLogin = lastLogin;
            return this;
        }
    }
    

      

    通过返回 this ,实现链式编程,但是返回 this 以后,Mybatis持久化框架给属性赋值的时候会不会出现问题,为了确认这个问题,查看了Mybatis的源码

    Mybaits是通过反射给实体对象赋值的,在主要是调用的 Invoker 接口的相关实现类。

    Invoker 接口定义:

    public interface Invoker {
    Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException; Class<?> getType(); }

    在Mybatis中,通过Invoker的实现类 SetFieldInvoker 给实体赋值,通过 GetFieldInvoker 获取实体的值。

    SetFieldInvoker 类定义:

    public class SetFieldInvoker implements Invoker {
      private Field field;
    
      public SetFieldInvoker(Field field) {
        this.field = field;
      }
    
      public Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException {
        field.set(target, args[0]);
        return null;
      }
    
      public Class<?> getType() {
        return field.getType();
      }
    }
    

    GetFieldInvoker 类定义:

    public class GetFieldInvoker implements Invoker {
      private Field field;
    
      public GetFieldInvoker(Field field) {
        this.field = field;
      }
    
      public Object invoke(Object target, Object[] args) throws IllegalAccessException, InvocationTargetException {
        return field.get(target);
      }
    
      public Class<?> getType() {
        return field.getType();
      }
    }
    

     

    二、Mybatis 异常情况

    mapper.xml 定义:

    <mapper namespace="xxx.xxx.xxx.UserMapper">
        <sql id="user_field">
            user_id,username,last_login
        </sql>
    
        <resultMap id="user" type="xxx.xxx.xxx.User">
            <id property="userId" column="user_id"/>
            <result property="userName" column="username"/>
            <result property="" column="last_login"/>
        </resultMap>
    <update id="updateLastLogin" parameterType="xxx.xxx.xxx.User">
    UPDATE user SET last_login = #{last_login} WHERE user_id = #{user_id}
    </update>
    </mapper> 

     当调用updateUser方法的时候,程序报错:There is no getter for property named ''last_login" in 'class xxx.xxx.xxx.User'

       报这个错的原因是因为我们在#{} 里面应该填写User的属性,而不是列名,我们将 Update语句改为以下形式就正确了:

    <update id="updateLastLogin" parameterType="xxx.xxx.xxx.User">
        UPDATE user SET lastLogin = #{last_login} WHERE user_id = #{userId}
    </update>
    

      

  • 相关阅读:
    Python关键点常识
    sublime text 3创建新文件插件-AdvanceNewFile
    sublime 插件安装packagecontrol
    消息队列软件产品大比拼
    http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html
    Error: opening registry key 'SoftwareJavaSoftJava Runtime Environment' Error: could not find java.dll
    xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
    http://ftp.gnu.org/gnu/ http://ftp.gnu.org/gnu/libc/
    http://mirror.centos.org/centos/7.2.1511/os/x86_64/Packages/, 开源软件清单list
    ftp://fr2.rpmfind.net/linux/centos/7.2.1511/os/x86_64, 开源软件清单list
  • 原文地址:https://www.cnblogs.com/googlemeoften/p/7199266.html
Copyright © 2020-2023  润新知