此案例是为ssm作铺垫的。

创建一个银行账户和基金账户,然后通过银行账户购买基金。由spring、springmvc、spring自带的c3p0实现。

废话不多说。如下

涉及到的 jar包(多了):

dao层:

  1. package com.bjsxt.dao;
  2.  
  3. import com.bjsxt.pojo.Account;
  4.  
  5. public interface IAccountDao {
  6. //新增
  7. void insertAccount(String aname,double balance);
  8. //更新
  9. void updateAccount(Account account);
  10. }
  1. package com.bjsxt.dao;
  2.  
  3. import com.bjsxt.pojo.Fund;
  4.  
  5. public interface IFundDao {
  6. //新增
  7. void insertFund(String fname,int amount);
  8. //更新
  9. void updateFund(Fund fund);
  10. }
  1. package com.bjsxt.dao.impl;
  2.  
  3. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  4. import org.springframework.stereotype.Repository;
  5.  
  6. import com.bjsxt.dao.IAccountDao;
  7. import com.bjsxt.pojo.Account;
  8.  
  9. @Repository
  10. public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
  11. //新增
  12. @Override
  13. public void insertAccount(String aname, double balance) {
  14. String sql="insert into account(aname,balance) values(?,?)";
  15. this.getJdbcTemplate().update(sql, aname,balance);
  16. }
  17. //更新
  18. @Override
  19. public void updateAccount(Account account) {
  20. String sql="update account set balance=balance-? where aname=?";
  21. this.getJdbcTemplate().update(sql, account.getMoney(),account.getAname());
  22. }
  23.  
  24. }
  1. package com.bjsxt.dao.impl;
  2.  
  3. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  4. import org.springframework.stereotype.Repository;
  5.  
  6. import com.bjsxt.dao.IFundDao;
  7. import com.bjsxt.pojo.Fund;
  8.  
  9. @Repository
  10. public class FundDaoImpl extends JdbcDaoSupport implements IFundDao {
  11. //新增
  12. @Override
  13. public void insertFund(String fname, int amount) {
  14. String sql="insert into fund(fname,amount) values(?,?)";
  15. this.getJdbcTemplate().update(sql, fname,amount);
  16. }
  17. //更新
  18. @Override
  19. public void updateFund(Fund fund) {
  20. String sql = "update fund set amount=amount+? where fname=?";
  21. this.getJdbcTemplate().update(sql, fund.getCount(),fund.getFname());
  22. }
  23.  
  24. }

handlers层:

  1. package com.bjsxt.handlers;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Scope;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7.  
  8. import com.bjsxt.pojo.Account;
  9. import com.bjsxt.pojo.Fund;
  10. import com.bjsxt.service.FundService;
  11.  
  12. //后端控制器
  13. @Controller
  14. @Scope("prototype")
  15. @RequestMapping("/springmvc")
  16. public class FundController{
  17. @Autowired
  18. private FundService fundService;
  19.  
  20. public FundService getFundService() {
  21. return fundService;
  22. }
  23.  
  24. public void setFundService(FundService fundService) {
  25. this.fundService = fundService;
  26. }
  27.  
  28. @RequestMapping("/buyFund")
  29. public String buyFund(Account account,Fund fund){
  30. fundService.modify(account, fund);
  31. return "welcome";
  32. }
  33.  
  34. }

pojo层:

  1. package com.bjsxt.pojo;
  2.  
  3. public class Account {
  4. private String aname;
  5. private double money;
  6. public String getAname() {
  7. return aname;
  8. }
  9. public void setAname(String aname) {
  10. this.aname = aname;
  11. }
  12. public double getMoney() {
  13. return money;
  14. }
  15. public void setMoney(double money) {
  16. this.money = money;
  17. }
  18. public Account() {
  19. super();
  20. // TODO Auto-generated constructor stub
  21. }
  22. public Account(String aname, double money) {
  23. super();
  24. this.aname = aname;
  25. this.money = money;
  26. }
  27. @Override
  28. public String toString() {
  29. return "Account [aname=" + aname + ", money=" + money + "]";
  30. }
  31.  
  32. }
  1. package com.bjsxt.pojo;
  2.  
  3. public class Fund {
  4. private String fname;
  5. private int count;
  6. public String getFname() {
  7. return fname;
  8. }
  9. public void setFname(String fname) {
  10. this.fname = fname;
  11. }
  12. public int getCount() {
  13. return count;
  14. }
  15. public void setCount(int count) {
  16. this.count = count;
  17. }
  18. public Fund() {
  19. super();
  20. // TODO Auto-generated constructor stub
  21. }
  22. public Fund(String fname, int count) {
  23. super();
  24. this.fname = fname;
  25. this.count = count;
  26. }
  27. @Override
  28. public String toString() {
  29. return "Fund [fname=" + fname + ", count=" + count + "]";
  30. }
  31.  
  32. }

