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

来源:互联网 发布:淘宝卖家手机必备软件 编辑:程序博客网 时间:2024/04/29 20:37

最近在做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

0 0
原创粉丝点击