惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Vercel News
Vercel News
Google DeepMind News
Google DeepMind News
罗磊的独立博客
WordPress大学
WordPress大学
The Cloudflare Blog
GbyAI
GbyAI
The Register - Security
The Register - Security
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
A
About on SuperTechFans
U
Unit 42
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
Blog — PlanetScale
Blog — PlanetScale
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
T
Tor Project blog
A
Arctic Wolf
H
Hacker News: Front Page
NISL@THU
NISL@THU
F
Full Disclosure
雷峰网
雷峰网
L
LINUX DO - 热门话题
Recent Announcements
Recent Announcements
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
Google Online Security Blog
Google Online Security Blog
I
InfoQ
Webroot Blog
Webroot Blog
S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
博客园 - Franky
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
Latest news
Latest news

博客园 - sleepy

Android 下进行单元测试 Test run failed:Instrumentation run failed due to 'java.lang.ClassNotFoundException' 使用Maven自动部署Tomcat 6和Tomcat 7下Web应用 windows 7下命令行修改用户密码 VMWare Workstation 中Guest OS使用Bridge方式上网故障诊断 CSS guideline java下运行windows命令行程序(批处理文件)示例 - sleepy - 博客园 不用安装Oracle Client如何使用PLSQL Developer 新的 好用的 免费的 功能强大的Oracle 客户端 MySQl客户端访问软件 VPC 2007 SP1下安装Windows虚拟机要点(关键字:VM,Virtual PC, 网络,共享文件,windows 2003, vista, Window XP) tomcat 设置初始内存大小(默认的可是太小了,容易内存溢出) - sleepy - 博客园 测试显示内存使用情况的JSP脚本 在Virtual PC 2007 SP1上安装linux ubuntu 810 如何解决eclipse编辑器,console 汉字显示为乱码?? MySQL内置命令导出数据库 设置MySQL 超时等待时间 Java中访问My SQL如何显示SQL中的汉字 如何高效率修改bug 如何在MySql中记录SQL日志(例如Sql Server Profiler)
iBatis 数据库空值的映射处理
sleepy · 2008-12-25 · via 博客园 - sleepy

上次提到的iBatis关于空值的映射处理,primitive数据类型容易出现此问题,今天服务就因此问题造成无法启动。

我查了一下资料又做了验证,目前有两种方法可解决此问题:

1

使用映射xml的 nullValue 属性(attribute)。例如,下面红色标注的nullValue='0'指当表tb_role中栏目role_Max为数据库空时,设置相应bean对象属性为0;反之如果对象com.hp.hpe.service.dataService.dao.domain.Role的roleMax 属性(property)为0时, 新增/更新数据库时,使用数据库的null值进行更新。

 <resultMap id="result" class="com.hp.hpe.service.dataService.dao.domain.Role">
  <result property="roleId" column="role_id"/>
  <result property="roleName" column="role_name"/>
  <result property="roleMax" column="role_max" nullValue='0'/>
  <result property="roleContext" column="role_context"/>
  <result property="gameRound" column="game_round"/>
 </resultMap>

2 

另一种方法是不需要修改映射文件,修改bean的声明,使用primitive类型对应的封装对象类型。例如: int <--> Integer,此时Java的null 与 DB的null对应。

public Integer getRoleMax() {
  return roleMax;
 }
 public void setRoleMax(Integer roleMax) {
  this.roleMax = roleMax;
 }

当然,修改schema非空也是一种办法,anyway,请大家基于业务选择合适的方法。