分析需求:
1:需要用ajax验证操作人的账户是否正确,余额是否充足,转账账户是否正确
2:因为ajax的内容都是查询,所以我们可以把这些功能写为一个功能。
3:需要添加事务,增加金额和减少金额
步骤:
首先搭建环境
需要整合spring-mybatis,所以在这里需要配置applicationContext-mybatis,然后需要使用service注解创建serviceImple对象,所以需要配置applicationContext-service.xml,转账系统需要事务管理,要么都成功,要么都失败,所以我们还需要配置事务。为了能够清楚的分辨每一个xml的功能,我们进行xml的拆分。
application-mybatis 连接数据库的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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描配置文件-->
<context:property-placeholder location="classpath:/*.properties"></context:property-placeholder>
<!--链接数据库-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver1}"></property>
<property name="url" value="${url1}"></property>
<property name="username" value="${username1}"></property>
<property name="password" value="${password1}"></property>
</bean>
<!-- 创建工厂-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
</bean>
<!-- 扫描mapper-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<property name="basePackage" value="com.bjsxt.mapper"></property>
</bean>
</beans>
applicationContext-service 使用service使用了注解,所以我们需要在这里进行配置:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bjsxt.service.imple"></context:component-scan>
</beans>
使用了事务管理,所以这里还需要配置 applicationContext-tx事务管理的配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="tx"></tx:annotation-driven>
</beans>
Controller 控制器,因为还是servlet,所以我们还是使用WebServlet,这里不能使用注解的原因是 servlet中创建对象是由Tomcat为我们创建的。
package com.bjsxt.controller;
import com.bjsxt.service.AccountService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servlet/AccountController")
public class AccountController extends HttpServlet {
private AccountService accountService;
@Override
public void init() throws ServletException {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
accountService = webApplicationContext.getBean("accountServiceImple", AccountService.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if(method.equals("checkUser")){
checkUser(req,resp);
}
if(method.equals("checkMoney")){
checkMoney(req,resp);
}
if(method.equals("checkInCno")){
checkInCno(req,resp);
}
if(method.equals("transfor")){
transfor(req,resp);
}
}
// 转账
private void transfor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String outCno = req.getParameter("outCno");
String money = req.getParameter("money");
String inCno = req.getParameter("inCno");
try{
accountService.transfor(outCno,inCno,money);
resp.sendRedirect(req.getContextPath()+"/success.jsp");
}catch (Exception e){
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
}
// 检查收款账户是否存在
private void checkInCno(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String inCno = req.getParameter("inCno");
Boolean result=accountService.check4One(inCno,null,null);
resp.getWriter().print(result);
}
// 检查余额是否充足
private void checkMoney(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String outCno = req.getParameter("outCno");
String money = req.getParameter("money");
Boolean result=accountService.check4One(outCno,null,money);
resp.getWriter().print(result);
}
// 检查用户密码是否正确(Ajax)
private void checkUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String outCno = req.getParameter("outCno");
String pwd = req.getParameter("pwd");
Boolean result=accountService.check4One(outCno,pwd,null);
resp.getWriter().print(result);
}
}
service和serviceImple
package com.bjsxt.service;
public interface AccountService {
// 检查该用户是否存在
Boolean check4One(String outCno, String pwd,String money);
//转账业务
void transfor(String outCno, String inCno, String money) throws Exception;
}
package com.bjsxt.service.imple;
import com.bjsxt.mapper.AccountMapper;
import com.bjsxt.pojo.Account;
import com.bjsxt.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountServiceImple implements AccountService {
@Autowired
AccountMapper accountMapper;
// 查询,有结果就返回true 没有结果就返回false
@Override
public Boolean check4One(String outCno, String pwd,String money) {
Account account=accountMapper.find4One(outCno,pwd,money);
if(account==null){
return false;
}
return true;
}
@Override
@Transactional
public void transfor(String outCno, String inCno, String money) throws Exception{
accountMapper.reduceMoney(outCno,money);
accountMapper.addMoney(inCno,money);
}
}
mapper层和mapper接口
package com.bjsxt.mapper;
import com.bjsxt.pojo.Account;
import org.apache.ibatis.annotations.Param;
public interface AccountMapper {
// 查询
Account find4One(@Param("outCno") String outCno, @Param("pwd")String pwd,@Param("money") String money);
// 减钱
void reduceMoney(@Param("outCno") String outCno, @Param("money")String money);
// 加钱
void addMoney(@Param("inCno")String inCno, @Param("money")String money);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.AccountMapper">
<select id="find4One" resultType="account">
select * from account
<where>
<if test="outCno!=null and outCno!=''">
and cno=#{outCno}
</if>
<if test="pwd!=null and pwd!=''">
and pwd=#{pwd}
</if>
<if test="money!=null and money!=''">
and money>=#{money}
</if>
</where>
</select>
<update id="reduceMoney">
update account set money=money-#{money} where cno=#{outCno}
</update>
<update id="addMoney">
update account set money=money+#{money} where cno=#{inCno}
</update>
</mapper>
web.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
首页(这里可以学习的是ajax的使用、表单的另一种验证)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<script type="application/javascript" src="js/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
var flag1=false;
var flag2=false;
var flag3=false;
$(function () {
$("#pwd").blur(function () {
var outCno = $("#outCno").val();
var pwd = $("#pwd").val();
$.post("/servlet/AccountController?method=checkUser","outCno="+outCno+"&pwd="+pwd,function (result) {
if(result){
flag1=true;
$("#checkUser").html("用户信息合法").css("color","green");
}else{
$("#checkUser").html("用户信息非法").css("color","red");
}
},"json")
})
$("#money").blur(function(){
var outCno = $("#outCno").val();
var money = $("#money").val();
$.post("/servlet/AccountController?method=checkMoney","outCno="+outCno+"&money="+money,function (result) {
if(result){
flag2=true;
$("#checkMoney").html("金额充足").css("color","green");
}else{
$("#checkMoney").html("金额不足").css("color","red");
}
},"json")
})
$("#inCno").blur(function () {
var inCno = $("#inCno").val();
$.post("/servlet/AccountController?method=checkInCno","inCno="+inCno,function (result) {
if(result){
flag3=true;
$("#checkInCno").html("账户存在").css("color","green");
}else{
$("#checkInCno").html("账户异常").css("color","red");
}
},"json")
})
})
function check() {
if(flag1&&flag2&&flag3){
return true;
}else{
return false;
}
}
</script>
</head>
<body>
<form action="/servlet/AccountController?method=transfor" method="post" onsubmit="return check()">
<p>
账户: <input type="text" name="outCno" id="outCno">
</p>
<p>
密码: <input type="text" name="pwd" id="pwd"> <span id="checkUser"></span>
</p>
<p>
金额: <input type="text" name="money" id="money"> <span id="checkMoney"></span>
</p>
<p>
汇款账户: <input type="text" name="inCno" id="inCno"> <span id="checkInCno"></span>
</p>
<p>
<input type="submit" value="转账">
</p>
</form>
</body>
</html>