前提:

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:指定去掉多余的前缀内容

posted on 2018-08-14 16:55  帅过驴的袋鼠  阅读(126)  评论(0编辑  收藏  举报