Mybatis dynamic sql splicing and the use of log4j

Due to select only accept an additional parameter, so the package needs to pass, using a dynamic mosaic ognl expressions,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 <select id="queryData" parameter="com.imooc.bean.Message" resultMap="">
 	select ID,COMMAND from MESSAGE where 1=1
    <!--
    这里&quot;是转义的上引号  &不能在这里用所以用ognl自带的操作符and 还有or 、in、 not in
    ognl可以直接获取对象中的属性值,这里的command是从Message中获得,还能调用对象属性
    -->
    <if test="command!=null and !&quit;&quot;.equals(command.trim())">
    <!--这里下面的语法并不是ognl表达式,是mybatis自己的-->
    and COMMAND=#{command}
    </if>
    <if test="description!=null and !&quit;&quot;.equals(command.trim())">
    and description like '%' #{description} '%'
    </if>
 </select>

## log4j debugging using dynamic SQL referencing the appropriate jar (log4j -. * Jar) and configuration.xml output is divided into several levels

log.debug() log.info() log.warn() log.error()

Log4j.rootLogger disposed in the log4j.properties = DEBUG, Console is output if all the above configuration, the output level is greater than the configuration information, if it is only with the ERROR output log.error ()

Console which is just a variable Natori, you can change, but you can not change the previous

1
2
3
4
5
6
7
8
log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
//定义输出布局
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
//定义输出格式 %d日期 %t线程 -5表示输出至少为5为不足则补齐 %p输出日志级别 %c输出日志是所属类全名  %m输出时附加的信息 %n换行
log4j.appender.Console.layout.ConversionPattern=%d[%t]%-5p[%c]-%m%n
//org.apache是自己写的包名,个性化 第一行是整个工程的
log4j.logger.org.apache=INFO

mybatis itself defines an excuse to call log tools, what kind of jar import log on with source code under org.apache.ibatis.logging.

MyBatis configuration file tags

MyBatis中的标签

foreach tag

1
2
3
4
5
6
7
8
9
10
```xml
<delete id="deleteBatch" parameterType="java.util.List">
    delete from message where id in(
        <!--用separator让mybatis来拼接,值为所需分隔符,这里index没有使用单纯只是做笔记-->
        <foreach collection="list"  index="i" item="item" separator=",">
            #{item}
        </foreach>
    )
</delete>
```

## where the label

1
2
3
4
5
6
7
8
  <select id="queryData" parameter="com.imooc.bean.Message" resultMap="">
 	select ID,COMMAND from MESSAGE 
    <where>
    <if test="command!=null and !&quit;&quot;.equals(command.trim())">
    and COMMAND=#{command}
    </if>
   </where>
 </select>

## sql tag

Sql reference segment may be repeated.

1
2
3
4
5
6
7
8
9
10
 <select id="queryData" parameter="com.imooc.bean.Message" resultMap="">
 	select <include refid="colums"/> from MESSAGE 
    <where>
    <if test="command!=null and !&quit;&quot;.equals(command.trim())">
    and COMMAND=#{command}
    </if>
   </where>
 </select>
 
 <sql id="colums"> ID,COMMADN</sql>

## set to solve major label separator splicing

1
2
3
4
5
6
7
8
   <update id="">
    update message 
    	<set>
        	<if test="command!=null and !&quit;&quot;.equals(command.trim())">
    and COMMAND=#{command}
    </if>
        </set>
   </update>

## trim标签 可以代替where和set

1
2
3
   <!--prefix 如果内有内容加入where  suffix如果有内容输出则加上  如果有会在最后切掉prefixOverrides="and/or"  suffixOverrides=","-->
   <trim prefix="where">
   </trim>

## choose标签

类似java的switch case

1
2
3
4
5
6
7
8
9
   <choose>
        <when test="">
        </when>
        <when test="">
        </when>
        <when test="">
        </when>
    	<otherwise></otherwise>
   </choose>

## association用于一对一映射

1
   <association property="" resultMap="sss.resultid"/>

原文:大专栏  Mybatis动态sql拼接以及使用log4j


Guess you like

Origin www.cnblogs.com/petewell/p/11607278.html