前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis + Oracle CRUD

Mybatis + Oracle CRUD

作者头像
Remember_Ray
发布2020-04-10 15:56:06
5870
发布2020-04-10 15:56:06
举报
文章被收录于专栏:Ray学习笔记

Columns

需要查询的列

Xml

代码语言:javascript
复制
<sql id="ztTerminalProducerColumns">
	a.id AS "id",
	a.terminal_id AS "terminalId",
	a.name AS "name",
	a.link_man AS "linkMan",
	a.telphone AS "telphone",
	a.address AS "address",
	a.emailbox AS "emailbox",
	a.orgunitid AS "orgunitid",
	a.unifiedid AS "unifiedid",
	a.business_license AS "businessLicense",
	a.create_date AS "createDate",
	a.create_by AS "createBy.id",
	a.update_date AS "updateDate",
	a.update_by AS "updateBy.id",
	a.remarks AS "remarks",
	a.del_flag AS "delFlag"
</sql>

Joins

需要加入的表

Xml

代码语言:javascript
复制
<sql id="ztTerminalProducerJoins">
</sql>

SELECT

get

获取单条记录

Xml

代码语言:javascript
复制
<select id="get" resultType="ZtTerminalProducer" >
	SELECT 
		<include refid="ztTerminalProducerColumns"/>
	FROM zt_terminal_producer a
		<include refid="ztTerminalProducerJoins"/>
	WHERE a.id = #{id}
</select>

findList

获取多条记录

Xml

代码语言:javascript
复制
<select id="findList" resultType="ZtTerminalProducer" >
	SELECT 
		<include refid="ztTerminalProducerColumns"/>
	FROM zt_terminal_producer a
		<include refid="ztTerminalProducerJoins"/>
	<where>
		a.del_flag = #{DEL_FLAG_NORMAL}
		<if test="terminalId != null and terminalId != ''">
			AND a.terminal_id = #{terminalId}
		</if>
		<if test="name != null and name != ''">
			AND a.name LIKE 
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
		</if>
	</where>
	<choose>
		<when test="page !=null and page.orderBy != null and page.orderBy != ''">
			ORDER BY ${page.orderBy}
		</when>
		<otherwise>
			ORDER BY a.update_date DESC
		</otherwise>
	</choose>
</select>

findAllList

获取多条记录

Xml

代码语言:javascript
复制
<select id="findAllList" resultType="ZtTerminalProducer" >
	SELECT 
		<include refid="ztTerminalProducerColumns"/>
	FROM zt_terminal_producer a
	<include refid="ztTerminalProducerJoins"/>
	<where>
		a.del_flag = #{DEL_FLAG_NORMAL}
	</where>		
	<choose>
		<when test="page !=null and page.orderBy != null and page.orderBy != ''">
			ORDER BY ${page.orderBy}
		</when>
		<otherwise>
			ORDER BY a.update_date DESC
		</otherwise>
	</choose>
</select>

${}

${}可变参数,有注入风险,推荐#{}

Xml

代码语言:javascript
复制
<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
<select id="findUniqueByProperty" resultType="ZtTerminalProducer" statementType="STATEMENT">
	select * FROM zt_terminal_producer  where ${propertyName} = '${value}'
</select>

INSERT

普通新增

Xml

代码语言:javascript
复制
<insert id="insert">
	INSERT INTO zt_terminal_producer(
		id,
		terminal_id,
		name,
		link_man,
		telphone,
		address,
		emailbox,
		orgunitid,
		unifiedid,
		business_license,
		create_date,
		create_by,
		update_date,
		update_by,
		remarks,
		del_flag
	) VALUES (
		#{id},
		#{terminalId},
		#{name},
		#{linkMan},
		#{telphone},
		#{address},
		#{emailbox},
		#{orgunitid},
		#{unifiedid},
		#{businessLicense},
		#{createDate},
		#{createBy.id},
		#{updateDate},
		#{updateBy.id},
		#{remarks},
		#{delFlag}
	)
