比方我的java bean 名字叫user 定义三个属性 ID name pwd add
对应数据库的表为 table 列名 ID NAME PWD ADD
当我更新时 我只要根据ID更新 pwd 和add
正常在sql中 写成 update table
set pwd=?
set add=?
where ID=?
我在Ibatis中这样写
update table
set ID=#ID#, NAME=#name#,PWD=#pwd#,ADD=#add#
where ID=#ID#
我只能先在数据库中根据ID查出user 然后根据前台输入获取pwd 和add 并且set到user中,ID和name的值仍是数据库中的值没变,最后通过 这个ibatis语句 把user 给update,我刚学对ibatis机制还是不太了解,不知道这样对不对。??
其实我想问 能不能只设置需要更新的 2个字段,它能不能自动找到java bean中的属性 进行匹配。就像下面:
update table
set PWD=#pwd#,ADD=#add#
where ID=#ID#
这样能不能更新成功呢,谢谢指点~~~!!!
[color=blue]当然可以更新成功,这个就是ibatis半自动化的特点,自由度比较高,你还可以这么写:[/color][code="xml"]
update table
set ID=#ID#
NAME=#name#
PWD=#pwd#
,ADD=#add#
where ID=#ID#
[/code]
[color=blue]
这样想更新什么字段就在JAVA代码中传什么字段,如果字段为空,就不会更新了。[/color]
可以更新成功的,ibatis说白了只是动态生成sql语句,只要你的数据库支持只写2个字段,那么ibatis拼出的sql语句就可以正常执行
sorry
上面代码少了property属性:
[code="java"]
update table
set ID=#ID#
NAME=#name#
PWD=#pwd#
,ADD=#add#
where ID=#ID#
[/code]