网站建设大致分哪几个板块,漯河做网站,简述商务网站建设,六安哪家做网站不错文章目录 1. 当callSettersOnNullstrue时2. 当callSettersOnNullsfalse时 在mybatis的settings配置参数中有个callSettersOnNulls参数#xff0c;官方解释为#xff1a;指定当结果集中值为 null 的时候是否调用映射对象的 setter#xff08;map 对象时为 put#xff09;方法… 文章目录 1. 当callSettersOnNullstrue时2. 当callSettersOnNullsfalse时 在mybatis的settings配置参数中有个callSettersOnNulls参数官方解释为指定当结果集中值为 null 的时候是否调用映射对象的 settermap 对象时为 put方法这在依赖于 Map.keySet() 或 null 值进行初始化时比较有用。注意基本类型int、boolean 等是不能设置成 null 的。什么意思呢 我们先看下面一段解释不理解没关系等看完下面的代码示例后再回来看一定会理解。 我们假设要把数据库中一条数据映射到java中Map类型的对象上库表字段名为Map中的key库表字段值为Map的value假如库表中某字段为null映射到Map中的key和value是什么样的呢当callSettersOnNullstrue时会把这个字段名映射到key上把null值put到value上当callSettersOnNullsfalse时就不会把这个字段名映射到key上也即Map中根本就不存在这个key当遍历这个Map时就会少了这个key。所以说对于返回类型Map的场景如果在java代码中对于库表字段值为null的也要输出或者处理就要把空的字段也要映射到Map的key和value中。 但是当返回类型为Bean类型时库表中某字段为null当callSettersOnNullstrue会把null值set到Bean对象对应的字段属性上那么该属性值为null当callSettersOnNullsfalse时就不会调set方法就不会把null值set到Bean对象对应的字段属性上那么该字段属性由于没有set内容默认还是null。所以对于返回类型为Bean的场景callSettersOnNulls配置true还是false无所谓。 如果没有理解上面一段话我们看一下下面的案例假设要把库表PERSON中一条数据映射到java的Map对象中。
库表PERSON如下所示
1. 当callSettersOnNullstrue时
mybatis配置如下所示
settingssetting namelogImpl valueSTDOUT_LOGGING/setting namecallSettersOnNulls valuefalse/
/settingsmapper中SQL如下所示假设查询PERSON_ID13的一条数据该条数据的PERSON_SEX列值为NULL。
select idselect2 resultTypeMapselect * from PERSON where PERSON_ID #{personId}
/select测试案例如下所示查询到数据映射到map后然后打印出所有的key和value
public void sqlSessionTest3(){SqlSessionFactory factory mybatisUtil.getFactory();SqlSession sqlSession factory.openSession(true); //true表示自动提交MapString, Object map sqlSession.selectOne(com.lzj.dao.PersonDao.select2, 13);for (String key : map.keySet()){System.out.println(key key);System.out.println(value map.get(key));System.out.println();}sqlSession.close();
}执行结果如下所示发现即使person_sex列为空但map中依然有key为person_sex但value为null说明当callSettersOnNullstrue时把person_sex字段名映射到了key上把null值put到value上。
Logging initialized using class org.apache.ibatis.logging.stdout.StdOutImpl adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 2259527.Preparing: select * from PERSON where PERSON_ID ? Parameters: 13(Integer)Columns: person_id, person_name, person_age, person_sexRow: 13, Jerry, 20, nullTotal: 1
keyperson_sex
valuenullkeyperson_name
valueJerrykeyperson_age
value20keyperson_id
value13Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl227a47]2. 当callSettersOnNullsfalse时
还是以上面的案例所示只不过把配置文件修改成如下所示
settingssetting namelogImpl valueSTDOUT_LOGGING/setting namecallSettersOnNulls valuefalse/
/settings重新运行上面的测试案例输出如下所示输出的map中不再含有名字为person_sex的key说明当callSettersOnNullsfalse时就不会把person_sex字段名映射到key上也即Map中根本就不存在这个key。
Logging initialized using class org.apache.ibatis.logging.stdout.StdOutImpl adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 219192399.Preparing: select * from PERSON where PERSON_ID ? Parameters: 13(Integer)Columns: person_id, person_name, person_age, person_sexRow: 13, Jerry, 20, nullTotal: 1
keyperson_name
valueJerrykeyperson_age
value20keyperson_id
value13Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpld109c4f]注意如果在java应用中如果要遍历库表列对应的所有key时只能配置callSettersOnNullstrue因为如果配置callSettersOnNullsfalse时map中不存在库表列值为NULL对应的key。