SpringBoot Mybatis问题收集
1.在SpringBoot中打印mybatis中执行的sql
其实在application.properties 文件下,添加一下配置即可:
logging.level.org.springframework=WARN
logging.level.org.spring.springboot.dao=DEBUG
logging.file=logs/spring-boot-logging.log
注意:其中logging.level.com.你的Mapper包=日志等级
logging.level.com.shitou.huishi.domain.dataaccess=debug
2.在mybatis中返回count这个方法的结果
<select id="selectListByType" parameterType="com.shitou.huishi.domain.entity.BusinessMarketAffiliate" resultType="java.lang.Integer">
select count() as num from tb_business_market_affiliate
where busi_id=#{busiId,jdbcType=INTEGER}
and data_key=#{dataKey,jdbcType=INTEGER}
and data_type=#{dataType,jdbcType=VARCHAR}
</select>
将resultMap="java.lang.Integer" 改成 resultType="java.lang.Integer"
3.在Mybatis中单个参数使用test判断
<select id="selectByPcode" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tb_area_divisions
where
<choose>
<when test="_parameter > 0">
pcode = #{pcode,jdbcType=INTEGER}
</when>
<otherwise>
level=
</otherwise>
</choose> </select>
注意语句中_parameter,使用pcode时会报错
程序异常,nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'pcode' in 'class java.lang.Integer'
但是单个参数和多参数的判断有个不同点,当我们的入参为entity实体,或者map的时候,使用if 参数判断没任何问题。
但是当我们的入参为java.lang.Integer 或者 java.lang.String的时候,这时候就需要注意一些事情了
首先入参是java.lang.Integer, 而不是map或者实体的入参方式,对于这类单个入参然后用if判断的,mybatis有自己的内置对象
4.MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
这个问题跟mybatis的版本有关
比如在mybatis3.4.0时 (mybatis-spring-boot-starter 1.1.1),#{0},#{1}都是可以使用的
但是在mybatis3.4.6时(mybatis-spring-boot-starter 1.3.2),使用#{0},#{1}就不可以,就会报上述错误
根据网上的描述,在mybatis3.4.4版本为边界,从MyBatis3.4.4版后不能直接使用 #{0} 要使用 #{arg0} ;
5.parametertype 多个参数
不写parameterType参数,但是不能改变参数顺序,也不能重复使用参数
public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
<select id="getXXXBeanList" resultType="XXBean">不需要写parameterType参数
select t.* from tableName where id = #{} and name = #{}
</select>
2.基于注解,这个比较推荐
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{id} and name = #{code}
</select>
https://blog.csdn.net/lixld/article/details/77980443
6.模糊查询使用
subject_name like '%${subjectName}%'
或者
subject_name like CONCAT('%',#{subjectName,jdbcType=VARCHAR},'%')
https://blog.csdn.net/u010398771/article/details/70768280
7.Mybatis语句中使用到大于小于
mybatis查询的时候,需要用到运算符 小于号:< 和 大于号: >,在mybatis配置文件里面,这种会被认为是标签,所以解析错误
解决方法:
<if test="null!=beginTime and ''!=beginTime">
<![CDATA[
and create_time>=#{beginTime,jdbcType=VARCHAR}
]]>
</if>
<if test="null!=endTime and ''!=endTime">
<![CDATA[
and create_time<=#{endTime,jdbcType=VARCHAR}
]]>
</if>
用 <![CDATA[ ]]> 把sql语句包裹起来,注意不要 包裹<if这样的,必须只能包裹sql语句。
8.Mybatis中使用in进行条件查询
下面这种情况是将names作为map的其中一个参数进行传递的
List<String> userNameList=new ArrayList<String>();
Map<String,Object> map= ReflectUtil.beanToMap(request);
map.put("userNames",userNameList);
<if test="null!=userNames and userNames.size>0 ">
and create_name in
<foreach collection="userNames" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
必须对列表进行筛选,size必须大于0,不然直接就是create_name in,foreach条件查询为空,sql语句会报错!
也可以自己手动拼接,然后使用${userNames}
如果参数类型为list,则在使用的时候,collection属性必须指定为list
查询方法:
List<FolderImgInfo> selectListByImageIds(List<Integer> ids);
Mybatis部分为:
where =
<if test="null!=list and list.size>0 ">
and id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
9.使用Map进行参数传递:
code:
List<LoanOrder> getLoanOrderListForJDGL(Map<String,Object> map);
xml:
<select id="getLoanOrderListForJDGL" parameterType="java.util.Map" resultMap="BaseResultMap">
select * from (
select loan_order_id,loan_type,
case loan_type
when then (select subject_no from hs_archive_info where archive_id=main_subject_archive_id)
when then (select subject_no from hs_archive_info where archive_id=borrower_archive_id)
end subject_no,
case loan_type
when then main_subject
when then borrower_name
end subject_name,
org_source,
create_time,create_user_id, create_user_dept,create_name
from hs_loan_order
where loan_status=
order by create_time desc
) tb
<where>
=
<if test="null!=productId">
and loan_type=#{productId,jdbcType=BIGINT}
</if>
<if test="null!=loanOrderId and ''!=loanOrderId">
and loan_order_id=#{loanOrderId,jdbcType=VARCHAR}
</if>
<if test="null!=subjectName and ''!=subjectName">
and subject_name=#{subjectName,jdbcType=VARCHAR}
</if>
<if test="null!=subjectNo and ''!=subjectNo">
and subject_no=#{subjectNo,jdbcType=VARCHAR}
</if>
<if test="null!=createUserId and createUserId>0">
and create_user_id=#{createUserId,jdbcType=BIGINT}
</if>
<if test="null!=departName and ''!=departName">
and create_user_dept=#{departName,jdbcType=VARCHAR}
</if>
<if test="null!=beginTime and ''!=beginTime">
<![CDATA[
and create_time>=#{beginTime,jdbcType=VARCHAR}
]]>
</if>
<if test="null!=endTime and ''!=endTime">
<![CDATA[
and create_time<=#{endTime,jdbcType=VARCHAR}
]]>
</if>
<if test="null!=userNames and userNames.size>0 ">
and create_name in
<foreach collection="userNames" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
</select>
10.字符串数组入参,字符串数组出参:
<select id="selectCountByOrderList" parameterType="java.util.List" resultType="java.lang.String">
select order_id from hs_app_push
<if test="null!=list and list.size>0 ">
where order_id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>
方法:
List<String> selectCountByOrderList(List<String> list);
11.批量插入
<insert id="insertBatchRecord" parameterType="java.util.List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<selectKey keyProperty="detailId" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into hs_excel_account_detail (batch_id, account_id, transaction_date,
transaction_time, borrow_amount, loan_amount,
currency, balance, opposite_account,
opposite_name, digest, remark1,
remark2, create_time, create_name
)
values
<foreach collection ="list" item="item" index= "index" separator =",">
(#{item.batchId,jdbcType=BIGINT}, #{item.accountId,jdbcType=BIGINT}, #{item.transactionDate,jdbcType=VARCHAR},
#{item.transactionTime,jdbcType=VARCHAR}, #{item.borrowAmount,jdbcType=DECIMAL}, #{item.loanAmount,jdbcType=DECIMAL},
#{item.currency,jdbcType=VARCHAR}, #{item.balance,jdbcType=DECIMAL}, #{item.oppositeAccount,jdbcType=VARCHAR},
#{item.oppositeName,jdbcType=VARCHAR}, #{item.digest,jdbcType=VARCHAR}, #{item.remark1,jdbcType=VARCHAR},
#{item.remark2,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.createName,jdbcType=VARCHAR}
)
</foreach>
</insert>
方法:
void insertBatchRecord(List<ExcelAccountDetail> list);
模板:
<?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.shitou.huishi.domain.dataaccess.BusinessMarketAffiliateMapper">
<resultMap id="BaseResultMap" type="com.shitou.huishi.domain.entity.BusinessMarketAffiliate">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="busi_id" jdbcType="INTEGER" property="busiId" />
<result column="data_type" jdbcType="VARCHAR" property="dataType" />
<result column="data_key" jdbcType="INTEGER" property="dataKey" />
<result column="data_value" jdbcType="INTEGER" property="dataValue" />
<result column="create_name" jdbcType="INTEGER" property="createName" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_name" jdbcType="INTEGER" property="updateName" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
</resultMap> <sql id="Base_Column_List">
id, busi_id, data_type, data_key, data_value, create_name, create_time, update_name,
update_time
</sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tb_business_market_affiliate
where id = #{id,jdbcType=INTEGER}
</select> <select id="selectListByLifeLoan" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tb_business_market_affiliate
where data_type='life_of_loan' and busi_id=#{busiId,jdbcType=INTEGER}
</select> <select id="selectListByType" parameterType="com.shitou.huishi.domain.entity.BusinessMarketAffiliate" resultType="java.lang.Integer"> select count() as num from tb_business_market_affiliate
where busi_id=#{busiId,jdbcType=INTEGER}
and data_key=#{dataKey,jdbcType=INTEGER}
and data_type=#{dataType,jdbcType=VARCHAR}
</select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete
from tb_business_market_affiliate
where id = #{id,jdbcType=INTEGER}
</delete> <insert id="insert" parameterType="com.shitou.huishi.domain.entity.BusinessMarketAffiliate">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into tb_business_market_affiliate (busi_id, data_type, data_key,
data_value, create_name, create_time,
update_name, update_time)
values (#{busiId,jdbcType=INTEGER}, #{dataType,jdbcType=VARCHAR}, #{dataKey,jdbcType=INTEGER},
#{dataValue,jdbcType=INTEGER}, #{createName,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
#{updateName,jdbcType=INTEGER}, #{updateTime,jdbcType=TIMESTAMP})
</insert> <insert id="insertSelective" parameterType="com.shitou.huishi.domain.entity.BusinessMarketAffiliate">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into tb_business_market_affiliate
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="busiId != null">
busi_id,
</if>
<if test="dataType != null">
data_type,
</if>
<if test="dataKey != null">
data_key,
</if>
<if test="dataValue != null">
data_value,
</if>
<if test="createName != null">
create_name,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateName != null">
update_name,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="busiId != null">
#{busiId,jdbcType=INTEGER},
</if>
<if test="dataType != null">
#{dataType,jdbcType=VARCHAR},
</if>
<if test="dataKey != null">
#{dataKey,jdbcType=INTEGER},
</if>
<if test="dataValue != null">
#{dataValue,jdbcType=INTEGER},
</if>
<if test="createName != null">
#{createName,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateName != null">
#{updateName,jdbcType=INTEGER},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert> <update id="updateByPrimaryKeySelective" parameterType="com.shitou.huishi.domain.entity.BusinessMarketAffiliate">
update tb_business_market_affiliate
<set>
<if test="busiId != null">
busi_id = #{busiId,jdbcType=INTEGER},
</if>
<if test="dataType != null">
data_type = #{dataType,jdbcType=VARCHAR},
</if>
<if test="dataKey != null">
data_key = #{dataKey,jdbcType=INTEGER},
</if>
<if test="dataValue != null">
data_value = #{dataValue,jdbcType=INTEGER},
</if>
<if test="createName != null">
create_name = #{createName,jdbcType=INTEGER},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateName != null">
update_name = #{updateName,jdbcType=INTEGER},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update> <update id="updateByPrimaryKey" parameterType="com.shitou.huishi.domain.entity.BusinessMarketAffiliate">
update tb_business_market_affiliate
set busi_id = #{busiId,jdbcType=INTEGER},
data_type = #{dataType,jdbcType=VARCHAR},
data_key = #{dataKey,jdbcType=INTEGER},
data_value = #{dataValue,jdbcType=INTEGER},
create_name = #{createName,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_name = #{updateName,jdbcType=INTEGER},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
https://blog.csdn.net/u010448530/article/details/52023256
https://www.2cto.com/database/201505/401604.html
https://blog.csdn.net/qq_35261296/article/details/73559247
https://blog.csdn.net/crystalssj/article/details/76549024
https://www.kunzhao.org/blog/2017/07/23/mybatis/
SpringBoot Mybatis问题收集的更多相关文章
- 基于SpringBoot+MyBatis实现一套电商系统
项目介绍 mall项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现. 前台商城系统包含首页门户.商品推荐.商品搜索.商品展示.购物车.订单流程.会员中心 ...
- 从 0 使用 SpringBoot MyBatis MySQL Redis Elasticsearch打造企业级 RESTful API 项目实战
大家好!这是一门付费视频课程.新课优惠价 699 元,折合每小时 9 元左右,需要朋友的联系爱学啊客服 QQ:3469271680:我们每课程是明码标价的,因为如果售价为现在的 2 倍,然后打 5 折 ...
- Thymeleaf+SpringBoot+Mybatis实现的家庭财务管理系统
项目简介 项目来源于:https://gitee.com/darlingzhangsh/graduation_project 本系统是基于Thymeleaf+SpringBoot+Mybatis.是非 ...
- Thymeleaf+SpringBoot+Mybatis实现的齐贤易游网旅游信息管理系统
项目简介 项目来源于:https://github.com/liuyongfei-1998/root 本系统是基于Thymeleaf+SpringBoot+Mybatis.是非常标准的SSM三大框架( ...
- 第五章 springboot + mybatis(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- 第五章 springboot + mybatis
springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml <!-- 与数 ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
- Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦
个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...
随机推荐
- Android -- 仿淘宝广告条滚动
1,在赶项目的时候我们经常会实现下面这个功能,及添加滚动条广告广播,先看一下淘宝的效果 2,这次实现效果主要使用Android自带的ViewFlipper控件,先来看一下我们的它的基本属性和基本方法吧 ...
- java 中使用ajax调用后台方法注意事项
java 中使用ajax调用后台方法注意事项,后台方法一定要加@ResponseBody jQuery.validator.addMethod("checkRuleName",fu ...
- SVN—使用总结
SVN使用教程总结 为什么要使用SVN? 在程序的编写过程中,每个程序员都会负责开发一个或多个模块,且开发中会生成很多不同的版本, 这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版 ...
- arm cortex-m0plus源码学习(三)GPIO
概述: Cortex-m0的integration_kit提供三个GPIO接口,其中GPIO0传输到外部供用户使用,为EXTGPIO:GPIO1是内核自己的信号,不能乱改,会崩掉:GPIO2是一些中断 ...
- python range函数
这个函数很简单,就不写例子了,看看语法,拿来即用 python range() 函数可创建一个整数列表,一般用在 for 循环中. 函数语法 range(start, stop[, step]) 参数 ...
- impala与hive的比较以及impala的有缺点
最近读的几篇关于impala的文章,这篇良心不错:https://www.biaodianfu.com/impala.html(本文截取部分内容) Impala是Cloudera公司主导开发的新型查询 ...
- SpringMVC中的自定义参数绑定案例
由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适 ...
- 转:C#线程系列讲座(1) BeginInvoke和EndInvoke方法
转载自:http://www.cnblogs.com/levin9/articles/2319248.html 开发语言:C#3.0IDE:Visual Studio 2008本系列教程主要包括如下内 ...
- Lucene 个人领悟 (二)
想了想,还是继续写吧,因为,太无聊了,媳妇儿也还有半个小时才下班. 前面拖拖拉拉用了三篇文章来做铺垫,这一篇开始正经搞了啊. 首先,我要加几个链接 http://www.cnblogs.com/xin ...
- python 闭包和装饰器
python 闭包和装饰器 一.闭包闭包:外部函数FunOut()里面包含一个内部函数FunIn(),并且外部函数返回内部函数的对象FunIn,内部函数存在对外部函数的变量的引用.那么这个内部函数Fu ...