mybatis中一些sql语法使用方法

在mybatis中的mapper中使用批量添加,这里针对mysql与oracle是有所区别的

在Oracle数据库

<!-- 批量添加 -->
  <insert id="insertyygetparts" parameterType="java.util.List">
  insert into C##TRAIN.YY_GETPARTS (ID, COMPONENTCATALOGID, COMPONENTCATALOGNAME, 
      USERID, USERNAME, WAREHOUSEID, 
      WAREHOUSENAME, NUM) 
      <foreach collection="list" item="YyGetparts" index="index" open="(" separator="union all" close=")">
      SELECT
      #{YyGetparts.id, jdbcType=VARCHAR},
      #{YyGetparts.componentcatalogid},
      #{YyGetparts.componentcatalogname},
      #{YyGetparts.userid},
      #{YyGetparts.username},
      #{YyGetparts.warehouseid},
      #{YyGetparts.warehousename},
      #{YyGetparts.num}
      from dual
      </foreach>
  </insert>

在mysql数据库

 <!-- 批量添加 -->
  <insert id="insertyygetparts" parameterType="java.util.List">
  insert into C##TRAIN.YY_GETPARTS (ID, COMPONENTCATALOGID, COMPONENTCATALOGNAME, 
      USERID, USERNAME, WAREHOUSEID, 
      WAREHOUSENAME, NUM) 
      <foreach collection="list" item="YyGetparts" index="index" separator=",">
      #{YyGetparts.id, jdbcType=VARCHAR},
      #{YyGetparts.componentcatalogid},
      #{YyGetparts.componentcatalogname},
      #{YyGetparts.userid},
      #{YyGetparts.username},
      #{YyGetparts.warehouseid},
      #{YyGetparts.warehousename},
      #{YyGetparts.num}
      </foreach>
  </insert>

在批量删除上mysql与oracle是一样的

<!-- 批量删除 -->
  <delete id="deleteYyGetparts" parameterType="java.util.List">
  delete from C##TRAIN.YY_GETPARTS 
  where id in 
  <foreach collection="list" item="id"  index="index" open="(" separator="," close=")">
  #{id}
  </foreach>
  </delete> 

在mysql使用in方法查询

<if test="catalogName != null">
  AND bd.catalog_name in 
  <foreach item="catalogName" index="index" collection="catalogName" open="(" separator="," close=")">
  #{catalogName}
  </foreach>
  </if>

foreach元素的属性主要有 item,index,collection,open,separator,close。

    item表示集合中每一个元素进行迭代时的别名,
    index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
    open表示该语句以什么开始,
    separator表示在每次进行迭代之间以什么符号作为分隔 符,

    close表示以什么结束。

在mybatis的mapper.xml中${name}与#{name}的区别和使用场景

使用占位符#{}可以有效防止sql注入,在使用时不需要关系参数值的类型,mybatis会自动进行JAVA类型和JDBC类型的装换。说的通俗一点,当我们使用#{}的时候,产生的sql,#{}代表的内容会自动的添加上''。

例子:

    <select id="selectByParmName" resultMap="BaseResultMap" parameterType="hashmap">
  	select <include refid="Base_Column_List"></include>
  	from bus_parm 
  	<where>
  		<if test="parmName != null">
  			AND parm_name = #{parmName}
  		</if>
  	</where>
  </select>	select <include refid="Base_Column_List"></include>
  	from bus_parm 
  	<where>
  		<if test="parmName != null">
  			AND parm_name = #{parmName}
  		</if>
  	</where>
  </select>
生成的sql: select * from bus_parm where parm_name='2222'

使用占位符${}可以将parameterType传入的内容拼接在sql中且不进行JDBC类型转换,不能防止sql注入。通俗一点就是,当我们使用${}的时候,产生的sql,${}代表的内容不会自动添加引号,传入的十什么就是什么。

例子:

<select id="selectByParmName" resultMap="BaseResultMap" parameterType="hashmap">  
    select <include refid="Base_Column_List"></include>
  	from bus_parm 
  	<where>
  		<if test="parmName != null">
  			AND parm_name like '%${parmName}%'
  		</if>
  	</where>
