mybatis 常用
1、新增时获得主键
<insert ...>
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO ...
</insert>
或
<insert id="insert" parameterType="com.test.User" keyProperty="id" useGeneratedKeys="true" >
INSERT INTO ...
</insert>
2、Boolean判断
<if test="isReturnReport !=null and isReturnReport.toString() == 'true'.toString()">
...
</if>
3、大于、小于符号转义
& &
" "
' '
大于号: > > 或 <![CDATA[ > ]]>
小于号: < < 或 <![CDATA[ < ]]>
eg.
AND DATE_FORMAT(create_date,'%Y-%m-%d') >= #{beginDate, jdbcType=VARCHAR}
AND DATE_FORMAT(create_date,'%Y-%m-%d') <![CDATA[ >= ]]> #{beginDate, jdbcType=VARCHAR}
4、参数为逗号隔开的字符串
FIND_IN_SET(field,param)函数:
AND FIND_IN_SET(user_id,#{_parameter})
_parameter:
获取传入的原参数值,不作预编译处理
5、模糊查询
AND settle_name LIKE CONCAT('%',#{settleName,jdbcType=VARCHAR},'%')
6、级联查询
一对多查询:
一中的xml:
<collection property="optionsList"
column="id"
ofType="com.hs.qmct.app.web.model.question.QuestionOptions"
select="com.hs.qmct.app.web.dao.question.QuestionOptionsMapper.selectByQuestionId">
</collection>
多中的dao:
List<QuestionOptions> selectByQuestionId(Integer questionId);
传递多个参数时:
一中的xml:
column= "{prop1=col1,prop2=col2}"
多中的xml:
<select parameterType="java.util.Map" ....>
select .... where val1=prop1 and val2=prop2
</select>
多对一查询:
一中的xml:
<association property="district"
column="receive_district_id"
javaType="com.hs.qmct.app.web.model.district.District"
select="com.hs.qmct.app.web.dao.district.DistrictMapper.selectByPrimaryKey">
</association>
另一个一中的dao:
District selectByPrimaryKey(Integer id);
7、传参占位符#{}和${}的区别
#{} 具有预编译效果,一般作为条件传参时使用,可防止sql注入
${} 不具有预编译效果,一般在排序中会用到,偶尔也会作为条件传参
eg1.
SELECT id from tables where id=#{id} 解析为:SELECT id from tables where id=?
SELECT id from tables where id='${id}' 解析为:SELECT id from tables where id=原参数值
eg2.
ORDER BY ${param} 此时param是一个列名
ORDER BY #{_parameter} 此时传入的参数有且仅为一个列名
8、批量新增使用forEach
int batchInsert(List<Event> records);
<!--批量新增事件-->
<insert id="batchInsert">
insert into
hserp_sys_event(id, event_name_parent, event_name,event_desc, class_path, interface_path,create_by, create_date)
values
<foreach item="item" index="index" collection="list" separator=",">
(#{item.id}, #{item.eventNameParent}, #{item.eventName},
#{item.eventDesc}, #{item.classPath}, #{item.interfacePath},
#{item.createBy}, sysdate())
</foreach>
on DUPLICATE key update last_update_date = sysdate()
</insert>
传参主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了
9、解决int类型返回报错
IFNULL(MAX(SORT),0)
10、一次执行多条sql
需要在xml中加入
<property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8&allowMultiQueries=true"/>
或在配置文件中加入
hikariConfig.security.jdbcUrl=jdbc:mysql://xx.xx.xx:3306/xxxxx?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
mybatis 常用的更多相关文章
- Mybatis 常用注解
Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...
- MyBatis 常用写法
MyBatis 常用写法 1.forEach 循环 forEach 元素的属性主要有 item, idnex, collection, open, separator, close. collec ...
- Mybatis常用xml
工作中mybatis常用的xml代码 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ma ...
- MyBatis - 常用标签与动态Sql
MyBatis常用标签 ● 定义sql语句:select.insert.delete.update ● 配置JAVA对象属性与查询结构及中列明对应的关系:resultMap ● 控制动态sql拼接:i ...
- Mybatis 常用标签
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- mybatis常用配置
前面两篇博客我们简单介绍了mybatis的使用,但是在mybatis的配置问题上我们只是使用了最基础的配置,本文我们就来说说其他一些常用的配置.如果小伙伴对mybatis尚不了解,可以先参考这两篇博客 ...
- [Mybatis]Mybatis 常用标签及功能整理
Mybatis中生成动态SQL的标签有四类,分别是: if choose (when, otherwise) trim (where, set) foreach 1.if 当需要动态生成where条件 ...
- mybatis常用标签
1. 定义sql语句 1.1 select 标签 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user ...
- MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用
学习框架一个比较好的路径阅读源码.本文介绍的SqlSessionFactory和SqlSession.可以通过了解SqlSessionFactory接口和SqlSession接口以及两个的实现类入手, ...
- [Mybatis]Mybatis常用操作
Mybatis是目前国内比较流行的ORM框架,特点是可以写灵活的SQL语句,非常适合中小企业的面向数据库开发. 本文总结自己开发过程中常用的Mybatis操作. 一.插入操作 主键自增插入单条 < ...
随机推荐
- 使用JavaScript获取URL中的参数(两种方法)
本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧. 方法一: //取url参数 var type = ...
- K8S 部署 ingress-nginx (三) 启用 https
部署 https 证书 cd ~/ingress # 生成私钥 tls.key, 密钥位数是 2048 openssl genrsa -out tls.key 2048 # 使用 tls.key 生成 ...
- jquery 树形导航菜单无限级
转自:http://www.jb51.net/article/71615.htm 侵删<!DOCTYPE html> <html lang="en"> &l ...
- 微信小程序调用地图选取位置后返回信息
先看一下wxml的代码,绑定个事件! <view class='carpool_data_all'> <view class='aa'> <text>*出发地< ...
- django模型基础(三)
本文转载自https://blog.csdn.net/xiaogeldx/article/details/88084034 表关系 一对一(OneToOne) 通过本表的主键外键关联另一张表的主键 创 ...
- bat 批处理获取时间语法格式
bat 批处理获取时间语法格式 取年份:echo %date:~0,4% 取月份:echo %date:~5,2% 取日期:echo %date:~8,2% 取星期:echo %date:~10 ...
- 使用Java实现简单的局域网设备扫描
在产品的使用中我们一般都要设置一个配置环节,这个环节可以设定主机的IP地址等信息,但是这样配置的话使得我们的产品用起来效果不是很好,因此我想到了实现局域网IP扫描的功能,IP局域网扫描是指定IP网段获 ...
- Markdown:常用语法
1.标题 说明:一共可以6级标题,几级几个# 一级标题 #一级标题 2.代码 用前后扩上 Hello World! 3.代码块 用前后扩上 Hello World! 4.加粗 加粗了 **加粗了** ...
- SpringBoot自定义属性配置以及@ConfigurationProperties注解与@Value注解区别
我们可以在application.properties中配置自定义的属性值,为了获取这些值,我们可以使用spring提供的@value注解,还可以使用springboot提供的@Configurati ...
- 关于C#List中FindAll用法的一些简单示例
using System; using System.Collections.Generic; public partial class List : System.Web.UI.Page { pro ...