zoukankan      html  css  js  c++  java
  • MyBatis起别名、resultMap、keyProperty

    实体起别名

    1.给实体类的权限定名取别名  (mybatis-config配置文件)
        com.lhl.demo1.Person        别名="Person"
    
        <typeAliase>
            <typeAlias type="实体的全类名" alia="别名"></typeAlias>
         </typeAliase>
    
    2.Mapper使用实体类名
        resultType="Person"

    MyBatis参数绑定的底层原理

    1.默认mapper文件绑定  #{xxx}
        底层使用的PreparedStatement对象
        使用的是SQL的?站位绑定,预编译
        优点:
            预编译,防止sql注入、相对效率高一点
        缺点:
            只能绑定数据值   sql关键字、列、非数据无法绑定
    
    2.使用sql字符串拼接绑定参数  ${xxx}
        优点:可以绑定任何内容
        缺点:sql注入

     将对象插入数据库后,绑定id

    目的:
        将insert插入的user对象,id赋值添加的id
    思路:
        1.发送select 序列 from dual的sql
        2.通过结果集获得id
        3.将id绑定在参数的urer对象的id属性中
    
    MyBatis插入数据绑定id
        <insert id="insert" parameterType="实体类型">
            <!--在insert语句之前,执行select序列的sql,为了给参数的id属性绑定值-->
            <selectKey order="BEFORE" resultType="java.lang.String" keyProperty="sql的执行结果绑定给参数的实体的那个属性  id">
              select seq_user.nextval from dual
         </selectKey> insert into t_user values(?,?,?) </insert>

     查询关系映射

    一、resultType
    
    二、使用resultMap定义映射关系
        <!--
            1.列   属性
            2.最终封装的对象类型
        -->
        <resultMap id="PersonMap"  type="实体对象">
             <!--id:主属性-->
            <id property="属性"  column="列"></id>
            <!--其他列属性-->
            <result property="属性" column="列"/>
            <result property="name" column="name"*>
        </resultMap>
    
    <!--查询功能-->
        <select id="selectById" parameterType="java.lang.Integer" resultMap="PersonMap">
        </select>
  • 相关阅读:
    Python 在线基础课程
    单链表实现两个集合的交并补
    矩阵奇异值分解SVD
    动态规划0-1背包问题
    Java数字反转
    简单SqL语句
    HTML和CSS
    while和for循环
    C#读取和写入配置文件
    C# 添加系统计划任务方案
  • 原文地址:https://www.cnblogs.com/lhl0131/p/13470571.html
Copyright © 2011-2022 走看看