</select>
生成的sql:select * from bus_parm where parm_name like '%22%'bus_parm where parm_name like '%22%'
 

在mysql和mybatis使用or和and连用的例子

sql中的语句,实现的就是在 clxxz_id或ldzcz_id或gongzhang_id或zljcy_id或yanshouyuan_id的id等于1并且时间在20180329和20180502之间。

select * from lund_maintain 
WHERE 
(clxxz_id = '1' OR 
ldzcz_id = '1' OR 
gongzhang_id = '1' OR 
zljcy_id = '1' OR 
yanshouyuan_id = '1' )
AND Date(CreateTime) >= '2018-03-29' 
AND Date(CreateTime) <= '2018-05-02' 
order by CreateTime desc

转化成mapper

<select id="selectSignature" resultMap="BaseResultMap" parameterType="hashmap">
  	select <include refid="Base_Column_List"></include>
  	from lund_maintain
  	<where>
  		<if test="stateType == 1">
  			<trim prefix="(" suffix=")" prefixOverrides="OR" >
				<if test="userId != null">
	  				OR clxxz_id = #{userId}
	  			</if>
	  			<if test="userId != null">
	  				OR ldzcz_id = #{userId}
	  			</if>
	  			<if test="userId != null">
	  				OR gongzhang_id = #{userId}
	  			</if>
	  			<if test="userId != null">
	  				OR zljcy_id = #{userId}
	  			</if>
	  			<if test="userId != null">
	  				OR yanshouyuan_id = #{userId}
	  			</if>
  			</trim>
  		</if>
  		<if test="beginTime != null">
	  		AND Date(CreateTime) >= #{beginTime,jdbcType=TIMESTAMP}
	  	</if>
	  	<if test="endTime != null">
	  		AND Date(CreateTime) <![CDATA[<=]]> #{endTime,jdbcType=TIMESTAMP}
	  	</if>
  	</where>
  	order by CreateTime desc
  </select>

需要注意事项:
这里讲解trim标签的几个属性:
prefix:前缀,指的是trim内容最开开始位置
suffix:后缀,指的是trim内容结束后添加
prefixoverride:去掉trim内容第一个出现的内容
suffixoverride:去掉trim最后一次出现的内容
在mybatis使用时候,我们使用trim标签后,trim和where标签一同使用这里需要自己取消where后的第一个条件前的AND或者OR因为这个时候where标签自动去掉第一个条件前的AND功能失效

Mysql的一个查询用法:

例子:现在有score表,和deduction表,score是一个人员成绩表,deduction是一个成绩范围内减去分数的表,我们需要查询出来成绩表的所有记录,并且用score的个人成绩去匹配deduction表的成绩范围,添加这个个人成绩应该减去的分值。

下面是score表

CREATE TABLE `score`  (
  `ID_` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '职工考试管理',
  `score_` int(3) NULL DEFAULT NULL COMMENT '个人成绩',
  `create_User_ID_` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建用户的ID',
  `create_User_Real_Name_` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建用户的真实姓名',
  `create_Time_` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `desc` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
  `default_select_` int(1) NULL DEFAULT NULL,
  PRIMARY KEY (`ID_`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

下面是deduction表

CREATE TABLE `deduction`  (
  `ID_` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '绩效考核》职工考试减分设置',
  `score_min_` int(3) NULL DEFAULT NULL COMMENT '成绩范围最小分',
  `score_max_` int(3) NULL DEFAULT NULL COMMENT '成绩范围的最大分',
  `minus_score_` int(2) NULL DEFAULT NULL COMMENT '减去的分值',
  `create_User_ID_` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建用户ID',
  `create_User_Real_Name_` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建用户的真实姓名',
  `create_Time_` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `desc_` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
  `default_select_` int(1) NULL DEFAULT NULL,
  PRIMARY KEY (`ID_`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

下面使我们SQL

SELECT score.* ,
	( SELECT deduction.minus_score_ FROM deduction 
		WHERE 
			(score.score_ >= deduction.score_min_ 
		AND 
			score.score_ <= deduction.score_max_ )
	) AS jianfengScore 
FROM
	score 

猜你喜欢

转载自blog.csdn.net/gelong_bokewang/article/details/75274566