</insert>

insertBatch

批量新增

Java

代码语言:javascript
复制
/**
 * 批量新增
 */
void insertBatch(@Param("ztTerminalProducerList") List<ZtTerminalProducer> ztTerminalProducerList);

Xml

代码语言:javascript
复制
   <insert id="insertBatch" parameterType="com.jeeplus.modules.ztfx.entity.ZtTerminalProducer">
	INSERT INTO zt_terminal_producer(
		id,
		terminal_id,
		name,
		link_man,
		telphone,
		address,
		emailbox,
		orgunitid,
		unifiedid,
		business_license,
		create_date,
		create_by,
		update_date,
		update_by,
		remarks,
		del_flag
	)
	<foreach collection="ztTerminalProducerList" item="tp" separator="UNION ALL">
		SELECT
			#{tp.id},
			#{tp.terminalId},
			#{tp.name},
			#{tp.linkMan},
			#{tp.telphone},
			#{tp.address},
			#{tp.emailbox},
			#{tp.orgunitid},
			#{tp.unifiedid},
			#{tp.businessLicense},
			#{tp.createDate},
			#{tp.createBy.id},
			#{tp.updateDate},
			#{tp.updateBy.id},
			#{tp.remarks},
			#{tp.delFlag}
		FROM DUAL
	</foreach>
</insert>

UPDATE

普通更新

Xml

代码语言:javascript
复制
   <update id="update">
	UPDATE zt_terminal_producer SET 	
		terminal_id = #{terminalId},
		name = #{name},
		link_man = #{linkMan},
		telphone = #{telphone},
		address = #{address},
		emailbox = #{emailbox},
		orgunitid = #{orgunitid},
		unifiedid = #{unifiedid},
		business_license = #{businessLicense},
		update_date = #{updateDate},
		update_by = #{updateBy.id},
		remarks = #{remarks}
	WHERE id = #{id}
</update>

updateBatch

批量更新 - 1

Java

代码语言:javascript
复制
/**
 * 根据id批量更新is_check标识
 */
void updateIsCheckByIds(@Param("ids") List<String> ids);

Xml

代码语言:javascript
复制
<!-- 批量更新 -->
<update id="updateIsCheckByIds">
	UPDATE zt_position_hist SET
		is_check = 1
	WHERE id in 
	<foreach collection="ids" item="id" open="(" separator="," close=")">
		#{id}
	</foreach>
</update>

批量更新 - 2

Java

代码语言:javascript
复制
int updateNewStock(@Param("list") List<GreenBeanMsg> list);

Xml

代码语言:javascript
复制
<update id="updateNewStock" parameterType="java.util.List">
    <foreach collection="list" item="bean" index="index" open="begin" close=";end;" separator=";">
        UPDATE green_beans
        <set>
            stock = #{bean.stock}
        </set>
        <where>
            beanUid = #{bean.beanUid}
        </where>
    </foreach>
</update>

注意:

open="begin" close=";end;",如果不加这个,无法识别 ;

DELETE

物理删除或逻辑删除

Xml

代码语言:javascript
复制
<!--物理删除-->
<update id="delete">
	DELETE FROM zt_terminal_producer
	WHERE id = #{id}
</update>

<!--逻辑删除-->
<update id="deleteByLogic">
	UPDATE zt_terminal_producer SET 
		del_flag = #{DEL_FLAG_DELETE}
	WHERE id = #{id}
</update>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-02-25|,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Columns
  • Joins
  • SELECT
    • get
      • findList
        • findAllList
          • ${}
          • INSERT
            • insertBatch
            • UPDATE
              • updateBatch
              • DELETE
              相关产品与服务
              批量计算
              批量计算(BatchCompute,Batch)是为有大数据计算业务的企业、科研单位等提供高性价比且易用的计算服务。批量计算 Batch 可以根据用户提供的批处理规模,智能地管理作业和调动其所需的最佳资源。有了 Batch 的帮助,您可以将精力集中在如何分析和处理数据结果上。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档