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>
注:可以通过<sql>中的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 '%王%'

 

6、查询某一个字段的最新记录

 

<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表示只取前一行的数据.

 

posted on 2020-03-12 20:53  coldpills  阅读(931)  评论(0编辑  收藏  举报