iBatis示例程序

纯粹是为了感受一下JavaEye非常好的代码格式化功能。
这样格式化得太漂亮了。

根据iBatis包中的示例程序修改。
com.mydomain.domain包中
1. Account.java

package com.mydomain.domain;

public class Account {
private int id;
private String firstName;
private String lastName;
private String 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 "id: " + this.getId() + "\nfirstName: " + this.getFirstName()
+ "\nlastName: " + this.getLastName() + "\nemailAddress: "
+ this.getEmailAddress() + "\n";
}
}


com.mydomain.data包中
2. Account.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="Account">
<typeAlias alias="Account" type="com.mydomain.domain.Account" />
<resultMap class="Account" id="AccountResult">
<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 id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
<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 id="insertAccount" parameterClass="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
) values (
#id#,#firstName#,#lastName#,#emailAddress#
)
</insert>
<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 id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ACC_ID = #id#
</delete>
</sqlMap>

3. 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>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/javaee" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="admin888" />
</dataSource>
</transactionManager>
<sqlMap resource="com/mydomain/data/Account.xml" />
</sqlMapConfig>

4. Service.java

package com.mydomain.data;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.mydomain.domain.Account;

public class Service {
private static SqlMapClient sqlMapper;

static {
try {
Reader reader = Resources
.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
throw new RuntimeException("Something bad happend while "
+ "building the SqlMapClient instance." + e, e);
}
}

@SuppressWarnings("unchecked")
public List<Account> selectAllAccount() throws SQLException {
return sqlMapper.queryForList("selectAllAccounts");
}

public Account selectAccountById(int id) throws SQLException {
return (Account) sqlMapper.queryForObject("selectAccountById", id);
}

public void insertAccount(Account account) throws SQLException {
sqlMapper.insert("insertAccount", account);
}

public void updateAccount(Account account) throws SQLException {
sqlMapper.update("updateAccount", account);
}

public void deleteAccount(int id) throws SQLException {
sqlMapper.delete("deleteAccountById", id);
}
}


5. Test.java

package com.mydomain.data;

import java.util.Iterator;
import java.util.List;

import com.mydomain.domain.Account;

public class Test {
private static Service service = new Service();

public static void main(String[] args) throws Exception {
Test test = new Test();
// test.testSelectAccountById(2);
// test.testInsertAccount(44, "aaa", "bbb", "ccc@ddd.com");
// test.testUpdateAccount(34, "Hu", "Jintao", "hujintao@gov.cn");
test.testDeleteAccount(44);
test.testSelectAllAccount();

}

public void testSelectAllAccount() throws Exception {
List<Account> accounts = service.selectAllAccount();
Iterator<Account> it = accounts.iterator();
while (it.hasNext()) {
Account account = (Account) it.next();
System.out.println(account.toString());
}
}

public void testSelectAccountById(int id) throws Exception {
Account account = service.selectAccountById(id);
System.out.println(account.toString());
}

public void testInsertAccount(int id, String firstName, String lastName,
String emailAddress) throws Exception {
Account account = new Account();
account.setId(id);
account.setFirstName(firstName);
account.setLastName(lastName);
account.setEmailAddress(emailAddress);
service.insertAccount(account);
}

/**
* 根据id进行查询,修改其他几个属性,不修改id
* @param id
* @param firstName
* @param lastName
* @param emailAddress
* @throws Exception
*/
public void testUpdateAccount(int id, String firstName, String lastName,
String emailAddress) throws Exception {
Account account = service.selectAccountById(id);
account.setFirstName(firstName);
account.setLastName(lastName);
account.setEmailAddress(emailAddress);
service.updateAccount(account);
}

public void testDeleteAccount(int id) throws Exception {
service.deleteAccount(id);
}
}



6. createDB.sql - MySQL

create table(
ACC_ID bigint,
ACC_FIRST_NAME varchar(20),
ACC_LAST_NAME varchar(20),
ACC_EMAIL varchar(30)
);

这是一个用iBator生成iBatis有关代码的例子。 特别说明: Eclipse应该是3.4.1以上版本, 并且安装了iBator插件。否则可用iBator的命令行版本或ant工具。 测试用数据是: CREATE TABLE PERSON( id INTEGER NOT NULL, firstName VARCHAR (40) NOT NULL, lastName VARCHAR (40) NOT NULL, PRIMARY KEY (ID) ); insert into PERSON values (1,'ng','Huang'); insert into PERSON values (2,'zh','Ni'); insert into PERSON values (3,'zy','Huang'); src下有三个目录: ibator/config:配置文件,其中: ibatorConfig.xml:iBaotr的配置文件,指示iBator如何生成代码,其中classPathEntry要指向一个 jdbc 驱动程序sqlMapConfig.properties:数据库配置,配置数据库密码等 AppSqlConfig.java:应用程序配置,如果目录结构相同,不必修改 SqlMapConfig.xmlSqlMap配置,在最后应该加上每个表的Map文件,特别注意useStatementNamespaces="true"不能为false org:生成的代码,分为三个目录,目录名在ibatorConfig.xml中指定 注意:其中生成的person_SqlMap.xml中的: <select id="ibatorgenerated_selectByPrimaryKey" resultMap="ibatorgenerated_BaseResultMap" > 经过修改了, 删除了parameterClass="org....的内容 test:一个测试主程序。运行它能得到数据库中的数据。 如何使用这个例子: 1、下载后解压缩 2、导入到Eclipse中 3、运行test/Test.java,看看结果 4、删除org及其下的三个目录及文件 5、在ibator/config/ibatorConfig.xml中的右键菜单,选择Generate iBATIS Artifacts 5、将自动生成org中的所有代码,研究一下这些代码 6、修改中的person_SqlMap.xml中的: <select id="ibatorgenerated_selectByPrimaryKey" resultMap="ibatorgenerated_BaseResultMap" > 7、再次执行test/Test.java,看看结果 8、研究ibator/config中的各个文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值