MyBatis操作DB2 數(shù)據(jù)庫

話不多說,直接上代碼!

實體類

User.java

@Data
public class User {
    private String userId;
    private String username;
    private String password;
    private String age;
}

數(shù)據(jù)操作層

UserDao.java

@Mapper
public interface UserDao {


    /**
     *  新增
     */
    Integer insert(User user);

    /**
     * 動態(tài)新增
     */
    Integer insertUser(User user);

    /**
     * 批量新增
     */ 
    Integer insertUserList(List<User> users);

    /**
     * 刪除
     */
    Integer deleteByUsername(String userId);

    /**
     * 根據(jù)userID批量刪除
     */
    Integer batchDeleteByUserId(List<String> userId);


    /**
     *  查詢
     */
    User selectByUsername(String username);

    /**
     * 模糊查詢
     */
    List<User> selectUser(User user);

    /**
     * 根據(jù)用戶Id批量查詢
     */
    List<User> selectByUserIdList(List<String> ids);

    /**
     * 更新
     */
    Integer update(String userId, String password);


    /**
     * 動態(tài)更新
     */
    Integer updateUser(User user);



mapper

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.examp.db2Test.dao.UserDao">

    <!--新增-->
    <insert id="insert" parameterType="com.examp.db2Test.entity.User">
        insert 
            into msk.user(userId, username, password, sex) 
            values(#{userId}, #{username}, #{password}, #{sex})
    </insert>

    <!--動態(tài)新增-->
    <insert id="insertUser" parameterType="com.examp.db2Test.entity.User">
        insert into msk.user

        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="username != null" >
                username,
            </if>
            <if test="password != null" >
                password,
            </if>
            <if test="sex != null" >
                sex,
            </if>
        </tirm>

        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="username != null" >
                username = #{username, jdbcType="VARCHAR"}
            </if>
            <if test="password != null" >
                password = #{password, jdbcType="VARCHAR"}
            </if>
            <if test="sex != null" >
                sex = #{sex, jdbcType="VARCHAR"}
            </if>
        </trim>
    </insert>


    <!-- 批量新增 -->
    <insert id='insertUserList'>
        insert into msk.user
            (userId, username, password, sex)
        values
        <foreach collection="list" item="user" separator=",">
            (
                #{user.userId}, #{user.username}, #{user.password}, #{user.sex}
            )
        </foreach>
    </insert>

    <!--刪除-->
    <delete id="deleteByUsername" parameterType="String">
        delete msk.user where userId=#{userId}
    </delete>

    <!-- 根據(jù)userID批量刪除 -->
    <delete id="batchDeleteByUserId">
        delete 
            from msk.user 
        where userId in 
        (
            <foreach collection="list" item="userId" separator=",">
                #{userId}
            </foreach>
        )
    </delete>

    <!--查詢-->
    <select id="select" resultType="com.examp.db2Test.entity.User">
        select 
            userId, username, password, sex 
        from msk.user where username=#{username}
    </select>

    <!--模糊查詢-->
    <select id="selectUser" resultType="com.examp.db2Test.entity.User">
        select 
            userId, username, password, sex
        from msk.user
            where 1=1  
        <if test="username !=null and username !=''">
            and username like '%' || #{username} || '%'
        </if>
        <if test="password !=null and password !=''">
            and password like '%' || #{password} || '%'
        </if>
        <if test="sex !=null and sex !=''">
            and sex like '%' || #{sex} || '%'
        </if>
    </select>

    <!-- 根據(jù)用戶Id批量查詢 -->
    <select id="selectByUserIdList" resultType="com.examp.db2Test.entity.User">
        select 
            userId, username, password, sex
        from msk.user
        where userId in 
        <foreach collection="list" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </select>

    <!--更新-->
    <update id="update" parameterType="String">
        update
            msk.user 
            set password = #{password}
            where userId=#{userId}
    </update>

    <!-- 動態(tài)更新 -->
    <update id="updateUser" parameterType="com.examp.db2Test.entity.User">
        update msk.user
        <set>
            <if test="username != null" >
                username = #{username, jdbcType="VARCHAR"}
            </if>
            <if test="password != null" >
                password = #{password, jdbcType="VARCHAR"}
            </if>
            <if test="sex != null" >
                sex = #{sex, jdbcType="VARCHAR"}
            </if>
        </set>
        where userId = #{userId, jdbcType=VARCHAR}
    </update>
   
</mapper>

以上這些日常操作基本夠用,其他數(shù)據(jù)庫基本也差不多是這樣,根據(jù)需要稍作修改即可。如有不足,還請指正

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容