文章目录
- 1. NONE
- 2. WARNING
- 3. FAILING
autoMappingUnknownColumnBehavior是< settings >配置下的属性,该属性是指定发现自动映射目标未知列(或未知属性类型)的行为。就是说当数据库中的字段找不到映射java对象的属性或者与java对象对应属性的类型不匹配时会产生什么样的行为。
autoMappingUnknownColumnBehavior属性可以指定当映射失败时会产生三种行为,默认情况配置的是NONE
- NONE: 不做任何反应;
- WARNING: 输出警告日志(‘org.apache.ibatis.session.AutoMappingUnknownColumnBehavior’ 的日志等级必须设置为 WARN);
- FAILING: 映射失败 (抛出 SqlSessionException)。
下面分别看下三种情况
1. NONE
假设java类User中只有id和name两个属性,而对应数据库表user有三个字段,分别为ID、NAME、AGE,库表和java对象不能完全匹配,看下会发生什么
public class User {private int id;private String name;}
user表
在mybatis配置文件配置autoMappingUnknownColumnBehavior的值为NONE
<settings><setting name="logImpl" value="STDOUT_LOGGING"/><setting name="autoMappingUnknownColumnBehavior" value="NONE"/>
</settings>
下面为mapper文件中对应的SQL
<select id="selectOne" parameterType="int" resultType="com.lzj.bean.User">select * from user where id=#{id}
</select>
执行这个SQL,输出结果如下所示,输出的id=2,name=‘xiaoli’,库表中的AGE字段没有对应的映射属性,丢弃掉了,程序也不会报错。
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.
Cache Hit Ratio [com.lzj.dao.UserDao]: 0.0
Opening JDBC Connection
Created connection 1713847712.
==> Preparing: select * from user where id=?
==> Parameters: 2(Integer)
<== Columns: id, name, age
<== Row: 2, xiaoli, 26
<== Total: 1
输出结果:User{id=2, name='xiaoli'}
2. WARNING
下面看下第二种情况,当autoMappingUnknownColumnBehavior取值WARNING时,修改mybatis的 < settings >配置如下所示
<settings><setting name="logImpl" value="STDOUT_LOGGING"/><setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
</settings>
重新执行上面的SQL,输出结果如下所示,发现也能正常输出User{id=2, name=‘xiaoli’},库表user的AGE列没有找到对应的java对象user的属性,丢弃掉了,程序也没有报错,但在自动映射后打印出了下面一条警告“Unknown column is detected on ‘com.lzj.dao.UserDao.selectOne’ auto-mapping. Mapping parameters are [columnName=age,propertyName=age,propertyType=null]”。所以当autoMappingUnknownColumnBehavior取值WARNING时,也不会报错但会打印出警告(设置的日志级别一定要小于等于warn)。
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.
Cache Hit Ratio [com.lzj.dao.UserDao]: 0.0
Opening JDBC Connection
Created connection 2070438137.
==> Preparing: select * from user where id=?
==> Parameters: 2(Integer)
<== Columns: id, name, age
<== Row: 2, xiaoli, 26
Unknown column is detected on 'com.lzj.dao.UserDao.selectOne' auto-mapping. Mapping parameters are [columnName=age,propertyName=age,propertyType=null]
<== Total: 1
输出结果:User{id=2, name='xiaoli'}
3. FAILING
下面看下最后一种,当autoMappingUnknownColumnBehavior取值FAILING时,看下如果没有完全映射上会发生情况
修改mybatis配置文件如下所示
<settings><setting name="logImpl" value="STDOUT_LOGGING"/><setting name="autoMappingUnknownColumnBehavior" value="FAILING"/>
</settings>
然后重新执行上面的SQL,输出结果如下所示,程序直接抛异常了,说明当库表中列不能映射到java对象中属性上,程序抛异常处理。
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.
Cache Hit Ratio [com.lzj.dao.UserDao]: 0.0
Opening JDBC Connection
Created connection 1617550648.
==> Preparing: select * from user where id=?
==> Parameters: 2(Integer)
<== Columns: id, name, age
<== Row: 2, xiaoli, 26
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.session.SqlSessionException: Unknown column is detected on 'com.lzj.dao.UserDao.selectOne' auto-mapping. Mapping parameters are [columnName=age,propertyName=age,propertyType=null]
### The error may exist in mapper/UserMapper.xml
### The error may involve com.lzj.dao.UserDao.selectOne
### The error occurred while handling results