1. 动态SQL
- if标签
| <select id="selectUsersByProperty" resultType="users"> |
| select * from users where 1=1 |
| <if test="userid != 0"> |
| and userid = |
| </if> |
| </select> |
- choose标签
| <select id="selectUsersByChoose" resultType="users"> |
| select * from users where 1=1 |
| <choose> |
| <when test="username != null and usersex!= ''"> |
| and username= |
| </when> |
| <otherwise> |
| and userid=1 |
| </otherwise> |
| </choose> |
| </select> |
- where标签
| // 如果判断条件不为空则自动添加where关键字,并且会自动去掉第一个条件前面的and或or |
| <select id="selectUsersByPropertyWhere" resultType="users"> |
| select * from users |
| <where> |
| <if test="userid != 0"> |
| and userid = |
| </if> |
| <if test="username != null and username != ''"> |
| and username = |
| </if> |
| </where> |
| </select> |
- bind标签
| |
| <select id="selectUsersByLikeName" resultType="users"> |
| <bind name="likeName" value"'%'+name+'%'"/> |
| select * from users where username like #{likeName} |
| </select> |
- set标签
| // 自动去掉最后一个if语句的多余的逗号 |
| <update id="usersUpdate"> |
| update users |
| <set> |
| <if test="username != null and username != ''"> |
| username = |
| </if> |
| <if test="usersex != null and usersex != ''"> |
| usersex = |
| </if> |
| </set> |
| where userid = |
| </update> |
- foreach标签
| <select id="selectUsersByIdUseCollection" resultType="users"> |
| select * from users where userid in |
| <foreach collection="collection" item="userid" open="(" separator="," close=")"> |
| #{userid} |
| </foreach> |
| </select> |
- foreach标签迭代List,Set
| <select id="selectUsersByIdUserCollection" resultType="users"> |
| select * from users where userid in |
| <foreach collection="collection" item="userid" open="(" separator="," close=")"> |
| #{userid} |
| </foreach> |
| </select> |
- foreach标签迭代array
| <select id="selectUsersByIdUseArray" resultType="users"> |
| select * from users where userid in |
| <foreach collection="array" item="userid" open="(" separator="," close=")"> |
| #{userid} |
| </foreach> |
| </select> |
- 迭代Map
| <select id="selectUsersCount" resultType="int"> |
| select count(*) from users where |
| <foreach collection="suibian" separator="and" item="value" index="key"> |
| ${key} = #{value} |
| </foreach> |
| </select> |
- 使用foreach标签完成批量添加
| <insert id="insertUsersBatch"> |
| insert into users values |
| <foreach collection="collection" item="user" separator=","> |
| (default, |
| </foreach> |
| </insert> |
2. Mybatis缓存
Mybatis会将相同查询条件的SQL语句的查询结果存储在内存或者某种缓存介质当中,当下次遇到相同的查询SQL时候不在执行该SQL,
而是直接从缓存中获取结果,减少服务器的压力,尤其是在查询越多、缓存命中率越高的情况下,使用缓存对性能的提高明显。
1. 一级缓存(默认开启)
如何判断两次查询是完全相同的查询?
mybatis认为,对于两次查询,如果以下条件都完全一样,那么就认为它们是完全相同的两次查询:
- 传入的statementId
- 查询时要求的结果集中的结果范围
- 这次查询所产生的最终要传递给Preparedstatement的sql语句字符串
- 传递的参数值
2. 二级缓存
mybatis的二级缓存是application级别的缓存,它可以提高对数据库查询的效率,以提高应用的性能。。
二级缓存是sqlSessionFactory上的缓存,可以是由一个SqlSessionFactory创建的不同的SqlSession之间共享缓存数据。
SqlSession在执行commit()或者close()的时候将数据放入到二级缓存。
- 实体类必须是可序列化的
- 映射语句文件中的所有select语句将会被缓存
- 二级缓存是以namespace为单位的,不同namespace下的操作互不影响
- 如果在加入标签的前提下让个别select元素不使用缓存,可以使用useCache属性,设置为false
- 缓存会使用默认的 Least Recently Used(LRU,最近最少使用的)算法来收回。
- 根据时间表,比如 No Flush Interval,(CNFI 没有刷新间隔),缓存不会以任何时间顺序 来刷新。
- 缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用
- 缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全的被调用者修改,不干扰其他调用者或线程所做的潜在修改
| 1. 在映射配置文件中添加<cache/> |
| 2. JavaBean对象必须实现序列化接口 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?