service层:

  1. package com.bjsxt.service;
  2.  
  3. import com.bjsxt.pojo.Account;
  4. import com.bjsxt.pojo.Fund;
  5.  
  6. public interface FundService {
  7. //新增银行账户
  8. void addAccount(Account account);
  9. //新增基金账户
  10. void addFund(Fund fund);
  11. //更新(购买基金)
  12. void modify(Account account,Fund fund);
  13. }
  1. package com.bjsxt.service.impl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.bjsxt.dao.IAccountDao;
  7. import com.bjsxt.dao.IFundDao;
  8. import com.bjsxt.pojo.Account;
  9. import com.bjsxt.pojo.Fund;
  10. import com.bjsxt.service.FundService;
  11.  
  12. @Service
  13. public class FundServiceImpl implements FundService {
  14. @Autowired
  15. private IAccountDao accountDao;
  16. @Autowired
  17. private IFundDao fundDao;
  18.  
  19. public IAccountDao getAccountDao() {
  20. return accountDao;
  21. }
  22. public void setAccountDao(IAccountDao accountDao) {
  23. this.accountDao = accountDao;
  24. }
  25. public IFundDao getFundDao() {
  26. return fundDao;
  27. }
  28. public void setFundDao(IFundDao fundDao) {
  29. this.fundDao = fundDao;
  30. }
  31. //新增银行账户
  32. @Override
  33. public void addAccount(Account account) {
  34. accountDao.insertAccount(account.getAname(), account.getMoney());
  35. }
  36. //新增基金账户
  37. @Override
  38. public void addFund(Fund fund) {
  39. fundDao.insertFund(fund.getFname(), fund.getCount());
  40. }
  41. //购买基金(更新)
  42. @Override
  43. public void modify(Account account, Fund fund) {
  44. accountDao.updateAccount(account);
  45. fundDao.updateFund(fund);
  46. }
  47.  
  48. }

