网页建站的费用网络建设招聘

当前位置: 首页 > news >正文

网页建站的费用,网络建设招聘,部门规划书 网站建设,山西大同企业做网站专栏精选 引入Mybatis Mybatis的快速入门 Mybatis的增删改查扩展功能说明 mapper映射的参数和结果 Mybatis复杂类型的结果映射 Mybatis基于注解的结果映射 Mybatis枚举类型处理和类型处理器 再谈动态SQL Mybatis配置入门 Mybatis行为配置之Ⅰ—缓存 Mybatis行为配置…专栏精选 引入Mybatis Mybatis的快速入门 Mybatis的增删改查扩展功能说明 mapper映射的参数和结果 Mybatis复杂类型的结果映射 Mybatis基于注解的结果映射 Mybatis枚举类型处理和类型处理器 再谈动态SQL Mybatis配置入门 Mybatis行为配置之Ⅰ—缓存 Mybatis行为配置之Ⅱ—结果相关配置项说明 文章目录 专栏精选引言摘要正文defaultExecutorTypedefaultStatementTimeoutdefaultResultSetTypesafeRowBoundsEnabledsafeResultHandlerEnabledjdbcTypeForNulldefaultScriptingLanguagedefaultEnumTypeHandlercallSettersOnNullsuseActualParamNamereturnInstanceForEmptyRow 总结 引言 大家好我是奇迹老李一个专注于分享开发经验和基础教程的博主。欢迎来到我的频道这里汇聚了汇集编程技巧、代码示例和技术教程欢迎广大朋友们点赞评论提出意见重要的是点击关注喔 期待在这里与你共同度过美好的时光️。今天要和大家分享的内容是Mybatis行为配置之Ⅲ—其他常用配置项说明。做好准备Let’s go 摘要 在这篇文章中我们将了解剩下的关于Mybatis行为的配置在mybatis项目开发过程中这些配置可能并不常用而且大多数情况下都是使用默认配置了解这些配置的意义可以让我们在解决很多罕见异常问题时有的放矢不至于手忙脚乱。那么准备好开启今天的神奇之旅了吗 正文 今天我们介绍Mybatis中最后几个控制Mybatis行为的配置项它们是 defaultExecutorType 备注配置默认的执行器。 默认值SIMPLE 可选值 值说明对应的Executor实现类SIMPLE普通的执行器,使用JDBC默认StatementSimpleExecutorREUSE预处理语句执行器,使用JDBC的PreparedStatementReuseExecutorBATCH重用语句批量更新执行器BatchExecutor 建议值SIMPLE 建议原因SIMPLE比较通用。如果有特殊需求可以通过 SqlSessionFactoryBuilder#openSession(ExecutorType execType)方法获取到包含对应的Executor的SqlSession。 注Executor保存在 org.apache.ibatis.session.defaults.DefaultSqlSession类中。 批量插入数据的代码示例 public class EnvConfigTest { private SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; Test public void testBatchInsert(){ ListAppTestEntity listnew ArrayList(); AppTestEntityBuilder builder new AppTestEntityBuilder(); builder.setAppName(test-name).setAppCode(test-name-code).setAuthType(1).setCreator(junit); list.add(builder.build()); //省略n个list.add //关键在ExecutorType.BATCH SqlSession session this.sqlSessionFactory.openSession(ExecutorType.BATCH); ApplicationRepository mapper session.getMapper(ApplicationRepository.class); list.stream().forEach(o-{ mapper.addApp(o); }); session.commit(); session.close(); } }defaultStatementTimeout 备注等待数据库响应的秒数。这项配置需要数据库驱动的支持在Mysql中此项配置基本无效在postgres数据库中此配置有效 默认值null 可选值任意正整数 建议值根据实际情况设置 建议原因取决于数据库硬件和配置 Postgres配置下的测试代码 public class PostgresTest { private SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; Before public void before(){ try (InputStream inputStream PostgresTest.class.getResourceAsStream(/mybatis-config.xml)){ this.sqlSessionFactorynew SqlSessionFactoryBuilder().build(inputStream,postgres9); this.sqlSessionthis.sqlSessionFactory.openSession(); } catch (IOException e) { throw new RuntimeException(e); } } After public void after(){ this.sqlSession.clearCache(); this.sqlSession.close(); } //新增十万条数据Test public void testBatchInsert(){ ListAppTestEntity listnew ArrayList(); AppTestEntityBuilder builder new AppTestEntityBuilder(); builder.setAppName(test-name).setAppCode(test-name-code).setAuthType(1).setCreator(junit); for (int i 0; i 100000; i) { AppTestEntity e builder.build(); e.setId(((long) i1)); list.add(e); } //省略n个list.add SqlSession session this.sqlSessionFactory.openSession(ExecutorType.BATCH); ApplicationRepository mapper session.getMapper(ApplicationRepository.class); list.stream().forEach(o-{ mapper.addApp(o); }); session.commit(); session.close(); } //查询测试Test public void testFetchSize(){ SimpleQueryMapper mapper this.sqlSession.getMapper(SimpleQueryMapper.class); long start System.currentTimeMillis(); ListAppTestEntity list mapper.queryList(1); long gap System.currentTimeMillis() - start; System.out.println(selected result size: list.size()); System.out.println(selected time: gap); }
}配置 setting namedefaultFetchSize value1/后的输出 selected result size: 100000 selected time: 8994配置 setting namedefaultFetchSize value1000/后的输出 selected result size: 100000 selected time: 413不配置情况下的输出 selected result size: 100000 selected time: 418细节 这里需要注意此配置会被 fetchSize属性覆盖。fetchSize可以通过以下方式设置 注解的方式 Select(value select * from app_test where auth_type#{type})
Options(fetchSize 1000)
ListAppTestEntity queryList(Param(type) String type);标签的方式 select idqueryList resultTypeAppTestEntity fetchSize1000 select * from app_test where auth_type#{type} /selectdefaultResultSetType 备注指定语句默认的滚动策略 可选值 配置值说明FORWARD_ONLY索引只能向后方滚动不能往前SCROLL_SENSITIVE索引可向前后滚动且更新敏感SCROLL_INSENSITIVE索引可向前后滚动且更新不敏感DEFAULT默认效果和不设置相同 默认值nullDEFAULT 建议值不设置 建议原因不常用此配置是对jdbc的行为控制而在mybatis项目中不会直接操作jdbc safeRowBoundsEnabled 备注是否允许在嵌套语句(子查询)中使用分页APIRowBounds。如果允许使用则设置为 false 默认值false 建议值 建议原因 此配置常见的生效情况是调用了 sqlSession#selectList(String statement,Object param,RowBounds rowBounds)这个方法而参数statement又是嵌套语句如以下测试代码 Test
public void testSafeRow(){ List objects this.sqlSession.selectList(top.sunyog.mybatis.mapper.SimpleQueryMapper.queryAppDetail, 1, new RowBounds(0, 1)); for (Object obj : objects) { System.out.println(obj); }
}此代码在默认配置情况下可以正常输出 AppTestEntity{idnull, appName测试应用1, appCodeceshi, authType1, createDate2023-10-31, creatoradmin, appStatus3, authTypeDictDictTest{dictNameNONE, dictCode1, dictTypeapp_auth_type, dictSort0}, appStatusDictDictTest{dictName正常应用, dictCode3, dictTypeapp_status, dictSort0}, services[ServiceTestEntity{id3, serviceName注册中心, serviceCodenacos-service, servicePath/nacos, appId1}]} 而配置 setting namesafeRowBoundsEnabled valuetrue/后会报错 org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: Mapped Statements with nested result mappings cannot be safely constrained by RowBounds. Use safeRowBoundsEnabledfalse setting to bypass this check.

The error may exist in mapper/SimpleQueryMapper.xml

The error may involve top.sunyog.mybatis.mapper.SimpleQueryMapper.queryAppDetail

The error occurred while handling results

SQL: select t1.* ,t2.dict_code as auth_type_dc,t2.dict_name as auth_type_dn,t2.dict_type as auth_type_dt,t2.dict_sort as auth_type_ds from ( select id,app_name,app_code,auth_type,create_date,creator,app_status from app_test where id? ) t1 left join ( select dict_code,dict_name,dict_type,dict_sort from dict_test where dict_typeapp_auth_type ) t2 on t1.auth_typet2.dict_code

Cause: org.apache.ibatis.executor.ExecutorException: Mapped Statements with nested result mappings cannot be safely constrained by RowBounds. Use safeRowBoundsEnabledfalse setting to bypass this check.safeResultHandlerEnabled

备注是否允许在嵌套语句中使用结果处理器ResultHandler。如果允许使用则设置为 false 默认值true 建议值按实际情况设置 建议原因 结果处理器ResultHandler的使用方法见Mybatis基于注解的结果映射这篇文章 jdbcTypeForNull 备注当没有为参数指定特定的 JDBC 类型时空值的默认 JDBC 类型。 某些数据库驱动需要指定列的 JDBC 类型多数情况直接用一般类型即可比如 NULL、VARCHAR 或 OTHER 可选值org.apache.ibatis.type.JdbcType 常量 默认值OTHER 建议值不设置 建议原因 defaultScriptingLanguage 备注指定动态 SQL 生成使用的默认脚本语言。 默认值org.apache.ibatis.scripting.xmltags.XMLLanguageDriver 建议值不设置 建议原因不常用 defaultEnumTypeHandler 备注指定枚举类型的默认TypeHandler关于枚举类型的TypeHandler使用示例见[[Mybatis基础#枚举类型映射]] 默认值EnumTypeHandler 建议值按实际情况设置建议设置为 EnumOrdinalTypeHandler 建议原因实际应用中枚举类型大多数都是顺序编码的字典值 callSettersOnNulls 备注指定当结果集中值为 null 的时候是否调用映射对象的 settermap 对象时为 put方法 默认值false 建议值true 建议原因返回值中某个字段为null时再map对象中也会插入这个对应的key这样可以减少对map的 containsKey方法操作。 通过以下代码测试设置的行为 Test
public void testNullSet(){ SimpleQueryMapper mapper this.sqlSession.getMapper(SimpleQueryMapper.class); //在这之前先执行这个sql//update app_test set creatornull where id13;ListMapString, Object list mapper.queryMapRes(13); System.out.println(list);
}默认设置时的输出 [{app_nametest-name, auth_type1, id13, create_date2023-11-30, app_codetest-name-code}]配置 setting namecallSettersOnNulls valuetrue/时的输出 [{app_nametest-name, auth_type1, creatornull, id13, create_date2023-11-30, app_codetest-name-code}]useActualParamName 备注允许使用方法签名中的名称作为语句参数名称。 为了使用该特性项目必须采用 Java 8 编译并且加上 -parameters 选项 默认值true 建议值true 建议原因 parameters选项的添加方式为修改maven的pom.xml文件 细节这个插件安装或修改后需要执行maven:clean和maven:compile后才能生效 project …build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId configuration compilerArgs arg-parameters/arg /compilerArgs /configuration /plugin /plugins /build /project新增测试代码 Test
public void testMethodParam(){ SimpleQueryMapper mapper this.sqlSession.getMapper(SimpleQueryMapper.class); ListAppTestEntity list mapper.getAppByStatusAndAuthType(3,1); System.out.println(list);
}public interface SimpleQueryMapper {//注意这里的入参没有添加Param注解ListAppTestEntity getAppByStatusAndAuthType(String status,String authType);
}select idgetAppByStatusAndAuthType resultTypeappTestEntity include refidtop.sunyog.mybatis.mapper.ApplicationRepository.query_column/include where app_status#{status} and auth_type#{authType}
/select配置 setting nameuseActualParamName valuefalse/后执行报错 org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter status not found. Available parameters are [0, 1, param1, param2]

Cause: org.apache.ibatis.binding.BindingException: Parameter status not found. Available parameters are [0, 1, param1, param2]默认配置或 setting nameuseActualParamName valuetrue/配置下打印结果

[AppTestEntity{id1, appName测试应用1, appCodeceshi, authType1, createDate2023-10-31, creatoradmin, appStatus3, authTypeDictnull, appStatusDictnull, servicesnull}]returnInstanceForEmptyRow 备注当返回行的所有列都是空时默认返回 null。 当开启这个设置时MyBatis会返回一个空实例。 请注意它也适用于嵌套的结果集 默认值false 建议值true 建议原因可减少空指针验证 测试代码 public interface SimpleQueryMapper {Select(select name,code from empty_table_test where 11 limit 0,1) MapString, Object queryEmptyMap(int i);
}public class EnvConfigTest {Test public void testReturnEmptyObj(){ SimpleQueryMapper mapper this.sqlSession.getMapper(SimpleQueryMapper.class); MapString,Object entity mapper.queryEmptyMap(112); System.out.println(entity); } }新增可为空的测试表 create table empty_table_test
( name int null, code varchar(32) null
); insert into empty_table_test(name,code) values(null,null),(null,null)默认配置下的输出 null配置 setting namereturnInstanceForEmptyRow valuetrue/时的输出 {}总结 今天介绍的配置项在日常工作中很多都不是很常用但了解这些配置可以减少很多不必要的错误甚至解决一些罕见的异常问题。如 returnInstanceForEmptyRow这个配置能在很大程度上减少空指针异常的出现。 联系方式 邮箱qijilaolifoxmail.com ❗版权声明 本文为原创文章版权归作者所有。未经许可禁止转载。更多内容请访问奇迹老李的博客首页