springboot+springmvc+mybatis 使用xml文件来写增查改的sql

本文介绍了在SpringBoot、SpringMVC和MyBatis环境中,使用XML文件进行增删改查操作的方法。详细展示了mapper.xml中关于查询和更新的SQL语句,包括单个条件查询、多个条件in查询,以及插入和更新的SQL语句。同时提到了resultMap和parameterType的使用注意事项,以及在使用in查询时如何处理参数。

最近在做Java后台所遇到的问题和总结,本文针对的是springboot+springmvc+mybatis 环境下的,所用的数据库是oracle。

mapper.xml文件

<?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.mapper.CustomerFieldMapper">
<resultMap type="com.entity.MstbCrmCustomerField" id="CustomerField">
<result column="ID" property="id" jdbcType="INTEGER"/>
<result column="BUSINESS_ID" property="businessId" jdbcType="VARCHAR"/>
<result column="CUSTOMER_BUS_ID" property="customerBusId" jdbcType="VARCHAR"/>
<result column="CREATE_TIME" property="createTime" jdbcType="TIMESTAMP"/>
<result column="UPDATE_TIME" property="updateTime" jdbcType="TIMESTAMP"/>
<result column="SPARE1" property="spare1" jdbcType="VARCHAR"/>
<result column="SPARE2" property="spare2" jdbcType="VARCHAR"/>
<result column="SPARE3" property="spare3" jdbcType="VARCHAR"/>
</resultMap>

<select id="findCustomerFieldByMap" resultMap="CustomerField" parameterType="java.util.HashMap">
select * from
mstb_crm_customer_field t
where t.business_id = #{businessId,jdbcType=VARCHAR}
</select>
<select id="findCustomerFieldList" resultMap="CustomerField">
select * from mstb_crm_customer_field t where t.CUSTOMER_BUS_ID in 

  <foreach collection="list" item="customerBusId"
                    index="index" open="(" close=")" separator=",">
                  #{customerBusId}
                </foreach>

</select>


<insert id="insert" parameterType="com.entity.MstbCrmCustomerField">
INSERT INTO
mstb_crm_customer_field(ID,BUSINESS_ID,CUSTOMER_BUS_ID,
MD5,OWNERADA,STATUS,FIELD_NAME,FIELD_INDEX,FIELD_CONTENT,
CREATE_TIME,UPDATE_TIME,SPARE1,SPARE2,SPARE3)
VALUES(MSTB_CRM_CUSTOMER_FIELD_SEQ.nextval,
#{businessId,jdbcType=VARCHAR},
#{customerBusId,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP},
#{updateTime,jdbcType=TIMESTAMP},
#{spare1,jdbcType=VARCHAR},
#{spare2,jdbcType=VARCHAR},
#{spare3,jdbcType=VARCHAR})
</insert>
<update id="update" parameterType="com.entity.MstbCrmCustomerField">
UPDATE mstb_crm_customer_field SET
BUSINESS_ID=#{businessId,jdbcType=VARCHAR},
CUSTOMER_BUS_ID=#{customerBusId,jdbcType=VARCHAR},
CREATE_TIME=#{createTime,jdbcType=TIMESTAMP},
UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP},
SPARE1=#{spare1,jdbcType=VARCHAR},
SPARE2=#{spare2,jdbcType=VARCHAR},
SPARE3=#{spare3,jdbcType=VARCHAR}
WHERE ID=#{id,jdbcType=INTEGER}
</update>
</mapper>

方法如下:

public MstbCrmCustomerField findCustomerFieldByMap(Map<String, Object> map);

public List<MstbCrmCustomerField> findCustomerFieldList(List<String> list);

public void insert(MstbCrmCustomerField customerField) ;

public void update(MstbCrmCustomerField customerField) ;

注意:

1.当查询的时候只要搞清楚resultMap、parameterType指的是什么就差不多了。返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

2.在用in查询的时候要注意了,要使用迭代。参考http://fireinjava.iteye.com/blog/1779420

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值