网站建设开题报告书,嵌入式网站开发,无锡新吴区住房和建设交通局网站,自己公司网站设计背景 在开发过程中遇到一个需求#xff1a;希望从数据库中读取预先定义好的sql#xff0c;根据传参进行参数预处理后执行sql语句。结合项目实际情况我决定使用mybatis执行预定义sql。 实现过程
缓存我们自定义的sql语句 MappedStatement ms new MappedStatement.Builder(co…背景 在开发过程中遇到一个需求希望从数据库中读取预先定义好的sql根据传参进行参数预处理后执行sql语句。结合项目实际情况我决定使用mybatis执行预定义sql。 实现过程
缓存我们自定义的sql语句 MappedStatement ms new MappedStatement.Builder(configuration, msId, sqlSource, SqlCommandType.SELECT).resultMaps(new ArrayListResultMap() {{add(new ResultMap.Builder(configuration, defaultResultMap, resultType, new ArrayListResultMapping(0)).build());}}).build();找到我们自定义的sql语句进行查询 public ListMapString, 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());ListMapString, Object maps rowSqlMapper.selectList(select * from ir_session_param);HashMapString, Object params new HashMap();params.put(id, 111);ListMapString, 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);}总结 文章参考 http://blog.csdn.net/isea533/article/details/40044417感谢大佬的方案。在使用过程中发现问题可以下方评论。如果文章对你有帮助请点赞分享加关注。代码包免费下载