原创作者: 饕尽天下
阅读:7706次
评论:5条
更新时间:2011-05-26
对任何数据库程序来说,添加,更新,删除都是十分常见且必要的特性.在该教程里我们已经讲解了使用iBatis在Java中进行插入和删除操作,现在本章将讲述如何使用iBatis在数据表中更新数据.在iBatis中执行一条更新语句是非常简单的.为了更新数据你得在SQL映射文件"Contact.xml"中添加SQL的update语句.
iBatis更新语句例子 :
Contact.java
public class Contact {
private String firstName;
private String lastName;
private String email;
private int id;
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
iBatis更新查询
在我们的例子中,我们通过参数中指定的id更新了表中的数据,因此对于"id"我们将"parameterClass"的属性值分配为"long".
Contact.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Contact">
<!--- Showing all data of table -->
<select id="getAll" resultClass="Contact">
select * from contact
</select>
<!--- Update data of Contact table -->
<update id="updateById" parameterClass="long">
update Contact
set
lastName = 'Raghuwanshi'
where
id=#id#
</update>
</sqlMap>
现在我们可以在Java程序中通过如下代码执行更新操作了 :
sqlMap.update("Contact.updateById",contactId);
IbatisUpdate.java代码如下 :
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class IbatisUpdate{
public static void main(String[] args)
throws IOException,SQLException{
Reader reader = Resources.getResourceAsReader(
"SqlMapConfig.xml"
);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
//Updating one record of contact
System.out.println("*---- Updating informations of Contact -----*");
Contact contct=new Contact();
long contactId=1;
sqlMap.update("Contact.updateById",contactId);
System.out.println("|Updated Record Successfully ");
System.out.println("All Contacts");
List<Contact> contacts = (List<Contact>)
sqlMap.queryForList("Contact.getAll",null);
Contact contact = null;
for (Contact c : contacts) {
System.out.print(" " + c.getId());
System.out.print(" " + c.getFirstName());
System.out.print(" " + c.getLastName());
System.out.print(" " + c.getEmail());
contact = c;
System.out.println("");
}
System.out.println("============================================");
}
}
为了执行update的例子,遵照如下步骤 :
创建Contact.java和SqlMapConfig.xml
编译Contact.java
创建Contact.xml
创建IbatisUpdate.java并将其编译
执行IbatisUpdate类文件,你会得到如下输出 :
5 楼 zhangpeili 2012-11-09 21:40
4 楼 lovefly_zero 2010-10-22 11:44
3 楼 梦回下花园 2010-03-04 16:40
2 楼 zhanger 2009-12-19 08:41
这个本来就是可以的啊,<update />是可以传递你要使用的参数的
1 楼 carrera 2009-08-24 19:38