【Mybatis框架】可以直接复用的java mapper文件 ✨ 每日积累

简介: 【Mybatis框架】可以直接复用的java mapper文件 ✨ 每日积累
<?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="mapper接口文件存放位置" >
  <resultMap id="BaseResultMap" type="实体类包位置" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP" />
    <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
  </resultMap>
  <resultMap id="QueryResultMap" type="实体类包位置" extends="BaseResultMap" />
  <sql id="Query_Where_Clause" >
    <trim prefix="WHERE" prefixOverrides="AND |OR " >
      <if test="id != null" >
        AND id = #{id,jdbcType=BIGINT}
      </if>
      <if test="gmtCreate != null" >
        AND gmt_create = #{gmtCreate,jdbcType=TIMESTAMP}
      </if>
      <if test="gmtModified != null" >
        AND gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}
      </if>
      <if test="userName != null" >
        AND user_name = #{userName,jdbcType=VARCHAR}
      </if>
    </trim>
  </sql>
  <sql id="Update_By_Query_Where_Clause" >
    <trim prefix="WHERE" prefixOverrides="AND |OR " >
      <if test="query.id != null" >
        AND id = #{query.id,jdbcType=BIGINT}
      </if>
      <if test="query.gmtCreate != null" >
        AND gmt_create = #{query.gmtCreate,jdbcType=TIMESTAMP}
      </if>
      <if test="query.gmtModified != null" >
        AND gmt_modified = #{query.gmtModified,jdbcType=TIMESTAMP}
      </if>
      <if test="query.userName != null" >
        AND user_name = #{query.userName,jdbcType=VARCHAR}
      </if>
    </trim>
  </sql>
  <sql id="Base_Column_List" >
    id, gmt_create, gmt_modified, user_name
  </sql>
  <select id="selectByQuery" resultMap="BaseResultMap" parameterType="query势例类存放路径" >
    select
    <include refid="Base_Column_List" />
    from user
    <if test="_parameter != null" >
      <include refid="Query_Where_Clause" />
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from user
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByQuery" parameterType="query势例类存放路径" >
    delete from user
    <if test="_parameter != null" >
      <include refid="Query_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="实体类存放位置" >
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
      SELECT @@IDENTITY
    </selectKey>
    insert into user (gmt_create, gmt_modified, user_name)
    values (#{gmtCreate,jdbcType=TIMESTAMP}, #{gmtModified,jdbcType=TIMESTAMP}, #{userName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertOrUpdate" parameterType="实体类存放位置" >
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
      SELECT @@IDENTITY
    </selectKey>
    insert into user (gmt_create, gmt_modified, owner_uid,
    user_id, user_name, display_name, active )
    values (#{gmtCreate,jdbcType=TIMESTAMP}, #{gmtModified,jdbcType=TIMESTAMP}, #{userName,jdbcType=VARCHAR})
    ON DUPLICATE KEY UPDATE
  </insert>
  <select id="countByQuery" parameterType="实体类存放位置" resultType="java.lang.Integer" >
    select count(*) from user
    <if test="_parameter != null" >
      <include refid="Query_Where_Clause" />
    </if>
  </select>
  <update id="updateByQuerySelective" parameterType="map" >
    update user
    <set >
      <if test="record.id != null" >
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.gmtCreate != null" >
        gmt_create = #{record.gmtCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="record.gmtModified != null" >
        gmt_modified = #{record.gmtModified,jdbcType=TIMESTAMP},
      </if>
      <if test="record.userName != null" >
        user_name = #{record.userName,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Query_Where_Clause" />
    </if>
  </update>
  <update id="updateByQuery" parameterType="map" >
    update user
    set id = #{record.id,jdbcType=BIGINT},
    gmt_create = #{record.gmtCreate,jdbcType=TIMESTAMP},
    gmt_modified = #{record.gmtModified,jdbcType=TIMESTAMP},
    user_name = #{record.userName,jdbcType=VARCHAR},
    <if test="_parameter != null" >
      <include refid="Update_By_Query_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="实体类包位置" >
    update user
    <set >
      <if test="gmtCreate != null" >
        gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
      </if>
      <if test="gmtModified != null" >
        gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
      </if>
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="实体类包位置" >
    update user
    set gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
    gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
    user_name = #{userName,jdbcType=VARCHAR},
  </update>
</mapper>
相关文章
|
11天前
|
Web App开发 Java 测试技术
《手把手教你》系列技巧篇(五十六)-java+ selenium自动化测试-下载文件-上篇(详细教程)
【5月更文挑战第20天】本文介绍了自动化测试中如何实现无弹窗下载文件,主要针对Firefox浏览器。作者指出,通常的下载操作包括点击下载按钮,但这里讨论的是避免下载弹窗直接保存文件的方法。文章详细讲解了通过设置Firefox参数(如`browser.download.dir`、`browser.helperApps.neverAsk.saveToDisk`等)来实现这一功能,并给出了Java Selenium的示例代码,展示了如何创建FirefoxProfile并进行相关设置,以及如何启动浏览器和执行下载操作。
30 0
《手把手教你》系列技巧篇(五十六)-java+ selenium自动化测试-下载文件-上篇(详细教程)
|
2天前
|
Java
io读两个文件,生成list 排重后写本地文件(Java)
io读两个文件,生成list 排重后写本地文件(Java)
|
2天前
|
Java Windows
文件操作和IO(2):Java中操作文件
文件操作和IO(2):Java中操作文件
5 0
|
3天前
|
Java 数据库连接 mybatis
Mybatis mapper动态代理解决方案
该文介绍了Mybatis中使用Mapper接口的方式代替XML配置执行SQL。Mapper接口规范包括:namespace与接口类路径相同,select ID与接口方法名一致,parameterType和方法参数类型匹配,resultType与返回值类型一致。实现过程中,需配置Mapper.xml,编写Mapper.java接口,并在Mybatis-config.xml中设置。测试类中,通过SqlSession的getMapper方法获取接口的动态代理对象,调用方法执行SQL。
|
3天前
|
Java 数据库连接 mybatis
为什么Mybatis Mapper不需要实现类?
在学习Java动态代理之前,我想让大家先思考这样几个问题。 • JDK动态代理为什么不能对类进行代理? • Mybatis Mapper接口为什么不需要实现类? 如果你还不知道上述问题的答案,那么这篇文章一定能消除你心中的疑惑。
|
8天前
|
Java Apache
Java将word、excel文件转成pdf文件
【5月更文挑战第26天】Java将word、excel文件转成pdf文件
48 1
|
8天前
|
存储 Java
java使用pdfbox 3.0版本删除pdf文件中指定字符所在行,生成新的pdf文件
【5月更文挑战第25天】java使用pdfbox 3.0版本删除pdf文件中指定字符所在行,生成新的pdf文件
21 1
|
9天前
|
XML Java 数据库连接
利用MyBatis框架操作数据库2
利用MyBatis框架操作数据库
19 2
|
9天前
|
Java 数据库连接 测试技术
利用MyBatis框架操作数据库1
利用MyBatis框架操作数据库
22 1
|
9天前
|
存储 缓存 Java
Java 文件 & 文件操作
Java 文件 & 文件操作