`

Spring mvc action获取mybaits数据

阅读更多
1)配置数据源(所要连接的数据库)db.xml
<bean id="db2dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.ibm.db2.jcc.DB2Driver</value>
		</property>
		<property name="url">
			<value>jdbc:db2://192.168.5.252:50000/ofcard</value>
		</property>
		<property name="username">
			<value>front</value>
		</property>
		<property name="password">
			<value>front</value>
		</property>
	</bean>


2)配置扫描目录和连接数据源 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName">
	<!-- auto register Processor -->
    <context:annotation-config />
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    	<property name="basePackage" value="com.anxin.msapweb.db.mybatis.mapper" />  
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    	<property name="dataSource" ref="db2dataSource" />  
	</bean>    
</beans>


3)定义查询接口
public interface MsapMerchantService {

	/**
	 * 账户概要查询(商户表+备付动态余额表)
	 * 
	 * @return
	 */
	public MsapFundDynamicDTO queryMerchantDeposit(MsapFundDynamicDTO msapFundDynamicDTO);


4)实现查询接口
public class MsapMerchantServiceImpl implements MsapMerchantService {

	@Resource
	private MsapFundDynamicMapper mapper;

	/*
	 * (non-Javadoc)
	 * @see com.anxin.msapweb.db.service.MsapMerchantService#queryMerchantDeposit(com.anxin.msapweb.db.service.dto.
	 * MsapFundDynamicDTO)
	 */
	@Override
	public MsapFundDynamicDTO queryMerchantDeposit(MsapFundDynamicDTO msapFundDynamicDTO) {
		// 组织查询条件
		MsapFundDynamicExample example = new MsapFundDynamicExample();
		MsapFundDynamicExample.Criteria criteria = example.createCriteria();

		// 添加商户号查询条件
		criteria.andMerchantIdEqualTo(msapFundDynamicDTO.getMerchantId());

		List<MsapFundDynamic> list = mapper.selectByExample(example);

		MsapFundDynamic msapFundDynamic = list.get(0);
		try {
			// 数据直拷贝
			BeanUtils.copyProperties(msapFundDynamicDTO, msapFundDynamic);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return msapFundDynamicDTO;
	}


5)spring配置service实现  biz.xml
<!-- 商户交易信息  -->
	<bean id="msapMerchantService" class="com.anxin.msapweb.biz.merchant.impl.MsapMerchantServiceImpl"></bean>


6)action调用service
@Controller
public class HomeAction extends BaseAction {

	@Resource
	private MsapMerchantService msapMerchantService;

	/**
	 * 首页.
	 * 
	 * @return
	 */
	@RequestMapping("/index.htm")
	public String index(HttpSession session) {
		session.setAttribute("merchantId", "");
		String merchantId = (String) session.getAttribute("merchantId");
		if (merchantId == null) {

		}

		MsapFundDynamicDTO msapFundDynamicDTO = new MsapFundDynamicDTO();
		msapFundDynamicDTO.setMerchantId("123456789012345");
		msapFundDynamicDTO = msapMerchantService.queryMerchantDeposit(msapFundDynamicDTO);

		System.out.println(msapFundDynamicDTO.getMerchantId());

		return "/home";
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics