数据库

MyBatis操作DB2 数据库

2020-08-25  本文已影响0人  马赛克同学

话不多说,直接上代码!

实体类

User.java

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

数据操作层

UserDao.java

@Mapper
public interface UserDao {


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

    /**
     * 动态新增
     */
    Integer insertUser(User user);

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

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

    /**
     * 根据userID批量删除
     */
    Integer batchDeleteByUserId(List<String> userId);


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

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

    /**
     * 根据用户Id批量查询
     */
    List<User> selectByUserIdList(List<String> ids);

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


    /**
     * 动态更新
     */
    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>

    <!--动态新增-->
    <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>

    <!-- 根据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>

    <!-- 根据用户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>

    <!-- 动态更新 -->
    <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>

以上这些日常操作基本够用,其他数据库基本也差不多是这样,根据需要稍作修改即可。如有不足,还请指正

上一篇下一篇

猜你喜欢

热点阅读