Mybatis之解决属性名和字段名不一致的问题(Mybatis solves the problem of inconsistency between attribute name and field name)

Mybatis

5、解决属性名和字段名不一致的问题

5.1、问题提出

  • 数据库中的字段

    user表 id name pwd

  • user表 id name pwd

  • 新建一个项目,拷贝之前的,测试实体类字段不一致的情况

    //实体类
    public class User {
    private int id;
    private String name;
    private String password;//不一致
    ……
    }

    测试出现问题

    User{id=2, name=’6′, password=’null’}

    //select * from user where id= #{id};
    //类型处理器
    //select id,name,pwd from user where id= #{id};

  • //实体类
    public class User {
    private int id;
    private String name;
    private String password;//不一致
    ……
    }

  • 测试出现问题

    User{id=2, name=’6′, password=’null’}

    //select * from user where id= #{id};
    //类型处理器
    //select id,name,pwd from user where id= #{id};

  • User{id=2, name=’6′, password=’null’}

  • //select * from user where id= #{id};
    //类型处理器
    //select id,name,pwd from user where id= #{id};

  • 解决方法:

    起别名

  • 起别名

5.2、resultMap

  • 结果集映射

    id name pwd
    id name password








  • id name pwd
    id name password








  • resultMap 元素是MyBatis中最重要最强大的元素
  • ResultMap的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了
  • ResultMap 最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显式地用到他们
————————

Mybatis

5. Solve the problem of inconsistency between attribute name and field name

5.1. Problem raising

  • 数据库中的字段

    user表 id name pwd

  • user表 id name pwd

  • 新建一个项目,拷贝之前的,测试实体类字段不一致的情况

    //实体类
    public class User {
    private int id;
    private String name;
    private String password;//不一致
    ……
    }

    测试出现问题

    User{id=2, name=’6′, password=’null’}

    //select * from user where id= #{id};
    //类型处理器
    //select id,name,pwd from user where id= #{id};

  • //实体类
    public class User {
    private int id;
    private String name;
    private String password;//不一致
    ……
    }

  • 测试出现问题

    User{id=2, name=’6′, password=’null’}

    //select * from user where id= #{id};
    //类型处理器
    //select id,name,pwd from user where id= #{id};

  • User{id=2, name=’6′, password=’null’}

  • //select * from user where id= #{id};
    //类型处理器
    //select id,name,pwd from user where id= #{id};

  • 解决方法:

    起别名

  • 起别名

5.2、resultMap

  • 结果集映射

    id name pwd
    id name password








  • id name pwd
    id name password








  • resultMap 元素是MyBatis中最重要最强大的元素
  • The design idea of resultmap is that there is no need to configure explicit result mapping for simple statements, but only need to describe their relationship for more complex statements
  • The best thing about resultmap is that although you already know it well, you don’t need to explicitly use them at all