标签:style blog http java color os
问题:spring的注解功能@autowired,默认是以type也就是类型匹配的,但是同一个类对应不同名字对象的情况,会无法确定是哪个类的对象。
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="tbhyJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="tbhyDataSource"/> </bean>
可以在@autowired后+@Qualifier("jdbcTemplate")
声明变量的时候,可以通过名字注入。
private JdbcTemplate jdbcTemplate;
private JdbcTemplate tbhyJdbcTemplate;
问题: transactionTemplate的用法。
package com.taobao.rrplatform.dao.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.taobao.rrplatform.dao.TbhyDAO; import com.taobao.rrplatform.domain.TbhyScenarioSettingDomain; import com.taobao.rrplatform.domain.TbhyTabSetingDomain; import com.taobao.rrplatform.domain.TbhyTopicSettingDomain; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 淘宝行业平台场景数据访问DAO * Created by goofy on 2014/7/11. */ public class TbhyDAOImpl extends SqlMapClientDaoSupport implements TbhyDAO { @Resource private TransactionTemplate transactionTemplate; @Override public TbhyScenarioSettingDomain get(long sceneId) { TbhyScenarioSettingDomain query = new TbhyScenarioSettingDomain(); query.setSceneId(sceneId); TbhyScenarioSettingDomain scene = (TbhyScenarioSettingDomain) getSqlMapClientTemplate().queryForObject("tbhyScenarioSetting.select", query); TbhyTopicSettingDomain topicQuery = new TbhyTopicSettingDomain(); topicQuery.setScenarioId(scene.getId()); List<TbhyTopicSettingDomain> topics = getSqlMapClientTemplate().queryForList("tbhyTopicSetting.select", topicQuery); scene.setTopicList(topics); for (TbhyTopicSettingDomain topic : topics) { TbhyTabSetingDomain tabQuery = new TbhyTabSetingDomain(); tabQuery.setTopicId(topic.getId()); List<TbhyTabSetingDomain> tabs = getSqlMapClientTemplate().queryForList("tbhyTabSeting.select", tabQuery); topic.setTabList(tabs); } return scene; } @Override public TbhyScenarioSettingDomain add(final TbhyScenarioSettingDomain domain) { return (TbhyScenarioSettingDomain) transactionTemplate.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { Long id = (Long) getSqlMapClientTemplate().insert("tbhyScenarioSetting.insert", domain); domain.setId(id); addSceneTopicsAndTabs(domain); return domain; } }); } /** * 插入场景的小主题和小主题下的tab信息 * * @param domain */ private void addSceneTopicsAndTabs(TbhyScenarioSettingDomain domain) { for (TbhyTopicSettingDomain topic : domain.getTopicList()) { topic.setScenarioId(domain.getId()); Long topicId = (Long) getSqlMapClientTemplate().insert("tbhyTopicSetting.insert", topic); topic.setId(topicId); for (TbhyTabSetingDomain tab : topic.getTabList()) { tab.setTopicId(topicId); tab.setId((Long) getSqlMapClientTemplate().insert("tbhyTabSeting.insert", tab)); } } } @Override public boolean delete(final long sceneId) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { TbhyScenarioSettingDomain scene = get(sceneId); getSqlMapClientTemplate().delete("tbhyScenarioSetting.delete", scene.getId()); deleteSceneTopicsAndTabs(scene); } }); return true; } /** * 删除场景的小主题和小主题下的tab信息 * * @param scene */ private void deleteSceneTopicsAndTabs(TbhyScenarioSettingDomain scene) { for (TbhyTopicSettingDomain topic : scene.getTopicList()) { getSqlMapClientTemplate().delete("tbhyTopicSetting.delete", topic); for (TbhyTabSetingDomain tab : topic.getTabList()) { getSqlMapClientTemplate().delete("tbhyTabSeting.delete", tab); } } } @Override public TbhyScenarioSettingDomain update(final TbhyScenarioSettingDomain domain) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { getSqlMapClientTemplate().update("tbhyScenarioSetting.update", domain); deleteSceneTopicsAndTabs(domain); addSceneTopicsAndTabs(domain); } }); return get(domain.getId()); } public static void main(String[] args) { TbhyScenarioSettingDomain scene = new TbhyScenarioSettingDomain(); scene.setCreator("古飞"); scene.setGmtCreate(new Date()); scene.setGmtModified(new Date()); scene.setId(12l); scene.setIndustryId(12l); scene.setLayoutType(1); scene.setMaxItemPerBrand(5); scene.setScenarioName("测试场景"); List<TbhyTopicSettingDomain> topicList = new ArrayList<TbhyTopicSettingDomain>(2); scene.setTopicList(topicList); for (int i = 0; i < 2; i++) { TbhyTopicSettingDomain topic = new TbhyTopicSettingDomain(); topic.setCreator("gufei"); topic.setDatasourceGroup(13l + 100 * i); topic.setDisplayOrder(i); topic.setGmtCreate(new Date()); topic.setGmtModified(new Date()); topic.setId((long) i); topic.setPictureUrl("http://baidu.com"); topic.setRankOption(2); topic.setScenarioId(12l); topic.setTabNum(3); topic.setTopicName("测试topic" + i); List<TbhyTabSetingDomain> tabList = new ArrayList<TbhyTabSetingDomain>(2); topic.setTabList(tabList); for (int j = 0; j < 2; j++) { TbhyTabSetingDomain tab = new TbhyTabSetingDomain(); tab.setCreator("gufei"); tab.setDatasourceId((long) (15 + 20 * j)); tab.setDisplayOrder(j); tab.setGmtCreate(new Date()); tab.setGmtModified(new Date()); tab.setId((long) j); tab.setPictureUrl("http://xxx.com/p/i" + j); tab.setTabName("测试tab" + i * 100 + j); tab.setTopicId((long) i); tabList.add(tab); } topicList.add(topic); } System.err.print(JSON.toJSONString(scene, SerializerFeature.PrettyFormat)); } }
标签:style blog http java color os
原文地址:http://www.cnblogs.com/xianghan/p/3856010.html