我希望通过上面的例子,你能完全懂得如何向数据库执行插入或者查询操作.所以在本例中你将学习到如何通过iBatis在数据库中删除数据.所以你需要分析代码并清楚的理解在这些代码里到底发生了什么.然而你绝对不需要再创建一个不同的数据库,虽然你知道我们使用上一个MySQL作为数据库而且你已经知道了我们的表名是Contact.但你可以选择是使用这个数据库还是再创建一个,这都由你决定!你唯一需要确定的就是你定义的表名是正确的,否则将会产生Bug.如果你从本iBatis的开始学下来的,那么你是不需要修改代码的.仅仅将给定的代码拷贝到文件夹并执行,最终删除数据库表中的数据.
正如我之前提到的,在iBatis的本章,我们将要从Ctract表中删除记录,我们使用MySQL的数据库"vin"
我们的Contact.java和SqlMapConfig.xm是与上一个例子中的是一样的.
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>
在Contract.xml文件中我们使用<delete>标签删除Contract表中的全部记录.
<delete id="deleteAll" parameterClass="Contact">
delete from Contact
</delete>
上面几行代码删除了Contract表中的所有记录,这里定义的id"deleteAll"会在IbatisDeletion类中执行数据库的查询操作.
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">
<!--- Delete data from Contact table -->
<delete id="deleteAll" parameterClass="Contact">
delete from Contact
</delete>
<!--- Showing all data of table -->
<select id="getAll" resultClass="Contact">
select * from contact
</select>
</sqlMap>
我们需要引入下面的包 :
com.ibatis.common.resources
com.ibatis.sqlmap.client
SQL映射所需的类和接口 :
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
上面的代码能从"SqlMapConfig.xml"中读取配置信息, IbatisDeletion.java的代码如下 :
IbatisDeletion.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 IbatisDeletion{
public static void main(String[] args)
throws IOException,SQLException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
//Deleting all records from contacts
System.out.println("*-- Deleting informations from Contact------*");
Contact contct=new Contact();
sqlMap.delete("Contact.deleteAll",contct);
System.out.println("|Deleted 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("============================================");
}
}
按照如下步骤执行本例 :
创建Contact.xml和SqlMapConfig.xml
创建Contract.java并将其编译
创建IbatisDeletion.java并将其编译
执行IbatisDeletion你将会在你的命令提示符中得到如下输出 :
3 楼 zhangpeili 2012-11-09 21:40
2 楼 抉择2011 2012-02-10 17:35
1 楼 p06012146 2010-10-14 16:28