基于Spring MVC + Spring + MyBatis的【银行卡系统】


资源下载:https://download.csdn.net/download/weixin_44893902/45604256

练习点设计: 删除、新增

一、语言和环境

  1. 实现语言:JAVA语言。
  2. 环境要求:MyEclipse/Eclipse + Tomcat + MySql。
  3. 使用技术:Jsp+Servlet+JavaBeanSpringMVC + Spring + Mybatis

二、实现功能

随着银行卡越来越多,办卡人员日益增多,特需要银行卡系统:
1.首页默认显示所有银行卡信息,默认按照银行卡余额升序,如图1所示。
图1 首页显示所有银行卡信息
2.用户点击删除,则弹出提示框,用户点击确定后,删除选中数据并显示最新数据,如图3和图4所示。图3 确认删除提示窗口 在这里插入图片描述

3.用户点击“添加银行卡”操作链接,跳转到银行卡添加界面,填写完相关信息后点击添加按钮,增加银行卡信息数据到数据库,且页面跳转到列表页面展示最新数据,如图6和图7所示。
 图4 数据删除后显示最新数据

三、数据库设计

1.创建数据库(card_db)。
2.创建数据表(tb_card),结构如下。

字段名说明字段类型长度备注
id编号int主键,自增,增量为1
name持卡人姓名varchar50不能为空
sex持卡人性别Char1不能为空
cardNo卡号int不能为空
balance银行卡余额Double不能为空
level银行卡级别int1普卡 2 白金卡 3黑卡

四、推荐实现步骤

1.JSP版本的实现步骤如下:

(1)按以上数据库要求建库、建表,并添加测试数据。
(2)创建Web工程并创建各个包,导入工程所需的jar文件。
(3)创建实体类。
(4)创建Servlet获取用户不同的请求,并将这些请求转发至业务处理层相应的业务方法。
(5)创建业务处理层,在其中定义业务方法实现系统需求,在这些业务方法中需要执行DAO方法。
(6)创建BaseDAO工具类,使用JDBC完成数据表数据的查询、删除和添加。
(7)编写JSP页面,展示数据的查询结果。

2.SSM版本的实现步骤如下:

(1)创建数据库和数据表,添加测试数据(至少添加5条测试数据)。
(2)创建Web工程并创建各个包,导入工程所需的jar文件。
(3)添加相关SSM框架支持。
(4)配置项目所需要的各种配置文件(mybatis配置文件、spring配置文件、springMVC配置文件)。
(5)创建实体类。
(6)创建MyBatis操作数据库所需的Mapper接口及其Xml映射数据库操作语句文件。
(7)创建业务逻辑相应的接口及其实现类,实现相应的业务,并在类中加入对DAO/Mapper的引用和注入。
(8)创建Controller控制器类,在Controller中添加对业务逻辑类的引用和注入,并配置springMVC配置文件。
(9)创建相关的操作页面,并使用CSS对页面进行美化。
(10)实现页面的各项操作功能,并在相关地方进行验证,操作要人性化。
(11)调试运行成功后导出相关的数据库文件并提交。

五、实现代码

1、MySQL数据库

card_db

在这里插入图片描述

2、项目Java代码

目录结构

Card

在这里插入图片描述

JAR包:

在这里插入图片描述在这里插入图片描述

src

com.mhys.crm.controller

CardContrtoller.java

package com.mhys.crm.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mhys.crm.entity.TbCard;
import com.mhys.crm.service.impl.CardService;

@Controller
public class CardContrtoller {
	@Resource
	CardService cardService;

	// 查询方法
	@RequestMapping("/cardsList")
	public String cardsList(Model model) {

		List<TbCard> cardList = cardService.selectAll();
		model.addAttribute("cardList", cardList);
		return "/card";

	}

	// 跳转添加页面的方法
	@RequestMapping("/insertInto")
	public String insert() {
		return "addCard";

	}

