Bootstrap

Mybatis单个和批量(查询、增加、更新、删除)的xml格式

一、单个语句

1、查询

(1)mapper

    // 根据用户名查询用户信息
    User findByName(String name);
    // 根据ID查询用户信息
    User findById(Integer id);
    // 根据用户名查询用户信息
    List<User> getUserInfoByName(String name);

(2)xml

<!--根据用户名查询用户信息-->
    <select id="findByName" resultType="com.xizhicheng.mybatis.entity.User">
        SELECT * FROM USER WHERE NAME = #{name}
    </select>

<!--根据ID查询用户信息-->
    <select id="findById" resultType="com.xizhicheng.mybatis.entity.User">
        SELECT * FROM USER WHERE ID = #{id}
    </select>

<!--根据用户名查询用户信息-->
    <select id="getUserInfoByName" resultMap="userResultMap">
        SELECT * FROM USER WHERE NAME = #{name}
    </select>

2、增加

(1)mapper

    // 插入一条用户信息
    void insertUser(User user);

(2)xml

<!--插入一条用户信息-->
    <insert id="insertUser" parameterType="com.xizhicheng.mybatis.entity.User">
        INSERT INTO USER(NAME, AGE, PWD) VALUES(#{name}, #{age}, #{pwd})
    </insert>

3、更新

(1)mapper

    // 根据ID更新用户信息
    void updateUserById(User user);

(2)xml

<!--根据ID更新用户信息-->
    <update id="updateUserById" parameterType="com.xizhicheng.mybatis.entity.User">
        UPDATE USER
            SET AGE = #{age},
                NAME = #{name},
                PWD = #{pwd}
         WHERE ID = #{id};
    </update>

4、删除

(1)mapper

    // 根据ID删除用户信息
    void deleteUserById(Integer id);

(2)xml

<!--根据ID删除用户信息-->
    <delete id="deleteUserById">
        delete from user where id = #{id}
    </delete>

二、批量语句

1、查询

(1)mapper

    // 批量查詢用户信息
    List<User> getUserInfoList(UserQueryReq req);

(2)xml

<!--批量查詢用户信息-->
    <select id="getUserInfoList" parameterType="com.xizhicheng.mybatis.dto.request.UserQueryReq" resultMap="userResultMap">
        select * from user
        where 1=1
        <if test="name != null">
            and NAME = #{name}
        </if>

        <if test="age != null">
            and age = #{age}
        </if>
    </select>

2、增加

(1)mapper

    // 批量插入用户信息
    Integer batchInsert(List<BatchInsertUserRequest> list);

(2)xml

<!--批量插入用户信息-->
    <insert id="batchInsert" parameterType="java.util.List">
        insert into user (name, age, pwd)
        values
        <foreach collection="list" item="list" index="index" separator=",">
            (#{list.name,jdbcType=VARCHAR},
            #{list.age,jdbcType=INTEGER},
            #{list.pwd,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

3、更新

(1)mapper

    // 批量更新用户信息
    int batchUpdate(List<BatchUpdateRequset> list);

(2)xml

<!--批量更新用户信息-->
    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="items" index="index" open="" close="" separator=";">
            UPDATE access_info
            <set>
                <if test="items.name != null">
                    name = #{items.name},
                </if>
                <if test="items.age != null">
                    age = #{items.age},
                </if>
                <if test="items.pwd != null">
                    pwd = #{items.pwd},
                </if>
            </set>
            where id = #{items.id}
        </foreach>
    </update>

;