• mybatis 保存对象 参数类型


    简单介绍:保存单个对象 ,参数类型的设置,正常的话应该设置成对应的pojo,我想起了以前,不懂事时候的一个做法,其实那时候刚接触到mabatis,做新增的时候,直接就是把需要插入表中的值,放到map里,结果写的是map类型,下边会给大家看,先展示现在的做法。

    代码:

    //class
    @Alias("Contract")
    @Getter
    @Setter
    public class OhOperContract {
    private String contractId;
    private String contractNumber;
    private String contractType;
    private String startTime;
    private String endTime;
    }
    //service层
    public void saveContract(Contract contract)throws Exception{
      dao.save("ContractMapper.saveContract",contract);
    }
    //mapper
    <resultMap id="ContractResultMap" type="Contract">
    <result column="contract_id" property="contractId"/>
    <result column="contract_number" property="contractNumber"/>
    <result column="contract_type" property="contractType"/>
    <result column="start_time" property="startTime"/>
    <result column="end_time" property="endTime"/>
    </resultMap> 

    //sql语句
    <insert id="saveContract" parameterType="Contract">
    insert into <include refid="tableName"></include>
    (
    <include refid="FieldContract"></include>
    ) values (
    <include refid="FieldValue"></include>
    )
    </insert> 

    <!--表名 -->
    <sql id="tableName">
    t_contract
    </sql> 
    <!-- 字段 -->
    <sql id="FieldContract">
    contract_id,
    contract_number,
    contract_type,
    start_time,
    end_time
    </sql>
    
    
    <!-- 字段值 -->
    <sql id="FieldValue">
    #{contractId},
    #{contractNumber},
    #{contractType},
    #{startTime},
    #{endTime}
    </sql> 

     以前的做法(捂脸.jpg),其实这样做相当于直接跨过了pojo 直接往表中插入数据:

  • 相关阅读:
    PHP -----上传文件
    PHP----预定义数组
    PHP-------- 会话控制
    PHP------XML
    PHP----练习-----三级联动
    PHP-------ajax返回值 返回JSON 数据
    PHP----练习----光标离开文本框时变色
    使用ansible安装配置zabbix客户端
    svn+apache安装配置
    rsync+lsyncd实现实时同步
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/9934042.html
Copyright © 2020-2023  润新知