• mybatis之动态sql


    前提:

    ssm框架已经搭建成功,能够正常进行增删改查操作。

    下面来看UserMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.six.dao.UserDao">
        <resultMap id="userMapper" type="com.six.pojo.User">
            <id column="id" property="userId"/>
            <result column="user_name" property="userName"/>
            <result column="user_age" property="userAge"/>
            <result column="user_sex" property="userSex"/>
        </resultMap>
        <insert id="addOneUser" parameterType="com.six.pojo.User">
            INSERT INTO t_user
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="userName != null and userName != ''">
                    user_name,
                </if>
                <if test="userAge != null and userAge != ''">
                    user_age,
                </if>
                <if test="userSex != null and userSex != ''">
                    user_sex,
                </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="userName != null and userName != ''">
                    #{userName},
                </if>
                <if test="userAge != null and userAge != ''">
                    #{userAge},
                </if>
                <if test="userSex != null and userSex != ''">
                    #{userSex},
                </if>
            </trim>
        </insert>
    </mapper>

    prefix:在trim标签内sql语句上加上前缀

    suffix:在trim标签内sql语句上加上后缀

    suffixOverrides:指定去除多余的后缀内容,如 suffixOverrides="," 表示去掉 trim 标签内sql语句的多余后缀 ","

    prefixOverrides:指定去掉多余的前缀内容

  • 相关阅读:
    内核并发管理---spin lock
    redhat samba安装配置
    内核系统调用接口分析
    kernel makefile
    你知道C#中的Lambda表达式的演化过程吗?
    农码一生博文索引
    那些年搞不懂的术语、概念:协变、逆变、不变体
    Asp.Net生命周期
    ASP.NET配置文件Web.config 详细解释
    加盐密码哈希:如何正确使用
  • 原文地址:https://www.cnblogs.com/zhanzhuang/p/9475672.html
Copyright © 2020-2023  润新知