私の常用Spring関連(不定時更新)

2051 ワード

  • 単条記録照会判定
  • 	public RyBean getRyByRybh(String rybh, int lb) {
    		RyBean bean = null;
    		try{
    			bean = (RyBean) jdbcTemplate.queryForObject(
    					selectOneSqlByRybh, new Object[] { rybh,lb }, new RyRowMapper());
    		}catch(EmptyResultDataAccessException e){
    			return null;
    		}
    		return bean;
    	}
  • プロファイルを取得するBean
  • ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-db.xml");
    int i = 0;
    while(i < 10000){
           System.out.println(((SerialNum) context.getBean("serialNum"))getSn("book"));
           i++;
    }
  • トランザクション構成
  • <bean id="transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">     
             <property name="dataSource" ref="dataSource"/>     
        </bean>
        
    	<aop:config>
    		<aop:pointcut id="serviceOperation" expression="execution(* com.njjy..*Service.*(..))"/>
    		<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
    	</aop:config>	
    
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
    			<tx:method name="delete*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice>
  • ある値
  • を取得する.
    final List<KVBean> list = new ArrayList<KVBean>();
    		jdbcTemplate.query(selectBhAndMcql, new RowCallbackHandler() {
    			public void processRow(java.sql.ResultSet rs) throws SQLException {
    				KVBean bean = new KVBean();     
    				bean.setKey(rs.getString("mc"));
    			    bean.setValue(rs.getString("bh"));
    			    list.add(bean);   
    			}   
    		});   
    		return list;