springboot+springmvc+mybatis 使用xml文件来写增查改的sql
2016-09-21 17:17
776 查看
最近在做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
环境下的,所用的数据库是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
相关文章推荐
- Spring Boot通过Mybatis,使用mapper接口和xml配置sql,连接数据库
- 使用springboot+springmvc+mybaits-spring+mybatis-generator开发
- Spring Boot干货系列:(九)数据存储篇-SQL关系型数据库之MyBatis的使用
- springboot+springmvc+mybatis 使用注解对数据库的增、改和查操作
- springMVC+mybatis 进行单元测试时 main SqlSessionFactoryBean - Parsed configuration file: 'class path resource' 无限的读取xml文件
- Spring Boot干货系列:(九)数据存储篇-SQL关系型数据库之MyBatis的使用
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
- 使用maven+springboot+springmvc+mybaits-spring+mybatis-generator开发
- Spring-Boot整合mybatis(二),使用阿里的druid包,进行监控SQL执行速度
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
- SpringMVC+Spring+Mybatis+Maven整合-使用的工具
- zTree的调用设使用(跨两个系统,两类技术实现的项目案例SpringMVC+Spring+MyBatis和Struts2+Spring+ibatis框架组合)
- springmvc+mybatis 使用 POIFSFileSystem 读取excel中的日期 问题
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持
- Spring+SpringMVC+mybatis+easyui整合实例(五)使用mybatis拦截器分页
- springmvc+mybatis+sql server实现简单登录功能
- spring mvc+mybatis+sql server简单配置
- (菜鸟来动手)SpringMVC+Spring+Mybatis整合(第二天) 使用maven来构建项目
- Spring+SpringMVC+mybatis+easyui整合实例(二)注解方式的mybatis的使用