zoukankan      html  css  js  c++  java
  • Mybatis中实体类属性和数据列之间映射的四种办法

    Mybatis不像Hibernate中那么自动化,通过@Column注解或者直接使用实体类的属性名作为数据列名,而是需要自己指定实体类属性和 
    数据表中列名之间的映射关系,这一点让用惯了Hibernate的人很不习惯,所幸经过探索找到了建立映射关系的三种办法,其中总也有比较 
    简单的。
    
    首先先定义一个实体类,如下:
    
    public class User implements Serializable {
        private Integer userId;
        private String userName;
        ......
    }
    
    1. 通过XML映射文件中的resultMap
    这种方式是最常见的,类似如下:
    
    <mapper namespace="data.UserMapper">
        <resultMap type="data.User" id="userResultMap">
            <!-- 用id属性来映射主键字段 -->
            <id property="id" column="user_id"/>
            <!-- 用result属性来映射非主键字段 -->
            <result property="userName" column="user_name"/>
        </resultMap>
    </mapper>
    
    通过里面的id标签和result标签来建立映射关系,由property和column分别指定实体类属性和数据表的列名。
    
    2. 通过注解@Results和@Result
    这两个注解是与XML文件中的标签相对应的:
    
    @Results对应resultMap
    @Result对应result
    这两个注解是应用在方法的级别上的,也就是在mapper方法上,如下:
    
    @Select("select * from t_user where user_name = #{userName}")
    @Results(
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name")
    )
    User getUserByName(@Param("userName") String userName);
    
    缺点:
    
    由于注解是针对方法的,对于Mapper中的每个操作数据库的方法都必须有相同的注解完成映射关系的建立,导致很多的配置是重复的;
    如果要避免配置重复的问题,可以采用在XML配置文件中配置这个resultMap,然后再@Result中通过id属性引用这个resultMap, 
    但是这样感觉很麻烦(由于使用了两种配置方式),不如直接使用基于XML的resultMap配置方式;
    3. 通过属性配置完成映射
    使用者最陌生的是通过配置属性来完成映射,Mybatis给我们提供了一种映射方式,如果属性的命名是遵从驼峰命名法的,数据列名遵从下划线命名, 
    那么可以使用这种方式,类似如下:
    
    userName对应user_name;
    userId对应user_id;
    配置代码如下:
    
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    Configuration configuration = new Configuration();
    configuration.setMapUnderscoreToCamelCase(true);
    sqlSessionFactoryBean.setConfiguration(configuration);
    
    4. 通过使用在SQL语句中定义别名完成映射
    这种方式最直接,直接在SQL语句中建立别名来完成映射,如下:
    
    @Select("select user_name as userName, user_id as userId from t_user where user_name = #{userName}")
    User getUserByName(@Param("userName") String userName);


    Restful 接口调用Json接收相关问题
    1、背景:

    在项目上使用SpringBoot为框架,调用第三方接口时,返回的参数类型,不符合标准的命名规则,需要进行处理,接受数据

    2、现象:

    调用第三方接口返回数据格式为方式均为小写,如下:

    {
               "rowid": "111111",
               "created": "2018-12-27 16:15:25",
               "createdby": "1111111",
               "lastupd": "2018-12-27 08:25:48",
               "lastupdby": "111111",
               "modificationnum": 1
            }
    返回Json参数字段均为小写,在接收时,需要按照标准的命名规则进行映射

    3、解决办法:

    创建接收数据对象,生成GetSet方法:,在Set方法上,加上@JsonProperty注解,

    @JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把rowId属性序列化为rowid,@JsonProperty("rowid")。

       private String rowId;
       private Date created;
       private String createdBy;
       private Date lastUpd;
    private String lastUpdBy;

    @JsonProperty("rowId")
       public String getRowId() {
           return rowId;
      }

       @JsonProperty("rowid")
       public void setRowId(String rowId) {
           this.rowId = rowId;
      }

       public Date getCreated() {
           return created;
      }
       @JsonDeserialize(using = CustomJsonDateDeserializer.class)
       public void setCreated(Date created) {
           this.created = created;
      }

       @JsonProperty("createdBy")
       public String getCreatedBy() {
           return createdBy;
      }

       @JsonProperty("createdby")
       public void setCreatedBy(String createdBy) {
           this.createdBy = createdBy;
      }

  • 相关阅读:
    form表单中有bootstrap-switch时怎么提交表单
    Django:drf过滤、搜索、排序功能
    pycharm:使用豆瓣源安装第三方库
    pycharm安装包时:Non-zero exit code (1)
    js: 前端通过ajax方式获取后台数据填充下拉列表
    Django:drf框架中序列化组件—字段验证
    ROS Reality: A Virtual Reality Framework Using Consumer-Grade Hardware for ROS-Enabled Robots David
    Virtual and Augmented Reality for Robotics in Unity environment using Mimic plugin
    Human-machine collaboration in virtual reality for adaptive production engineering
    An open modular framework for efficient and interactive simulation and analysis of realistic human motions for professional applications
  • 原文地址:https://www.cnblogs.com/xianz666/p/13297191.html
Copyright © 2011-2022 走看看