1. 精讲#{}和${}的区别是什么?
(1)mybatis在处理#{}时,预编译机制会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值。
有效的防止SQL注入
(2)mybatis在处理${}时,就是把${}字符串替换。
${}之所以保留是因为很多场景会出现替换,#{}过于受限
1.1 模糊查询
select * from foo where bar like “%”#{value}”%”
//比如说我们想要进行条件查询,但是几个条件不是每次都要使用,那么我们就可以
//通过判断是否拼接到sql中
<select id="queryById" resultMap="BascResultMap" parameterType="entity">
SELECT * from entity
<where>
<if test="name!=null">
name like concat('%',#{name},'%')
</if>
</where>
</select>
2. 参数是 List 或数组
用来循环容器的标签forEach,查看例子
使用实例: https://www.cnblogs.com/-blog/p/5178106.html
foreach元素的属性主要有item,index,collection,open,separator,close。
- item:集合中元素迭代时的别名,
- index:集合中元素迭代时的索引
- open:常用语where语句中,表示以什么开始,比如以’(‘开始
- separator:表示在每次进行迭代时的分隔符,
- close 常用语where语句中,表示以什么结束,
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
针对最后一条,我们来看一下官方说法:
注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。 所以,不管是多参数还是单参数的list,array类型,都可以封装为map进行传递。如果传递的是一个List,则mybatis会封装为一个list为key,list值为object的map,如果是array,则封装成一个array为key,array的值为object的map,如果自己封装呢,则colloection里放的是自己封装的map里的key值
3.
4. 插入时返回主键
mysql:
<!-- 插入学生 自动主键--> <insert id="createStudent" parameterType="com.model.StudentEntity" keyProperty="studentId"> <selectKey keyProperty="studentId" resultType="String" order="BEFORE"> select nextval('student') </selectKey> INSERT INTO STUDENT_TBL(STUDENT_ID,STUDENT_NAME,STUDENT_SEX, STUDENT_BIRTHDAY, STUDENT_PHOTO, CLASS_ID, PLACE_ID) VALUES (#{studentId}, #{studentName}, #{studentSex}, #{studentBirthday}, #{studentPhoto, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler}, #{classId}, #{placeId}) </insert>
postgresql:
给insert语句添加useGeneratedKeys=”true” keyProperty=”id”
而自动生成的主键值在 insert 方法执行完后可以被设置到传入的参数对象中。
<insert id="insertUserMessage" parameterType="com.xxx.xxx.model.UserMessage"
useGeneratedKeys="true" keyProperty="userMessage.id">
insert into my_news
(orderid,commentid,type,title,content,createtime)
values
(#{userMessage.orderid},#{userMessage.commentid},#{userMessage.type},#{userMessage.title}
,#{userMessage.content},#{userMessage.createtime})
</insert>
5. SQL 复用
通过<include refid="" />
标签引用,refid=”” 中的值指向需要引用的<sql>
中的id=“”属性
<!--定义sql片段-->
<sql id="orderAndItem">
o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<!--引用sql片段-->
<include refid="orderAndItem" />
from ordertable o
join orderitem i on o.orderitem_id = i.orderitem_id
where o.order_id = #{orderId}
</select>
6. 如何解决实体类中的属性名和表中的字段名不同
另一种问法: sql执行结果封装为目标对象并返回的?都有哪些映射形式?
使用别名
<select id="getOrder" parametertype="int" resultetype="cn.mybatis.domain.order"> select order_id id, order_no orderNo ,order_price price form orders where order_id=#{id}; </select>
使用 resultMap ```go
<a name="cm5UB"></a>
# 7. 插入多条数据
```xml
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>
8. 分页插件原理
https://blog.csdn.net/jaryle/article/details/52315565
9. 传入多个参数
1、第一种:DAO层的函数public User selectUser(String name,String area);
对应的xml,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
<select id="selectUser"r esultMap="BaseResultMap">
select * from user_user_t where user_name=#{0} and user_area=#{1}
</select>
2、第二种:使用@param注解
<select id="selectuser" resulttype="user">
select id,username,hashedpassword from some_table
where username=#{username} and hashedpassword=#{hashedpassword}
</select>
10. 批量插入
<insert id="insertBatch">
INSERT INTO t_user
(id, name, del_flag)
VALUES
<foreach collection ="list" item="user" separator =",">
(#{user.id}, #{user.name}, #{user.delFlag})
</foreach >
</insert>
11. 选择标签
//…
//…