让你的 Ibatis2 也支持Annotation

[b][color=Blue]在支持xml配置方式的基础上增加了对annotation的支持,用户可以根据自己的喜好来选择或者两种方式并存。[/color][/b]详情请大家看:[url]http://www.rest4g.org/viewthread.php?tid=12&extra=page%3D1[/url]

代码示例如下:
Account.java[code]
package org.jrest4guice.persistence.ibatis;
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;
}

}
[/code]AccountService.java[code]
package org.jrest4guice.persistence.ibatis;
import java.sql.SQLException;
import java.util.List;
import org.jrest4guice.persistence.ibatis.annotations.Delete;
import org.jrest4guice.persistence.ibatis.annotations.IbatisDao;
import org.jrest4guice.persistence.ibatis.annotations.Insert;
import org.jrest4guice.persistence.ibatis.annotations.Result;
import org.jrest4guice.persistence.ibatis.annotations.ResultMap;
import org.jrest4guice.persistence.ibatis.annotations.Select;
import org.jrest4guice.persistence.ibatis.annotations.Update;
import org.jrest4guice.transaction.annotations.Transactional;
import org.jrest4guice.transaction.annotations.TransactionalType;

import com.google.inject.Inject;
import com.ibatis.sqlmap.client.SqlMapClient;

@IbatisDao
@SuppressWarnings("unchecked")
@Transactional
@ResultMap(id = "accountResultMap", result = {
@Result(property = "id", column = "id"),
@Result(property = "firstName", column = "firstName"),
@Result(property = "lastName", column = "lastName"),
@Result(property = "emailAddress", column = "emailAddress") }, resultClass = Account.class)
@Cachemodel(id = "account-cache", flushInterval = "24", flushOnExecute = {
"insertAccount", "updateAccount", "deleteAccount" }, type = "LRU",
property = { @Property(name = "size", value = "100") })
public class AccountService {
@Inject
private SqlMapClient sqlMapper;

@Select(id = "selectAllAccounts", sql = "select * from ACCOUNT",
resltMap = "accountResultMap", cacheModel = "account-cache")
@Transactional(type = TransactionalType.READOLNY)
public List<Account> findAll() throws SQLException {
return sqlMapper.queryForList("selectAllAccounts");
}

@Select(sql = "select id ,firstName,lastName,emailAddress from "
+ "ACCOUNT where id = #id#")
@Transactional(type = TransactionalType.READOLNY)
public Account getAccountById(int id) throws SQLException {
return (Account) sqlMapper.queryForObject("getAccountById", id);
}

@Insert(id = "insertAccount", sql = "insert into ACCOUNT (id,firstName,"
+ "lastName,emailAddress) values (#id#, #firstName#, #lastName#, "
+ "#emailAddress#)")
public void createAccount(Account account) throws SQLException {
sqlMapper.insert("insertAccount", account);
}

@Update(sql = "update ACCOUNT set firstName = #firstName#,lastName = "
+ "#lastName#,emailAddress = #emailAddress# where id = #id#")
public void updateAccount(Account account) throws SQLException {
sqlMapper.update("updateAccount", account);
}

@Delete(id = "deleteAccount", sql = "delete from ACCOUNT where id = #id#")
public void deleteAccount(int id) throws SQLException {
sqlMapper.delete("deleteAccount", id);
}

@Select(id = "queryAccounts",
sql = "select * from ACCOUNT "
+ "<dynamic prepend=\"where\">"
+ " <isNotNull prepend=\"and\" property=\"firstName\">"
+ " firstName = #firstName#"
+ " </isNotNull>"
+ " <isNotNull prepend=\"and\" property=\"lastName\">"
+ " lastName = #lastName#"
+ " </isNotNull>"
+ " <isNotNull prepend=\"and\" property=\"emailAddress\">"
+ " emailAddress = #emailAddress#"
+ " </isNotNull>"
+ "</dynamic> "
+ "order by lastName", resltMap = "accountResultMap",
cacheModel = "account-cache")
@Transactional(type = TransactionalType.READOLNY)
/**
* 动态SQL查询
*/
public List<Account> queryAccounts(Account account) throws SQLException {
return sqlMapper.queryForList("queryAccounts",account);
}
}
[/code]AccountServiceTest.java[code]
package org.jrest4guice.persistence.ibatis;

import java.sql.SQLException;
import java.util.List;
import junit.framework.Assert;
import org.jrest4guice.guice.GuiceContext;
import org.jrest4guice.guice.PersistenceGuiceContext;
import org.junit.BeforeClass;
import org.junit.Test;

public class AccountServiceTest {
private static AccountService service;

@BeforeClass
public static void setUp() throws Exception {
// 初始化JRest4Guice
PersistenceGuiceContext.getInstance().useIbatis(
"org.jrest4guice.persistence.ibatis").init();
// 获取服务
service = GuiceContext.getInstance().getBean(AccountService.class);
}

@Test
public void doTest() {
List<Account> accounts;
try {
Account account = new Account();
account.setFirstName("张");
account.setLastName("学友");
account.setEmailAddress("jackey@rest4g.org");
// 添加
service.createAccount(account);

account = new Account();
account.setFirstName("刘");
account.setLastName("学友");
account.setEmailAddress("test@rest4g.org");
// 添加
service.createAccount(account);

//查询(按lastName)
Account queryCondition = new Account();
queryCondition.setLastName("学友");
accounts = service.queryAccounts(queryCondition);
Assert.assertEquals(2, accounts.size());

//查询(按firstName和lastName)
queryCondition.setFirstName("张");
accounts = service.queryAccounts(queryCondition);
Assert.assertEquals(1, accounts.size());

// 修改
account = accounts.get(0);
account.setFirstName("何");
service.updateAccount(account);
account = service.getAccountById(account.getId());
Assert.assertNotNull(account);
Assert.assertEquals("何", account.getFirstName());

//查询所有
accounts = service.findAll();
Assert.assertEquals(2, accounts.size());

// 删除
for (Account ac : accounts){
service.deleteAccount(ac.getId());
}

//断言删除的结果
accounts = service.findAll();
Assert.assertEquals(0, accounts.size());
} catch (SQLException e) {
Assert.fail(e.getLocalizedMessage());
}
}
}
[/code]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值