【Spring框架】Spring整合Web(实现转账案例)

Spring整合Web:
通过Spring对数据库进行 操作后将数据显示在Web页面上。

步骤:
web.xml文件配置:

<!--确定配置文件位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/applicationContext.xml</param-value>
    </context-param>
    <!--配置spring 监听器,加载xml配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Hello2Servlet</servlet-name>
        <servlet-class>com.spring.web.servlet.Hello2Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello2Servlet</servlet-name>
        <url-pattern>/Hello2Servlet</url-pattern>
    </servlet-mapping>

Spring配置文件:(applicationContext.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 1.datasource 此处是用c3p0的连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_day03?serverTimezone=UTC"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123"></property>
    </bean>

    <!-- 2.dao -->
    <bean id="accountDao" class="com.spring.web.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 3.service -->
    <bean id="accountService" class="com.spring.web.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
</beans>

创建accountDao接口与实现类:

接口:
package com.spring.web.dao;
public interface AccountDao {
    /**
     * 汇款
     * @param outer
     * @param money
     */
    public void out(String outer, Integer money);

    /**
     * 收款
     * @param inner
     * @param money
     */
    public void in(String inner, Integer money);
}

实现类:
package com.spring.web.dao.impl;
import com.spring.web.dao.AccountDao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/*
    继承JdbcDaoSupport,将实现类的对象放入spring容器中,通过JdbcDaoSupport赋值给dataSource
     */
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    @Override
    public void out(String outer, Integer money) {
        this.getJdbcTemplate().update("update account set money = money - ? where username = ?",money,outer);
    }

    @Override
    public void in(String inner, Integer money) {
        this.getJdbcTemplate().update("update account set money = money + ? where username = ?",money,inner);
    }
}

创建accountService接口与实现类:

接口:
package com.spring.web.service;
public interface AccountService {
    /**
     * 转账
     * @param outer
     * @param inner
     * @param money
     */
    public void transfer(String outer, String inner, Integer money);
}

实现类:
package com.spring.web.service.impl;
import com.spring.web.dao.AccountDao;
import com.spring.web.service.AccountService;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao){
            this.accountDao = accountDao;
    }

    @Override
    public void transfer(String outer, String inner, Integer money) {
        accountDao.in(inner,money);
        //int i=1/0;
        accountDao.out(outer,money);
        System.out.println("转账成功@");
    }
}

servlet实现:

package com.spring.web.servlet;
import com.spring.web.service.AccountService;
import org.springframework.context.ApplicationContext;
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;

public class Hello2Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从appalication作用域(ServletContext)获得spring容器
        //方式1:
        ApplicationContext applicationContext = (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        //方式2:
//        ApplicationContext applicationContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //操作
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        accountService.transfer("Rose","Jack",1000);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

jsp:

<a href="${pageContext.request.contextPath}/Hello2Servlet">spring容器获得,转账</a>

点击转账链接即进行转账操作。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谦风(Java)

一起学习,一起进步(✪ω✪)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值