zoukankan      html  css  js  c++  java
  • mybatis-mapper.xml 增删改查SQL详解

    <?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.example.mybatisdemo.daomapper.StudentMapper">
    <resultMap id="BaseResultMap" type="com.example.mybatisdemo.entity.Student">
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="sex" jdbcType="CHAR" property="sex" />
    </resultMap>
    //--以上内容是自动生成得

    //插入一条新得数据,部分字段更新
    //parameterType 入参是对象

    <insert id="insertStudent" parameterType="com.example.mybatisdemo.entity.Student">
      insert into mysql_student ( name, age, sex)
    values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
    #{sex,jdbcType=CHAR})
    </insert>
    //插入一条新得数据,每个字段都更新切判断
    //useGeneratedKeys="true"  keyProperty="id":当主键id是自增的情况下,添加一条记录的同时,我们要获取到主键id。
    // <trim prefix="(" suffix=")" suffixOverrides=","> ,prefix:在trim标签内sql语句加上前缀"(",suffix:在trim标签内sql语句加上后缀")",suffixOverrides:指定去除多余的后缀内容","
    <insert id="insertSelective" parameterType="com.example.mybatisdemo.entity.Student"  useGeneratedKeys="true"
    keyProperty="id">
    insert into mysql_student
    <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
    id,
    </if>
    <if test="name != null">
    name,
    </if>
    <if test="age != null">
    age,
    </if>
    <if test="sex != null">
    sex,
    </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
    #{id,jdbcType=INTEGER},
    </if>
    <if test="name != null">
    #{name,jdbcType=VARCHAR},
    </if>
    <if test="age != null">
    #{age,jdbcType=INTEGER},
    </if>
    <if test="sex != null">
    #{sex,jdbcType=CHAR},
    </if>
    </trim>
    </insert>
    -------------------------------------------------------------------------
    //修改,每个字段判空
    <update id="update" parameterType="addressMappingDto">
    update address_base
    <set>
    <trim prefix="" suffix="" suffixOverrides=",">
    <if test="road != null">
    road = #{road,jdbcType=VARCHAR},
    </if>
    <if test="stationId != null">
    station_id = #{stationId,jdbcType=INTEGER},
    </if>
    <if test="stationName != null and stationName !=''">
    station_name = #{stationName,jdbcType=VARCHAR},
    </if>
    update_time = now(),
    update_user = #{updateUser,jdbcType=VARCHAR}
    </trim>
    </set>
    where id = #{id,jdbcType=BIGINT}
    AND is_delete = 0
    </update>

    //简单得update
    <update id="updateUser" parameterType="com.example.mybatisdemo.entity.Student">
    update mysql_student
    set name = #{name,jdbcType=VARCHAR},
    age = #{age,jdbcType=INTEGER},
    sex = #{sex,jdbcType=CHAR}
    where id = #{id,jdbcType=INTEGER}
    </update>
    -----------------------------------------------------------
    <!-- 删除实体 -->
    <update id="delete" parameterType="addressMappingDto">
    update address_base
    <set>
    is_delete = 1,
    update_time = now(),
    update_user = #{updateUser,jdbcType=VARCHAR}
    </set>
    where id = #{id,jdbcType=BIGINT}
    AND is_delete = 0
    </update>

    <delete id="deleteUserById" parameterType="java.lang.Integer">
    delete from mysql_student
    where id = #{id,jdbcType=INTEGER}
    </delete>
    ------------------------------------------------------
    //查询根据id
    <select id="selectStudentId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select * from mysql_student where id = #{id,jdbcType=INTEGER}
    </select>
    //查询多条件
    <select id="getUserByCondition" parameterType="java.lang.String" resultMap="BaseResultMap">
    select * from mysql_student where name = #{name,jdbcType=VARCHAR} and age=#{age,jdbcType=INTEGER}
    </select>
    //根据条件查询一个实体
    <select id="findOneByCky2AndAddress" resultMap="BaseResultMap"
    parameterType="addressMappingDto">
    select
    id,
    province_id,
    city_id,
    county_id,
    town_id,
    cky2,
    address,
    road,
    station_id,
    station_name
    from address_base
    where cky2 = #{cky2,jdbcType=INTEGER}
    AND address = #{address,jdbcType=VARCHAR}
    AND is_delete = 0
    ORDER BY id DESC LIMIT 1
    </select>
     
  • 相关阅读:
    phonon 播放视频
    ubuntu清理系统垃圾与备份
    QT中调用mplayer显示视频并加透明文字
    Qt中的QFileDialog类的几个示范代码
    Qt Phonon 例子
    Ubuntu 安装qt 和其他开发环境
    qhttp 下载
    QT 中文乱码 解决
    thrift oneway的问题
    leveldb 之 内存管理
  • 原文地址:https://www.cnblogs.com/danyuzhu11/p/15543926.html
Copyright © 2011-2022 走看看