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(1) 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=1 </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 = #{0} and name = #{1} </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 1=1 <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 1 then (select subject_no from hs_archive_info where archive_id=main_subject_archive_id) when 2 then (select subject_no from hs_archive_info where archive_id=borrower_archive_id) end subject_no, case loan_type when 1 then main_subject when 2 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=1 order by create_time desc ) tb <where> 1=1 <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(1) 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