zoukankan      html  css  js  c++  java
  • (办公)mybatis工作中常见的问题(不定时更新)

           1.mybatis的like查询的方式.

    <if test="shopName != null and shopName != ''">
                    <bind name="shopName01" value="'%'+shopName+'%'" />
                    AND s.shop_name  LIKE  #{shopName01}
                </if>

           2.mybatis的批量插入,批量修改,批量删除

    <insert id="insertBatch">
        INSERT INTO t_user (id, name, del_flag) VALUES <foreach collection ="list" item="user" separator =",">
             (#{user.id}, #{user.name}, #{user.delFlag}) </foreach >
    </insert>

            特别注意:mysql默认接受sql的大小是1048576(1M).

    <deleteid="deleteBatch">
        delete from table_name where id in 
      <foreach collection ="list" item="item" separator =",">
             #{item}
        </foreach >
    </delete>

           3.父子表,查询都查出来.

      

    1.主表添加子表属性
    2.主表xml查询left join子表
    3.xml查询返回的主表resultmap配置collection配置子表

        子表配置主表association属性,关联查询一下即可.

           3.获取主键

    keyProperty="id" useGeneratedKeys="true"

          4.连接mysql数据库注意编码问题

    jdbc:mysql://127.0.0.1:3306/lanhuprivi?useUnicode=true&characterEncoding=UTF-8

          5.传递的参数不多的话,可以不用类,用@Param注解

    List<SysRole> selectRo lesByUseridAndRoleEnabled( 
    @Pa ram (”user Id”) Long user Id, 
    @Pa ram (” enable d ”) Integer enabled) ; 

         6.动态拼接sql的常用标签:if, choose(类似sql里的 case when/就是java里的if else)  where(标签的作用:如果该标签包含的元素中有返回值,就插入 where ;如果 where
    后面 字符串是以 AND和OR 开头的,就将它们剔除。) ,set, trim, foreach, bind(bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中。)

    trim的属性
    prefix :当 trim 元素内包含内容时,会给内容增加 prefix 指定的前缀。
    prefixOverrides :当 trim 元素内包含内容时,会把内容中匹配的前缀字符串去掉。
    suffix :当 trim 元素内包含内容时,会给内容增加 suffix 指定的后缀。
    suffixOverrides :当 trim 元素内包含内容时,会把内容中匹配的后缀字符串去
    </if> 
    <if test=” userEmail != null and userEmail !=”” >
    and user email = #{userEmail} 
    〈/工 f>
    <choose> 
    <when test=” id != nul l ” >
    and id= #{id} 
    </when> 
    <when test=” u serName != null and userName !=””>
    and user name = #{userName} 
    </when> 
    <otherwise> 
    and 1 = 2 
    </otherwise> 
    </ choose> 
    <where> 
    <if test=”userName != null and userName !=””>
    and user name like concat .....
    </ if> 
    <if test=” userEmail != ” and userEmail != nul l ”>
    and user email = #{userEmail} 
    </ if> 
    </where> 
    foreach 
    collection 必填,值为要选代循环的属性名。这个属性值的情况有很多。
    item :变量名,值为从法代对象中取出的每一个值。
    index :索引的属性名,在集合数组情况下值为当前索引值 当选代循环的对象是 Map
    类型时,这个值为 Map key (键值)。
    ope 口:整个循环内容开头的字符串
    close 整个循环内容结尾的字符串。
    separator :每次循环的分隔符
    <foreach collection=” lis t ” i tern=” user” separator=”,”>
    (
    #{user.userName}, #{user . userPassword},#{user . userEmail} , 
    #{user.userlnfo} , #{user.headlmg, jdbcType=BLOB} , 
    #{user.createTime , jdbcType=TIMESTAMP}) 
    </foreach> 
    <if test="shopName != null and shopName != ''">
                    <bind name="shopName01" value="'%'+shopName+'%'" />
                    AND s.shop_name  LIKE  #{shopName01}
                </if>

         7.多数据库的支持, databaseld ,就是语句也写多个.

    <select id=” selectByUser” database Id=” mysql ” 
    result Type=” tk .mybatis . simple.model.SysUser” >
    select* from sys_userand 
    </select> 
    <se lect id=” selectByUser” database Id=” oracle ” 
    result Type=” t k.mybatis. simple.model.SysUser” >
    select * from sys userand 
    </select> 

          8.mybatis的ognl表达式

    MyBatis常用OGNL表达式
    
        e1 or e2
        e1 and e2
        e1 == e2,e1 eq e2
        e1 != e2,e1 neq e2
        e1 lt e2:小于
        e1 lte e2:小于等于,其他gt(大于),gte(大于等于)
        e1 in e2
        e1 not in e2
        e1 + e2,e1 * e2,e1/e2,e1 - e2,e1%e2
        !e,not e:非,求反
        e.method(args)调用对象方法
        e.property对象属性值
        e1[ e2 ]按索引取值,List,数组和Map
        @class@method(args)调用类的静态方法
        @class@field调用类的静态字段值

           9.mybatis的select里面有注释,也会解析,报错.(工作中发现的错误)

           10.org.apache.ibatis.reflection.ReflectionException

    传入的值,没有get,set,接口上加上这个注解@Param

           11.org.mybatis.spring.MyBatisSystemException

    传入多个值,没有get,set,接口上加上这个注解@Param
  • 相关阅读:
    大二下每周总结
    大二下学期之阅读笔记(人月神话)
    大二下学期之阅读笔记(人月神话)
    大二下学期第一次结对作业(第一阶段:地图下钻)
    大二下学期第一次结对作业(第一阶段)
    java 多线程编程之: synchronized
    书籍
    elasticsearch size 设置最大返回条数
    Java 设计模式--策略模式,枚举+工厂方法实现
    Elasticsearch rollover API
  • 原文地址:https://www.cnblogs.com/historylyt/p/10516640.html
Copyright © 2011-2022 走看看