Spring+Mybatis+servlet的整合

在这里以简单的购买基金功能来说明三者的整合
1.编写一个简单的前端页面login.jsp,绝对路径的两种写法

另一种 form action="${pageContext.request.contextPath}/fundServlet"
```

  
    购买基金页
  
  
  
银行账户:
消费金额:
基金名称:
买入金额:
```

2.编写spring的配置文件

```
	
    
    
    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
        
    
    
    
        
        
     
	```

3.编写FundServlet(获取数据、调用业务层、资源跳转)通过WebApplicationContextUtils获取Spring容器

		 public class FundServlet extends HttpServlet{
		    @Override
		    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		        super.doGet(request, response);
		    }
		
		    @Override
		    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		        //获取数据
		        String aname = request.getParameter("aname");
		        String balanceStr = request.getParameter("balance");
		        Double balance=Double.valueOf(balanceStr);
		        String fname = request.getParameter("fname");
		        String amountStr = request.getParameter("amount");
		        int amount=Integer.valueOf(amountStr);
		        //封装进account  与fund中
		        Account account=new Account();
		        account.setAname(aname);
		        account.setBalance(balance);
		        Fund fund=new Fund();
		        fund.setFname(fname);
		        fund.setAmount(amount);
		
		        //业务层  获取spring容器来连接service层  在这里不用classpathXmlApplicationContext是重量级的
		        ApplicationContext ac =WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		        FundService fundService = ac.getBean("fundServiceImpl", FundService.class);
		        fundService.buyFund(account,fund);
		        //页面跳转
		        request.getRequestDispatcher("/success.jsp").forward(request,response);
		    }
		}

 4.这是后由于spring.xml的路径问题和命名会报一个
 java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring.xml]
 这时只需要在web.xml中手动配置监听器并指定spring配置文件的路径及名称;


```
	
	
	    
	        FundServlet
	        com.hxh.servlet.FundServlet
	    
	    
	        FundServlet
	        /fundServlet
	    
	    
	    
	        contextConfigLocation
	        classpath:spring.xml
	    
	    
	    
	        org.springframework.web.context.ContextLoaderListener
	    
	
```

注解式
1.spring配置文件

	
	    
	    
	    
	    
	        
	        
	        
	        
	    
	    
	    
	        
	        
	    
	    
	    
	        
	        
	    

2.mapper层加注解

		public interface AccountMapper {
		    //修改
		    @Update("update account set balance=balance-#{balance} where aname=#{aname}")
		    void updateAccount(Account account);
		}

3.service层加注解,表明将当期类交个spring容器管理

		@Service("fundServiceImpl")
		public class FundServiceImpl implements FundService {
		    @Autowired
		    private AccountMapper accountMapper;
		    @Autowired
		    private FundMapper fundMapper;
		
		    public AccountMapper getAccountMapper() {
		        return accountMapper;
		    }
		
		    public void setAccountMapper(AccountMapper accountMapper) {
		        this.accountMapper = accountMapper;
		    }
		
		    public FundMapper getFundMapper() {
		        return fundMapper;
		    }
		
		    public void setFundMapper(FundMapper fundMapper) {
		        this.fundMapper = fundMapper;
		    }
		
		    @Override
		    public void buyFund(Account account, Fund fund) {
		            accountMapper.updateAccount(account);
		            fundMapper.updateFund(fund);
		        }
		}
4.注解servlet(版本要在3.0及以上版本),表明名称以及路径
		@WebServlet(name="FundServlet",urlPatterns="#{/funServlet}")

你可能感兴趣的:(java)