	// 添加方法
	@RequestMapping("/insertCard")
	public String insertCard(TbCard tbCard) {

		int insertCard = cardService.insertCard(tbCard);
		return "redirect:/cardsList.do";

	}

	// 删除方法
	@RequestMapping("/deleteCard")
	public String deleteCard(int id) {

		int deletCard = cardService.deleteCard(id);
		return "redirect:/cardsList.do";

	}

}

com.mhys.crm.dao

TbCardMapper.java

package com.mhys.crm.dao;

import java.util.List;

import com.mhys.crm.entity.TbCard;

public interface TbCardMapper {
	
	int deleteByPrimaryKey(Integer id);

    int insert(TbCard record);

    List<TbCard> selectAll();

}

TbCardMapper.xml

<?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.mhys.crm.dao.TbCardMapper">
	<resultMap id="BaseResultMap" type="com.mhys.crm.entity.TbCard">
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="name" property="name" jdbcType="VARCHAR" />
		<result column="sex" property="sex" jdbcType="VARCHAR" />
		<result column="cardNo" property="cardno" jdbcType="VARCHAR" />
		<result column="balance" property="balance" jdbcType="VARCHAR" />
		<result column="level" property="level" jdbcType="VARCHAR" />
	resultMap>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
		delete from tb_card
		where id = #{id,jdbcType=INTEGER}
	delete>
	<insert id="insert" parameterType="com.mhys.crm.entity.TbCard">
		insert into tb_card (id, name, sex,
		cardNo, balance, level
		)
		values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
		#{sex,jdbcType=VARCHAR},
		#{cardno,jdbcType=VARCHAR}, #{balance,jdbcType=VARCHAR}, #{level,jdbcType=VARCHAR}
		)
	insert>
	<select id="selectAll" resultMap="BaseResultMap">
		select id, name, sex, cardNo, balance, level
		from tb_card
	select>
mapper>

com.mhys.crm.entity

TbCard.java

package com.mhys.crm.entity;

public class TbCard {
    private Integer id;

    private String name;

    private String sex;

    private String cardno;

    private String balance;

    private String level;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public String getCardno() {
        return cardno;
    }

    public void setCardno(String cardno) {
        this.cardno = cardno == null ? null : cardno.trim();
    }

    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance == null ? null : balance.trim();
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level == null ? null : level.trim();
    }
}

com.mhys.crm.service.impl

CardService.java

package com.mhys.crm.service.impl;

import java.util.List;

import com.mhys.crm.entity.TbCard;

public interface CardService {
	
	    //查询
		List<TbCard> selectAll();
		//添加
		int insertCard(TbCard tbCard);
		//删除
		int deleteCard(int id);

}

CardServiceImpl.java

package com.mhys.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mhys.crm.dao.TbCardMapper;
import com.mhys.crm.entity.TbCard;
@Service
public class CardServiceImpl implements CardService {
	@Resource 
	TbCardMapper mapper;

	@Override
	public List<TbCard> selectAll() {
		List<TbCard> selectAll=mapper.selectAll();
		return selectAll;
	}

	@Override
	public int insertCard(TbCard tbCard) {
		int addCard=mapper.insert(tbCard);
		return addCard;
	}

	@Override
	public int deleteCard(int id) {
		int delCard=mapper.deleteByPrimaryKey(id);
		return delCard;
	}

}

mybatis

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<typeAliases>
		<package name="com.mhys.crm.entity"/>
	typeAliases>
	
	
configuration>

spring

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util 
	http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	
	<context:property-placeholder location="classpath:database.properties" />
    
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	bean>
	
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
	
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
		
		<property name="dataSource" ref="dataSource" />
	bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	
		<property name="basePackage" value="com.mhys.crm.dao" />
	bean>
	
beans>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
	<context:component-scan base-package="com.mhys.crm" />
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource">property>
	bean>
	
	<tx:annotation-driven transaction-manager="transactionManager" />
beans>

