0
点赞
收藏
分享

微信扫一扫

小白跟做江科大32单片机之旋转编码器计次

茗越 2024-06-06 阅读 30

背景

实现过程

  • 缓存我们自定义的sql语句

MappedStatement ms = new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT)
                .resultMaps(new ArrayList<ResultMap>() {
                    {
                        add(new ResultMap.Builder(configuration, "defaultResultMap", resultType, new ArrayList<ResultMapping>(0)).build());
                    }
                })
                .build();
  • 找到我们自定义的sql语句,进行查询
 public List<Map<String, Object>> selectList(String sql, Object value) {
        log.info("selectList sql:{},params:{}", sql, JSONUtil.toJsonStr(value));
        Class<?> parameterType = value != null ? value.getClass() : null;
        String msId = mapperStatementBuilder.selectDynamic(sql, parameterType);
        return sqlSession.selectList(msId, value);
    }

使用说明



 public static void main(String[] args) {


        // 1. 创建SqlSessionFactory 仅作示意,具体情况具体操作
        Configuration configuration = new Configuration();
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

        RowSqlMapper rowSqlMapper = new RowSqlMapper(sqlSessionFactory.openSession());
        List<Map<String, Object>> maps = rowSqlMapper.selectList("select * from ir_session_param");

        HashMap<String, Object> params = new HashMap<>();
        params.put("id", "111");
        List<Map<String, Object>> maps1 = rowSqlMapper.selectList("<script> select * from temp_table where id = #{id} <if test= \"dept_name != null\">" +
                " and dept_name= #{dept_name} </if> </script>", params);
    }

总结

举报

相关推荐

0 条评论