原创作者: 饕尽天下
阅读:9534次
评论:4条
更新时间:2011-05-26
iBatis最棒的特点就是它的简洁,这也是唯一令它在任何数据库程序中更容易使用的原因.iBatis使得通过Java或者任何其它的Microsoft的程序来使用数据库变得非常简单.本章我们将会通过一个例子向你介绍如何向数据库插入一行数据.我们使用MySQL作为本例的数据库,和我们上一章中使用的是一样的.这是"Contact"表和我们上一章使用过的两个文件:"Contact.java"和"SqlMapConfig.xml"
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>
我们使用<insert>标签来映射SQL语句,在该标签中我们定义了一个"id",它将在IbatisInsertion.java文件中用来执行数据库插入,查询操作.
<selectKey resultClass="int" keyProperty="id">
select last_insert_id() as id
</selectKey>
上面的代码指出了表中被插入数据的下一行.
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">
<!--- Inserting data in table -->
<insert id="insert" parameterClass="Contact">
insert into contact (firstName,lastName,email)
values (#firstName#, #lastName#, #email#)
<selectKey resultClass="int" keyProperty="id">
select last_insert_id() as id
</selectKey>
</insert>
<!--- Showing all data of table -->
<select id="getAll" resultClass="Contact">
select * from contact
</select>
</sqlMap>
IbatisInsertion.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 IbatisInsertion{
public static void main(String[] args) throws IOException,SQLException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
//Inserting one record in contacts
System.out.println(
"*-------------- Inserting information in Contact Table -----------*");
Contact contact=new Contact("Amit","Kumar","amit@roseindia.net");
sqlMap.insert("Contact.insert",contact);
System.out.println("|Record Inserted Successfully ");
System.out.println("All Contacts");
List<Contact> contacts = (List<Contact>)
sqlMap.queryForList("Contact.getAll",null);
Contact contactall = new Contact();
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("===============================================");
}
}
如何执行本例 :
1.创建Contact.java并将其编译
2.创建Contact.xml和SqlMapConfig.xml
3.创建IbatisInsertion.java
4.执行IbatisInsertion类文件,结果将在你的命令提示符上输出如下 :
"Record Inserted Successfully"
输出 :
4 楼 光亮花 2012-08-24 17:10
</selectKey>
中的last_insert_id()是什么啊?
3 楼 quyang 2012-07-06 10:08
2 楼 Checkmate 2009-03-16 19:43
...诶...最简单java代码也要这么多...
自己动手试试吧,其实代码还是很少的.
这系列教程非常的基础
1 楼 chenjinlai 2009-03-13 16:22