Mabatis,参考手册:
https://www.kancloud.cn/digest/andyalien-mybatis/190190

1 Mybatis 不能直接写SQL的小/大于号< > ,不然会报错:The content of elements must consist of well-formed character data or markup
要变成

 &            &amp;<            &lt;>            &gt;"             &quot;'              &apos;例如a小于等等b(a<=b) 在xml文件就要:  a &lt;= b

注意不等于的写法:
a <>b 在XML中就要:a <![CDATA[ <> ]]>b

2 # 和$的区别

解析出来会带“”,而$解析出来不带

所以Limit 不能用 LIMIT #{topValue},要LIMIT topValue才可以。参数传的是String,不能用{topValue}才可以。 参数传的是String,不能用topValue才可以。参数传的是String,不能用,不然会报错,如我修改的这个BUG

注意事项
1、能使用#{}的地方应尽量使用#{}
2、#{}可以有效防止sql注入,像PreparedStatement ,,${}则可能导致sql注入成功。
例如用户输入用户名admin和密码123456’ or ‘1’ = ‘1’,那么拼接出来的语句就为
select * from user where name=’ admin ’ and password=‘123456’ or ‘1’= ‘1’;
这能够查询所有,这就出问题了。

3 mybatis返回集与传参,resultMap resultType parameterType
resultMap 和resultType 的区别,resultMap 定义好的实体类映射,resultType 表示返回的数据类型,常用的如Map ,Integer等等。
resultMap :

<resultMap type="EqptData" id="EqptDataResult"><result property="dataId"    column="data_id"    /><result property="equipmentId"    column="equipment_id"    /><result property="equipmentDate"    column="equipment_date"    /><result property="status"    column="status"    /><result property="collectDate"    column="collect_date"    /><result property="remark"    column="remark"    /><result property="statusStr"    column="status_str"    /><result property="employeeNumber"    column="employee_number"    /></resultMap><sql id="selectEqptDataVo">SELECTdata_id,equipment_id,equipment_date,STATUS,collect_date,remark,status_str,employee_numberFROMeqpt_data</sql><select id="selectEqptDataList" parameterType="EqptData" resultMap="EqptDataResult"><include refid="selectEqptDataVo"/><where>  <if test="dataId != null "> and data_id = #{dataId}</if><if test="equipmentId != null "> and equipment_id = #{equipmentId}</if><if test="equipmentDate != null "> and equipment_date = #{equipmentDate}</if><if test="status != null  and status != '' "> and status = #{status}</if><if test="collectDate != null "> and collect_date = #{collectDate}</if><if test="remark != null  and remark != '' "> and remark = #{remark}</if><if test="statusStr != null  and statusStr != '' "> and status_str = #{statusStr}</if><if test="employeeNumber != null  and employeeNumber != '' "> and employee_number = #{employeeNumber}</if></where><if test="counts != null"><![CDATA[   LIMIT ${counts}]]></if></select>

resultType :

 <select id="selectEqptCount" resultType="Integer">SELECT  count(1) fromeqpt_data  <if test="partitionTime != null">partition (${partitionTime})</if> a LEFT JOIN eqpt_equipment b on  a.equipment_id = b.equipment_idLEFT JOIN eqpt_equipment_type c on b.equipment_type_id = c.equipment_type_id<where>b.del_flag = 0<if test="variety != null"> and c.variety = #{variety}</if><if test="deptId != null and deptId != ''">and (b.dept_id = #{deptId} or  b.dept_id IN ( SELECT    t.dept_id FROM sys_dept t WHERE FIND_IN_SET(#{deptId},ancestors))) </if> <choose><when test='status == "0"'>and a.status  = 151</when><when test='status == "00"'>and a.status = 100</when></choose><if test="equipmentTypeValue != null "> and c.equipment_type_value = #{equipmentTypeValue}</if><if test='attributeValue == "0"'>and a.collect_date &gt;= (NOW() - interval 12 hour)</if><if test='attributeValue == "1"'>and a.collect_date &gt;= (NOW() - interval 24 hour)</if><if test='attributeValue == "2"'>and a.collect_date &gt;= DATE_SUB(CURDATE(), INTERVAL 6 DAY)</if><if test='attributeValue == "3"'>and a.collect_date &gt; DATE_SUB(CURDATE(), INTERVAL 30 DAY)</if><if test='attributeValue == "4"'>and a.collect_date &gt; DATE_SUB(CURDATE(), INTERVAL 90 DAY)</if><if test='attributeValue == "5"'>and a.collect_date &gt; DATE_SUB(CURDATE(), INTERVAL 180 DAY)</if><if test='attributeValue == "6"'>and a.collect_date &gt; DATE_SUB(CURDATE(), INTERVAL 365 DAY)</if><if test='time == "taday"'>and TO_DAYS(a.collect_date)=TO_DAYS(NOW())</if></where></select>

