Java后端的SQL语句

1. 选择

<select id="getStaffName" parameterType="String" resultType="com.wlhse.entity.SuperVisionGroupMemberPojo">
    select * from supervisiongroupmember where SupervisionGroupCode=#{uid};
</select>

注:此处的id必须与xxxDao接口(interface)执行该sql语句的方法名一致。
  如果需要传值,需要加入parameterType属性与id属性并列。resultType表示查询到的值封装的类型。
  uid只是一个占位符,代表传入的值。而SupervisionGroupCode是数据库中表的字段(据说windows不区分大小写,我这里表名、字段都区分了大小写)
  
2. 添加

<insert id="addTask">
  insert into supervisiontask(StartDate,EndDate)values(#{startDate},#{endDate});
</insert>

注:第一个()中,代表的是数据库中的字段,第二个()中代表的是实体类中的字段,由于实体类是驼峰形式,所以第一个字母小写了。如果不小写,get/set方法不会起作用,切记!!!

3. 更新

<update id="addTask" parameterType="com.wlhse.entity.SuperVisionTaskPojo">
  insert into set StartDate=#{startDate},EndDate=#{endDate} where id=#{id};
</update>

以上语句还可以改写为:

<sql id="key1">
    <trim suffixOverrides=",">
        <if test="startDate != null and startDate  != ''">
            StartDate,
        </if>
        <if test="endDate != null and endDate  != ''">
            EndDate,
        </if>
  </trim>
</sql>
<sql id="value1">
    <trim suffixOverrides=",">
        <if test="startDate != null and startDate  != ''">
            #{startDate},
        </if>
        <if test="endDate != null and endDate  != ''">
            #{endDate},
        </if>
  </trim>
</sql>
<insert id="addTask">
    insert into supervisiontask
    (<include refid="key1"/> )
    values
    (<include refid="value1"/>)
</insert>

注:可以通过中的id中的值来确定语句,在insert中引用该语句。当需要写比较复杂的语句时,可以使用这种方式。

4. 删除

<delete id="addTask" parameterType="Interger">
  delete from supervisiontask where id=#{uid};
</delete>

注:id指的是数据库中的字段,uid只是占位符,代表传入的参数。

5. 模糊查询

<select id="getTaskTypeDetail" parameterType="String" resultType="com.wlhse.entity.DataDictPojo">
    select * from datadict where DictCode like '%${value}%'
</select>

注:value代表传入的参数,比如传入的是字符串”王“则语句为 select * from datadict where DictCode like ‘%王%’

7. 查询某一个字段的最新记录

<select id="queryStartDateByName" parameterType="String" resultType="String">
    SELECT StartDate FROM supervisiontask WHERE ExecStaffName=#{uname} ORDER BY SupervisionTaskID DESC LIMIT 0,1;
</select>

注:这里是查询员工名为某某某的任务起始时间,后面的ORDER BY SupervisionTaskID DESC LIMIT 0,1表示:通过SupervisionTaskID来排序,后面的LIMIT 0,1表示只取前一行的数据.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值