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>
点击转账链接即进行转账操作。