我的常用Spring相关(不定时更新)

  • 单条记录查询判空
	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;





你可能感兴趣的:(我的常用Spring相关(不定时更新))