parameterType 代表传参的数据类型,如map
从controller到xml

/*** 测试查询数据是否正常*/@PostMapping("/newEvnAllData")@ResponseBodypublic TableDataInfo newEvnAllData(@RequestParam Map<String, Object> paramMap) {paramMap.put("topValue", 20);paramMap.put("status", Global.getEquipmentOffLine());//不查离线的数据List<?> simpleSelectEvnAllData = iEnvService.simpleSelectEvnAllData(paramMap);logger.info("simpleSelectEvnAllData == " + JSON.toJSONString(simpleSelectEvnAllData));return getDataTable(simpleSelectEvnAllData);}

传参和返回值都是Map,这样的可以不用定义实体类,这样的用法比较灵活

 <select id="simpleSelectEvnAllData" parameterType="Map"  resultType="Map">SELECT equipmentEncod,equipmentName,equipmentTypeCode,aisle,measureType,measureData,equipmentLocation,g.status,h.status_name statusType,remark,g.data_id dataId,collectDate FROM(  SELECT a.data_id,c.equipment_encod equipmentEncod,c.equipment_name equipmentName,CASE d.equipment_type_id   WHEN '84' THEN e.measure_type ELSE b.measure_type END  measureType,CASE d.equipment_type_id   WHEN '84' THEN e.aisle ELSE b.aisle   END aisle,concat(CASE d.equipment_type_id   WHEN '84' THEN e.particles ELSE b.measure_data    END,ifNULL(i.css_class,' ')) measureData,a.collect_date collectDate,d.equipment_type_value equipmentTypeCode,a.status,c.equipment_location equipmentLocation,f.status_name remarkFROM (SELECT collect_date ,STATUS,equipment_id,data_id FROM eqpt_data ORDER BY data_id DESC  LIMIT 100) aLEFT JOIN eqpt_environment_data b ON a.data_id= b.data_id LEFT JOIN eqpt_equipment c ON a.equipment_id = c.equipment_idLEFT JOIN eqpt_equipment_type d ON c.equipment_type_id = d.equipment_type_idLEFT JOIN eqpt_particles_data e ON e.data_id = a.data_idLEFT JOIN state_dictionary f ON f.id=e.state_dictionary_id OR f.id=b.state_dictionary_idLEFT JOIN sys_dict_data i on b.measure_type = i.dict_value or e.measure_type= i.dict_value AND i.dict_type = 'eqpt_measure_type' WHERE  d.variety = 1 AND  c.del_flag = 0 AND d.del_flag = 0  <if test="status != null and status != ''">and a.STATUS <![CDATA[ <> ]]> #{status} and f.status_switching_type not in (SELECT status_key FROM eqpt_status_switching WHERE (status_switching_id = #{status} or  status_switching_id  IN ( SELECT    t.status_switching_id FROM eqpt_status_switching t WHERE FIND_IN_SET(#{status},ancestors))))</if><if test="deptId != null and deptId != ''">and (c.dept_id = #{deptId} or  c.dept_id IN ( SELECT    t.dept_id FROM sys_dept t WHERE FIND_IN_SET(#{deptId},ancestors))) </if>ORDER BY data_id DESCLIMIT ${topValue})g  LEFT JOIN eqpt_status_switching h ON g.status=h.status_switching_id</select>

5 动态SQL,加LIMIT的方式

<![CDATA[   LIMIT  ${topValue}]]>

Mybatis的灵活运用及注意事项相关推荐

  1. mybatis配置文件解析错误解决方法

    2019独角兽企业重金招聘Python工程师标准>>> 转:mybatis写mapper文件注意事项 mybatis3 mapper cdata mybatis3 null xml中 ...

  2. case mybatis 不同表_解决mybatis case when 报错的问题

    在mybatis中使用case when进行条件筛选判断时遇到 Failed to process, please exclude the tableName or statementId. 这样的报 ...

  3. 双剑合璧————Spring Boot + Mybatis Plus

    引言 最近在学习Mybatis Plus的使用,希望通过spring boot快速将mybatis plus整合进来. 对于springboot项目,mybatis plus团队也有自己的启动器 :m ...

  4. mybatis 中case_解决mybatis case when 报错的问题

    在mybatis中使用case when进行条件筛选判断时遇到 Failed to process, please exclude the tableName or statementId. 这样的报 ...

  5. java 文件编码 查询系统_javaweb垃圾分类查询系统、ssm+mysql

    需求分析 基于SSM实现一个垃圾分类查询管理系统, 用户可以根据自定义查询分类信息, 管理员可以对分类信息, 垃圾详情信息进行增删改查的管理 本站提供了其他类型的 垃圾分类管理系统源代码 点击查看 运 ...

  6. 可用性测试的权衡之道

    对于可用性测试,业内人士存在一些普遍认可的原则.它们神圣地如同自然科学里的理论,似乎我们只能对其言听计从.俯首称臣才能践行出"好的可用性测试".其实,即便是科学,它的一个特征也是& ...

  7. 用单链表编程实现一个简易的高校学籍管理系统_SSM框架实现学生学籍管理系统...

    基于Spring, SpringMVC, Mybatis 框架实现一个学生学籍管理系统, 能够对学生基本信息, 比如姓名年龄形变状态进行管理, 也能进行通讯录维护, 并能导出Excel报表 运行环境 ...

  8. 网上花店java项目_Java+SSM实现网上花店售卖系统

    需求分析 基于Spring, SpringMVC, Mybatis实现一个网上花店售卖系统, 普通游客能够浏览并查看鲜花类商品并进行完整的购物车操作, 结算购买,该网上花店售卖系统采用java进行开发 ...

  9. 基于java的车辆维护系统设计_基于SSM车辆维修管理系统-JavaWeb汽车保养管理系统...

    需求分析 基于ssm框架实现一个车辆维修管理系统(java+springmvc+mybatis+mysql),能够实现用户信息管理.车辆信息管理.故障信息管理.维修订单管理.零件管理.统计管理等功能. ...

  10. 免费分享20套微信小程序源码 源码免费下载【强烈推荐】

    淘源码:国内知名的源码免费下载平台 微信小程序源码包括:商城系统源码.点餐外卖源码.垃圾分类识别源码.预约洗车源码.物业管理源码.校园跑腿源码.驾考学习源码.会议预约源码.图书管理源码.智能停车源码. ...

最新文章

  1. C++_STL——array(C++11)
  2. 谁说程序员干不过写ppt的,码农就该这样雄起
  3. python写一个表白程序_用Python写一个能算出自己年龄的小程序
  4. V8 引擎是如何工作的?
  5. linux之pkill命令
  6. CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
  7. WOW战斗逻辑系统分析
  8. python中not是什么意思_python中的not具体使用及意思
  9. Linux驱动里的wmb函数
  10. 用Python写爬虫代码,记录斗鱼主播的热度变化情况,并绘制热度变化图。
  11. 计算机无法共享访问受限,设置组策略解决共享访问受限问题
  12. 图片批量下载 +图片马赛克:多张图片组成端午安康!
  13. MYSQL之错误代码----mysql错误代码与JAVA实现
  14. systemtap工具使用介绍
  15. 一二, Spark概述和快速入门
  16. 从零开始学习网络数据包分析:科来抓包的入门教程
  17. HTTPS的工作原理
  18. 收集的一些名人经典语录
  19. 等概率情况下查找成功时的平均查找长度
  20. WMS系统功能分析-出库、库管

热门文章

  1. html选择器优先级如何计算,CSS选择器权重计算与优先级
  2. 计算机组成原理符号标志,计算机组成原理之数据的表示及运算
  3. SIPC的认证算法java实现
  4. LTE入门之UE-Category
  5. 计算机中模拟和数字信号,模拟信号和数字信号的区别是什么
  6. iOS音乐播放器(歌词自动滚动)
  7. CC2652RB硬件I2C读取FXOS8700CQ加速度传感器
  8. java学习网络编程遇到异常java.net.SocketException:Software caused connection abort: socket write error
  9. 怎么在运行上面看域名服务器,域名dns服务器查询方法是什么?如何查看dns服务器地址...
  10. 批处理版音视频播放器(甲兵时代原创批处理)(上)