MyBatis中XML文件的模板

MyBatis中XML文件的模板

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="generator.mapper.UsersMapper">

    <resultMap id="BaseResultMap" type="generator.domain.Users">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="username" column="username" jdbcType="VARCHAR"/>
            <result property="password" column="password" jdbcType="VARCHAR"/>
            <result property="email" column="email" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,username,password,
        email
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from users
        where  id = #{id,jdbcType=INTEGER} 
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete from users
        where  id = #{id,jdbcType=INTEGER} 
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="generator.domain.Users" useGeneratedKeys="true">
        insert into users
        ( id,username,password
        ,email)
        values (#{id,jdbcType=INTEGER},#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR}
        ,#{email,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="generator.domain.Users" useGeneratedKeys="true">
        insert into users
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null">id,</if>
                <if test="username != null">username,</if>
                <if test="password != null">password,</if>
                <if test="email != null">email,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="id != null">#{id,jdbcType=INTEGER},</if>
                <if test="username != null">#{username,jdbcType=VARCHAR},</if>
                <if test="password != null">#{password,jdbcType=VARCHAR},</if>
                <if test="email != null">#{email,jdbcType=VARCHAR},</if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="generator.domain.Users">
        update users
        <set>
                <if test="username != null">
                    username = #{username,jdbcType=VARCHAR},
                </if>
                <if test="password != null">
                    password = #{password,jdbcType=VARCHAR},
                </if>
                <if test="email != null">
                    email = #{email,jdbcType=VARCHAR},
                </if>
        </set>
        where   id = #{id,jdbcType=INTEGER} 
    </update>
    <update id="updateByPrimaryKey" parameterType="generator.domain.Users">
        update users
        set 
            username =  #{username,jdbcType=VARCHAR},
            password =  #{password,jdbcType=VARCHAR},
            email =  #{email,jdbcType=VARCHAR}
        where   id = #{id,jdbcType=INTEGER} 
    </update>
</mapper>
相关推荐
武子康7 分钟前
Java-39 深入浅出 Spring - AOP切面增强 核心概念 通知类型 XML+注解方式 附代码
xml·java·大数据·开发语言·后端·spring
泽韦德8 分钟前
【Redis】笔记|第9节|Redis Stack扩展功能
数据库·redis·笔记
喜欢踢足球的老罗14 分钟前
使用 Spring Boot 3.3 和 JdbcTemplate 操作 MySQL 数据库
数据库·spring boot·mysql
文牧之16 分钟前
PostgreSQL 的扩展pg_prewarm
运维·数据库·postgresql
行星00819 分钟前
Postgresql字符串操作函数
数据库·postgresql
清风~徐~来1 小时前
【Redis】类型补充
数据库·redis·缓存
代码探秘者1 小时前
【Redis从入门到精通实战文章汇总】
数据库·redis·缓存
weixin_748877001 小时前
【Redis实战:缓存与消息队列的应用】
数据库·redis·缓存
····懂···2 小时前
PostgreSQL 技术峰会,为您打造深度交流优质平台
数据库·postgresql
2301_802502335 小时前
哈工大计算机系统2025大作业——Hello的程序人生
数据库·程序人生·课程设计