Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
下载MySQL5和MySQL5的JDBC驱动:[url]http://dev.mysql.com/downloads/mysql/5.0.html[/url],[url]http://dev.mysql.com/downloads/connector/j/5.1.html[/url]
下载SQLyog Enterprise 6.5,这是MySQL的客户端工具,[url]http://www.fixdown.com/china/Programming/109.htm[/url]
安装MySQL的客户端工具,SQLyog Enterprise 6.5
jdbc.url=jdbc:mysql://localhost:3306/ibatisdb
jdbc.username=root
jdbc.password=leizhimin
ACC_ID bigint not null AUTO_INCREMENT,
ACC_FIRST_NAME varchar(20) default NULL,
ACC_LAST_NAME varchar(30) default NULL,
ACC_EMAIL varchar(30) default NULL,
PRIMARY KEY(ACC_ID)
) ENGINE=MyISAM DEFAULT CHARSET=gbk COMMENT='IBATIS简单测试'
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="jdbc.properties"/>
<settings
cacheModelsEnabled="true"
errorTracingEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="true"/>
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${jdbc.driver}"/>
<property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
<property name="JDBC.Username" value="${jdbc.username}"/>
<property name="JDBC.Password" value="${jdbc.password}"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="com/lavasoft/ibatissut/simple/domain/entity/Account.xml"/>
<!-- List more here...-->
</sqlMapConfig>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Account">
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="Account" type="com.lavasoft.ibatissut.simple.domain.entity.Account"/>
<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly. -->
<resultMap id="AccountResult" class="Account">
<result property="id" column="ACC_ID"/>
<result property="firstName" column="ACC_FIRST_NAME"/>
<result property="lastName" column="ACC_LAST_NAME"/>
<result property="emailAddress" column="ACC_EMAIL"/>
</resultMap>
<!-- Select with no parameters using the result map for Account class. -->
<select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
<!-- A simpler select example without the result map. Note the
aliases to match the properties of the target result class. -->
<select id="selectAccountById" parameterClass="int" resultClass="Account">
select
ACC_ID as id,
ACC_FIRST_NAME as firstName,
ACC_LAST_NAME as lastName,
ACC_EMAIL as emailAddress
from ACCOUNT
where ACC_ID = #id#
</select>
<!-- Insert example, using the Account parameter class -->
<insert id="insertAccount" parameterClass="Account">
insert into account(ACC_ID,ACC_FIRST_NAME,ACC_LAST_NAME,ACC_EMAIL) values(NULL, #firstName#, #lastName#, #emailAddress#)
</insert>
<!-- Update example, using the Account parameter class -->
<update id="updateAccount" parameterClass="Account">
update ACCOUNT set
ACC_FIRST_NAME = #firstName#,
ACC_LAST_NAME = #lastName#,
ACC_EMAIL = #emailAddress#
where
ACC_ID = #id#
</update>
<!-- Delete example, using an integer as the parameter class -->
<delete id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ACC_ID = #id#
</delete>
</sqlMap>
/**
* Created by IntelliJ IDEA.<p>
* User: leizhimin<p>
* Date: 2008-8-16 9:57:03<p>
* 帐户
*/
public class Account {
private int id;
private String firstName;
private String lastName;
private String emailAddress;
public Account() {
}
public Account(String firstName, String lastName, String emailAddress) {
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", emailAddress='" + emailAddress + '\'' +
'}';
}
}
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.lavasoft.ibatissut.simple.domain.entity.Account;
import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;
/**
* Created by IntelliJ IDEA.<p>
* User: leizhimin<p>
* Date: 2008-8-16 9:59:10<p>
* 首先声明这不是最好的例子,但是从这个例子中可以很快的了解iBatis的工作原理,
* 如果需要更好的学习iBatis,可以参看iBatis实现的JPetStore-5.0.zip。
* [url]http://apache.mirror.phpchina.com/ibatis/binaries/ibatis.java/JPetStore-5.0.zip[/url]
*/
public class SimpleExample {
/**
* SqlMapClient的实例是线程安全的,因此仅需要一个实例即可,这里使用了一个静态单例模式。
*/
private static SqlMapClient sqlMapClient;
/**
* 将SqlMapClient的构件放到此不是好注意,应该放到一个单例模式的工具类中,需要的时候随时获取
*/
static {
try {
//读取SqlMapConfig的资源配置
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//构件一个SqlMapClient的实例
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
//关闭输入流
reader.close();
} catch (IOException e) {
throw new RuntimeException("在构件SqlMapClient实例的时候发生了异常!" + e, e);
}
}
public static List selectAllAccounts() throws SQLException {
//返回所有的帐户
return sqlMapClient.queryForList("Account.selectAllAccounts");
}
public static Account selectAccountById(int id) throws SQLException {
return (Account) sqlMapClient.queryForObject("Account.selectAccountById", id);
}
public static void insertAccount(Account account) throws SQLException {
sqlMapClient.insert("Account.insertAccount", account);
}
public static void updateAccount(Account account) throws SQLException {
sqlMapClient.update("Account.updateAccount", account);
}
public static void deleteAccount(int id) throws SQLException {
sqlMapClient.delete("Account.deleteAccount", id);
}
public static void main(String[] args) throws SQLException {
Account act = new Account("1","1","1");
act.setId(23);
insertAccount(act);
List<Account> acclist= selectAllAccounts();
for(Account acc:acclist){
System.out.println(acc);
}
}
}
Account{id=2, firstName='234', lastName='234', emailAddress='23423'}
Account{id=3, firstName='1', lastName='1', emailAddress='1'}
Account{id=4, firstName='1', lastName='1', emailAddress='1'}
Account{id=5, firstName='1', lastName='1', emailAddress='1'}
Process finished with exit code 0