spring-mvc.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
	<context:component-scan base-package="com.mhys.crm" />
	
	<mvc:annotation-driven />
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  
		<property name="prefix" value="/WEB-INF/jsp/" />
		
		<property name="suffix" value=".jsp" />
	bean>
	
beans>
	

jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/card_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver

WebContent

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Carddisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring/applicationContext-*.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring/spring-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

JSP

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
DOCTYPE html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XXX系统title>
head>
<body>
<script>
	window.location.href="<%=basePath%>/cardsList.do";
script>
body>
html>

addCard.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
DOCTYPE html>
<html>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<head>
<meta charset="utf-8">
<title>录入信息title>
<style type="text/css">
table {
	margin: auto;
	text-align: center;
}

.button {
	margin: auto;
}
style>
head>
<body>

	<form action="insertCard.do">

		<table border="1" cellspacing="0" cellpadding="5">
			<tr>
				<td
					style="background-color: #42B983; color: white; text-align: center;"
					colspan="2">新增银行卡信息td>
			tr>
			<tr>
				<td width="450px"><input type="hidden" name="id"
					value="${card.id}"> 办卡人姓名td>
				<td width="450px"><input type="text" name="name"
					value="${card.name}">td>
			tr>
			<tr>
				<td width="450px">办卡人性别td>
				<td width="450px"><input type="radio" name="sex" value="1"
					if test="${card.sex==1}">checked="checked"c:if>>男 <input
					type="radio" name="sex" value="0"
					if test="${card.sex!=1}">checked="checked"c:if>>女td>
			tr>
			<tr>
				<td width="450px">生成卡号td>
				<td width="450px"><input type="text" name="cardno"
					value="${card.cardno}">td>
			tr>
			<tr>
				<td width="450px">余额td>
				<td width="450px"><input type="text" name="balance"
					value="${card.balance}">td>
			tr>
			<tr>
				<td width="450px">银行卡级别td>
				<td width="450px"><select name="level">
						<option name="level" value="普通卡">普通卡option>
						<option name="level" value="白金卡">白金卡option>
						<option name="level" value="黑卡">黑卡option>
				select>td>
			tr>
			<tr>
				<td width="450px" colspan="2" style="text-align: center;"><input
					style="background-color: #42B983; color: white;" type="submit"
					value="添加" />td>
			tr>
		table>
	form>
body>
html>

card.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
DOCTYPE html>
<html>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<head>
<meta charset="utf-8">
<title>银行卡系统title>

<style type="text/css">
h2 {
	position: relative;
	left: 40%;
}

table {
	text-align: center;
}

.foot {
	margin-right: 100px;
	float: right;
}

tr:hover {
	background: #EEEEEE;
}

a {
	text-decoration: none;
}

p {
	text-align: right;
}
style>
head>
<body>

	<h2>银行卡列表h2>
	<form action="cardsList.do" method="post">
		<table width="100%" border="1px" cellpadding="5" cellspacing="0">
			<tr style="background-color: #42B983; color: white">
				<th width="80px">银行卡序号th>
				<th width="150px">持卡人姓名th>
				<th width="150px">持卡性别th>
				<th width="150px">银行卡卡号th>
				<th width="150px">银行卡余额th>
				<th width="150px">银行卡级别th>
				<th width="160px">操作th>
			tr>
			<c:forEach var="card" items="${cardList }" varStatus="item">
				<tr>
					<td width="80px">${card.id}td>
					<td width="150px">${card.name}td>
					<td width="150px"><c:if test="${card.sex==1}">c:if> <c:if test="${card.sex!=1}">c:if>td>
					<td width="150px">${card.cardno}td>
					<td width="150px">¥${card.balance}td>
					<td width="150px">${card.level}td>
					<td width="160px"><a
						href="javascript:if(confirm('确实删除银行卡信息?'))location='deleteCard.do?id=${card.id}'">删除a>
					td>
				tr>
			c:forEach>
		table>
		<p>
			<a href="insertInto.do">添加银行卡a>
		p>
	form>
body>
html>

相关