zoukankan      html  css  js  c++  java
  • mybatis 常见面试题

    1、xml映射文件中#{}与${}区别

    #{}是sql的参数占位符,Mybatis会将sql中的#{}替换为?号,在sql执行前会使用PreparedStatement的参数设置方法,按序给sql的?号占位符设置参数值,比如ps.setInt(0, parameterValue),#{item.name}的取值方式为使用反射从参数对象中获取item对象的name属性值,相当于param.getItem().getName()。

    ${}用于sql,具有静态文本替换功能,常用在${table}, order by ${orderField},group by ${groupField}

    附:${}还用于,是Properties文件中的变量占位符,它可以用于标签属性值,属于静态文本替换,比如${driver}会被静态替换为com.mysql.jdbc.Driver。

    01.Map<String, Object> parms = new HashMap<String, Object>();  
    02.parms.put("table", "foo"); // 表名  
    03.parms.put("criteria", 37); // 查询过滤条件  
    04.List<Object> rows = mapper.generalSelect(parms);  


    01.<select id="generalSelect" parameterType="map">  
    02.  select * from ${table} where col1 = #{criteria}  
    03.</select>  


    MyBatis生成的SQL语句(prepared statement)如下所示:
    01.select * from foo where col1 = ?  

    参考:

    https://www.cnblogs.com/huajiezh/p/6415388.html

    http://www.cnblogs.com/huajiezh/p/6415322.html

    2、mybatis 数据表主键自增长,默认执行insert mapperStatement后,不能获取自增长的主键;insert中设置useGeneratedKeys="true" keyProperty="id"后就可以,获取主键,

    keyProperty="数据表主键对应的对象属性名"

    <insert id="add" useGeneratedKeys="true" keyProperty="id" parameterType="DoubleColorBalls">
    insert into double_color_ball_result (period_serial, red, red2, red3, red4, red5, red6, blue) values (
    #{period_serial}, #{red1}, #{red2}, #{red3}, #{red4}, #{red5}, #{red6}, #{blue})
    </insert>

  • 相关阅读:
    springboot(十)使用LogBack作为日志组件
    springboot(九)文件上传
    django 安装
    macbook使用“终端”远程登录linux主机
    Mac 怎么通过自带终端连接linux服务器
    什么是变量?
    选择最好用的PyCharm IDE
    开发你的第一个Python程序
    Python介绍
    PyCharm 2017 安装教程
  • 原文地址:https://www.cnblogs.com/hblthink/p/8838667.html
Copyright © 2011-2022 走看看