src下的配置文件:

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql:///test
  3. jdbc.username=root
  4. jdbc.password=victor
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd
  15. http://www.springframework.org/schema/mvc
  16. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  17. http://www.springframework.org/schema/context
  18. http://www.springframework.org/schema/context/spring-context.xsd">
  19. <!-- 加载jdbc属性文件 -->
  20. <context:property-placeholder location="classpath:jdbc.properties"/>
  21. <!-- c3p0数据源 -->
  22. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  23. <property name="driverClass" value="${jdbc.driver}"></property>
  24. <property name="jdbcUrl" value="${jdbc.url}"></property>
  25. <property name="user" value="${jdbc.username}"></property>
  26. <property name="password" value="${jdbc.password}"></property>
  27. </bean>
  28. <!-- 注册dao -->
  29. <bean id="accountDaoImpl" class="com.bjsxt.dao.impl.AccountDaoImpl">
  30. <property name="dataSource" ref="dataSource"/>
  31. </bean>
  32. <bean id="fundDaoImpl" class="com.bjsxt.dao.impl.FundDaoImpl">
  33. <property name="dataSource" ref="dataSource"/>
  34. </bean>
  35. <!-- 注册组件扫描器 -->
  36. <context:component-scan base-package="com.bjsxt.dao.impl"></context:component-scan>
  37. <context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>
  38. </beans>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd
  15. http://www.springframework.org/schema/mvc
  16. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  17. http://www.springframework.org/schema/context
  18. http://www.springframework.org/schema/context/spring-context.xsd">
  19. <!-- 注册组件扫描器 -->
  20. <context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
  21. <!-- 注册注解驱动 -->
  22. <mvc:annotation-driven/>
  23. <!-- 注册视图解析器 -->
  24. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  25. <property name="prefix" value="/jsp/"></property>
  26. <property name="suffix" value=".jsp"></property>
  27. </bean>
  28.  
  29. <!-- 静态资源无法访问第三种解决方案 -->
  30. <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
  31. <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
  32. </beans>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>springmvc--primary</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <!-- 指定spring配置文件的路径及名称 -->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:spring.xml</param-value>
  11. </context-param>
  12. <!-- 注册 ContextLoaderListener:监听ServletContext,当其初始化时,创建spring容器对象-->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <!-- 注册字符编码过滤器 -->
  17. <filter>
  18. <filter-name>CharacterEncodingFilter</filter-name>
  19. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  20. <init-param>
  21. <param-name>encoding</param-name>
  22. <param-value>utf-</param-value>
  23. </init-param>
  24. <init-param>
  25. <param-name>forceEncoding</param-name>
  26. <param-value>true</param-value>
  27. </init-param>
  28. </filter>
  29. <filter-mapping>
  30. <filter-name>CharacterEncodingFilter</filter-name>
  31. <url-pattern>/*</url-pattern>
  32. </filter-mapping>
  33. <!-- 注册springmvc前端控制器(中央调度器) -->
  34. <servlet>
  35. <servlet-name>springmvc</servlet-name>
  36. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  37. <!-- 指定springmvc配置文件的路径以及名称 -->
  38. <init-param>
  39. <param-name>contextConfigLocation</param-name>
  40. <param-value>classpath:springmvc.xml</param-value>
  41. </init-param>
  42. </servlet>
  43. <servlet-mapping>
  44. <servlet-name>springmvc</servlet-name>
  45. <url-pattern>/</url-pattern>
  46. </servlet-mapping>
  47. </web-app>

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath}/springmvc/buyFund" method="POST">
  11. 银行账户名称:<input type="text" name="aname"><br/>
  12. 金额:<input type="text" name="money"><br/>
  13. 基金账户名称:<input type="text" name="fname"><br/>
  14. 数量:<input type="text" name="count"><br/>
  15. <input type="submit" value="提交"><br/>
  16. </form>
  17. </body>
  18. </html>

spring-springmvc-jdbc小案例的更多相关文章

  1. Spring+SpringMVC+Hibernate小案例(实现Spring对Hibernate的事务管理)

    原文地址:https://blog.csdn.net/jiegegeaa1/article/details/81975286 一.工作环境 编辑器用的是MyEclipse,用Mysql数据库,mave ...

  2. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  3. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  4. spring boot入门小案例

    spring boot 入门小案例搭建 (1) 在Eclipse中新建一个maven project项目,目录结构如下所示: cn.com.rxyb中存放spring boot的启动类,applica ...

  5. Spring+SpringMVC+JDBC实现登录

    Spring+SpringMVC+JDBC实现登录 有一位程序员去相亲的时候,非常礼貌得说自己是一名程序员,并解释自己是做底层架构的,于是女方听到"底层"两个字,就一脸嫌弃:什么时 ...

  6. Eclipse使用JDBC小案例

    JDBC(Java Database Connectivity:Java访问数据库的解决方案)定义一套标准接口,即访问数据库的通用API,不同数据库厂商根据各自数据的特点去实现这些接口. JDBC是J ...

  7. 模拟用户登录-SpringMVC+Spring+Mybatis整合小案例

    1. 导入相关jar包 ant-1.9.6.jarant-launcher-1.9.6.jaraopalliance.jarasm-5.1.jarasm-5.2.jaraspectj-weaver.j ...

  8. spring + springmvc + jdbc + quartz + maven整合

    个人搭建框架: pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...

  9. spring+springmvc+hibernate架构、maven分模块开发样例小项目案例

    maven分模块开发样例小项目案例 spring+springmvc+hibernate架构 以用户管理做測试,分dao,sevices,web层,分模块开发測试!因时间关系.仅仅測查询成功.其它的准 ...

  10. ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查

    三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...

随机推荐

  1. 提取C3D视频特征(官方文档&实践)

    C3D Introduction 卷积神经网络(CNN)近年被广泛应用于计算机视觉中,包括分类.检测.分割等任务.这些任务一般都是针对图像进行的,使用的是二维卷积(即卷积核的维度为二维).而基于视频的 ...

  2. Django REST framework serializer 嵌套显示绝对路径

    在 Django REST framework官方文档提到,当调用Serializer时,应当传入request参数,以便生成完整的url而不是相对url.使用ModelSerializer时requ ...

  3. Cordova IOT Lesson003

    bot index.html <!DOCTYPE html> <html> <head> <title>Arduino蓝牙机械昆虫控制器</tit ...

  4. Python-uiautomator使用说明文档

    https://github.com/xiaocong/uiautomator 这个Python库是基于Android自带的uiautomator测试框架的一个python封包.适用于Android ...

  5. python经典书籍必看:流畅的Python

    作者:熊猫烧香 链接:www.pythonheidong.com/blog/article/26/ 来源:python黑洞网 目标读者 本书的目标读者是那些正在使用 Python,又想熟悉 Pytho ...

  6. 理解DP(持续更新)

    理解DP author: thy from buaa 初见 dynamic programming(可以理解为动态刷表法 其实这里的programming并不是编程而是规划.设计表格的意思) 关于动态 ...

  7. LCA的在线与离线算法

    在线:链接 离线:链接

  8. JAVA 学习日记

    4. 数组 int[] in = new int[5];in[0] = 1;in[1] = 11;in[2] = 111;for(int i=0;i<in.length;i++){ System ...

  9. Solve Error: "errcode": 40016, "errmsg": "invalid button size hint"

    在使用微信官方给的添加自定义菜单的示例代码: { "button": [ { "name": "扫码", "sub_button& ...

  10. 加密传输:每位数字+6,然后用除以9的余数代替该数字, 在把第一位和第四位交换,第二位和第三位交换,例如3276->3840

    /** * @author:(LiberHome) * @date:Created in 2019/3/6 10:00 * @description: * @version:$ */ /*今日目标:5 ...