Java 如何在ibatis中有效地映射这一点?
我将ClientDetails设置为bean,结构如下:Java 如何在ibatis中有效地映射这一点?,java,spring,ibatis,Java,Spring,Ibatis,我将ClientDetails设置为bean,结构如下: public class ClientDetails{ protected String id; protected String type; protected AddressDetails; protected PhoneDetails; //getters and setters follows... } public class AddressDetails{ protected
public class ClientDetails{
protected String id;
protected String type;
protected AddressDetails;
protected PhoneDetails;
//getters and setters follows...
}
public class AddressDetails{
protected List<Address> address
//getters and setters follows...
}
public Address{
protected String addressType;
protected String line1;
protected String line2;
protected String line3;
//getters and setters follows...
}
public PhoneDetails{
protected List<PhoneDetail> phoneDetail;
//getters and setters follows...
}
public class PhoneDetail {
protected String type;
protected String phoneNumber;
//getters and setters follows...
}
公共类客户端详细信息{
受保护的字符串id;
保护字符串类型;
受保护的地址详情;
受保护的电话详情;
//接球手和接球手紧随其后。。。
}
公共类地址详细信息{
受保护列表地址
//接球手和接球手紧随其后。。。
}
公共广播{
受保护的字符串地址类型;
保护字符串行1;
受保护的字符串行2;
保护字符串行3;
//接球手和接球手紧随其后。。。
}
公共电话详细信息{
受保护的列表电话详细信息;
//接球手和接球手紧随其后。。。
}
公共类电话详细信息{
保护字符串类型;
受保护的字符串电话号码;
//接球手和接球手紧随其后。。。
}
数据库中的表:
ClientDetails、AddressDetails、PhoneDetails
我将在服务层接收ClientDetails列表,并且必须始终更新ClientDetails表。AddressDetails、PhoneDetails将可选地更新(如果不为空)。如何在ibatis中有效地映射这一点?Akhilesh这是我想到的一种方法(见下文)。我也在学习Ibatis/MyBatis,并逐渐掌握它。我想您希望在单个事务中包含多个插入/更新(考虑性能)?与批处理更新类似,将批处理包装在单个事务中。此示例基于IBatis 2.3.4 在你的刀课上你可以
final SqlMapClient sqlMap = getSqlMapClient();
try {
sqlMap.startTransaction();
sqlMap.startBatch();
for (final ClientDetails clientXXX : clientDetailsList) {
createOrUpdateClientDetails(sqlMapClient, clientXXX);
createOrUpdateClientAddressDetails(sqlMapClient, clientXXX.getAddressDetails());
createOrUpdateOtherXXXDetails(clientXXX.getXXXXX); //have similar private method
}
sqlMap.executeBatch();
sqlMap.commitTransaction();
} catch (final SQLException e) {
throw new XXXException(e);
} finally {
try {
sqlMap.endTransaction();
} catch (SQLException e) {
throw new XXXException(e);
}
}
private void createOrUpdateClientDetails(final SqlMapClient sqlMap, final List<ClientDetails> clientDetails) {
for (final ClientDetails client: clientDetails) {
if (client.getId() != null) {
sqlMap.update("updateClientXXX", client); //your sqlmap method
} else {
sqlMap.insert("createClientXXX", client); //your sqlmap method
}
}
}
final SqlMapClient sqlMap=getSqlMapClient();
试一试{
startTransaction();
sqlMap.startBatch();
用于(最终客户端详细信息客户端XXX:clientDetailsList){
createOrUpdateClientDetails(sqlMapClient,clientXXX);
createOrUpdateClientAddressDetails(sqlMapClient,clientXXX.getAddressDetails());
createorupdateotherxxx详细信息(clientXXX.getXXXXX);//具有类似的私有方法
}
executeBatch();
sqlMap.commitTransaction();
}捕获(最终SQLE异常){
抛出新的XXXException(e);
}最后{
试一试{
endTransaction();
}捕获(SQLE异常){
抛出新的XXXException(e);
}
}
私有void createOrUpdateClientDetails(最终SqlMapClient sqlMap,最终列表clientDetails){
用于(最终客户端详细信息客户端:客户端详细信息){
if(client.getId()!=null){
update(“updateClientXXX”,client);//您的sqlMap方法
}否则{
insert(“createClientXXX”,client);//您的sqlMap方法
}
}
}
但是请注意,无论何时使用一组成批语句,在调用executeBatch()方法之前,都不会生成数据库生成的键。这意味着如果使用selectKey
使用生成的密钥更新对象,它们将返回null如果有任何对象需要新生成的键作为另一个依赖插入的一部分,则可以在startBatch()之前插入此插入/父插入。。例如,AddressDetails和PhoneDetails的客户id是否为FK
我还会再次查看ClientDetails父类,看看是否可以简化它。同样,我不确定这是否是你所追求的方法,但我想分享一下我的想法。谢谢每张桌子都写一把刀是很典型的。谢谢马尔斯的真诚思考。达成一致的第一点是在单个交易中包含插入/更新。当我使用spring框架时,在服务类中注释事务也是如此。我还能使用executeBatch()吗?我正在寻找实现ibatis映射的方法,以及如何编写存储过程,以便在满足最佳性能的情况下将整个ClientDetails列表传递给ibator层results@AkhileshShukla啊,我看到你在使用Spring,我能问一下你在使用Spring和iBatisSQLMaps吗?正如我所建议的Ibatis DAO/Sqlmaps一样,Spring对我来说是新事物,因此我无法回答有关executeBatch()的问题(尽管似乎不会基于我的快速搜索提供批处理方法)。我找到了这两个链接,希望它们对你有用。另请参见(搜索“批处理”)是的,我将Spring与iBATISsqlmaps一起使用。我想executeBatch()将是一件很好的事情,可以尝试并检查您提到的关于主键生成的限制。实际上,我希望spring负责整个事务管理,并关注Sql映射以及如何在此上下文中将域对象传递给存储过程。