> 技术文档 > MyBatis高级应用实战指南

MyBatis高级应用实战指南


MyBatis高级应用实例

以下是MyBatis高级应用实例,涵盖复杂查询、动态SQL、插件开发、缓存优化等场景,帮助深入掌握MyBatis核心技术。

动态SQL构建

Example 1: 多条件动态查询
使用标签实现条件组合:

 SELECT * FROM user  AND name = #{name} AND age = #{age} 

Example 2: 动态更新字段
标签避免全字段更新:

 UPDATE user  name = #{name}, email = #{email}  WHERE id = #{id}

Example 3: 批量插入
处理集合数据:

 INSERT INTO user (name, age) VALUES  (#{item.name}, #{item.age}) 

高级结果映射

Example 4: 一对一关联
嵌套结果映射:

     

Example 5: 一对多嵌套查询
分步加载关联数据:

 

Example 6: 鉴别器映射
根据条件返回不同对象:

  

缓存优化

Example 7: 二级缓存配置
在mapper.xml中启用缓存:

Example 8: 自定义缓存实现
集成Redis缓存:

public class RedisCache implements Cache { // 实现Cache接口方法}

Example 9: 局部缓存禁用
特定语句跳过缓存:

 SELECT * FROM stock_data

插件开发

Example 10: SQL执行时间统计
拦截Executor组件:

@Intercepts(@Signature( type= Executor.class, method=\"query\", args={MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))public class SqlTimerPlugin implements Interceptor { // 实现拦截逻辑}

Example 11: 分页插件
自动改写SQL语句:

StatementHandler handler = (StatementHandler) invocation.getTarget();BoundSql boundSql = handler.getBoundSql();String sql = boundSql.getSql() + \" LIMIT ?,?\";

Example 12: 敏感数据加密
拦截参数处理:

ParameterHandler ph = (ParameterHandler) invocation.getTarget();if (ph.getParameterObject() instanceof User) { User user = (User) ph.getParameterObject(); user.setPassword(encrypt(user.getPassword()));}

复杂类型处理

Example 13: JSON字段映射
自定义TypeHandler:

public class JsonTypeHandler extends BaseTypeHandler { @Override public void setNonNullParameter(PreparedStatement ps, int i, Map parameter, JdbcType jdbcType) { ps.setString(i, JSON.toJSONString(parameter)); }}

Example 14: 枚举类型转换
实现EnumOrdinalTypeHandler:

Example 15: 存储过程调用
使用标签调用:

 {call calculate_bonus(#{empId,jdbcType=INTEGER,mode=IN})}

批量操作优化

Example 16: 批量更新
ExecutorType.BATCH模式:

SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);try { UserMapper mapper = session.getMapper(UserMapper.class); for (User user : users) { mapper.updateUser(user); } session.commit();} finally { session.close();}

Example 17: 游标查询
处理大数据集:

try (Cursor cursor = mapper.scanUsers()) { cursor.forEach(user -> process(user));}

Example 18: 多结果集处理
存储过程返回多个结果:

<select id=\"getMultiResults\" resultSets=\"users,departments\" r