资源下载:https://download.csdn/download/weixin_44893902/45604661
练习点设计: 模糊查询、删除、新增、修改
一、语言和环境
- 实现语言:JAVA语言。
- 环境要求:MyEclipse/Eclipse + Tomcat + MySql。
- 使用技术:
Jsp
+Servle
t+JavaBean
或SpringMVC
+Spring
+Mybatis
。
二、实现功能
现需要制作银行账户信息管理系统,主要功能如下:
1.首页默认显示所有银行账户信息,如图 1 所示。
2.用户输入账户名称,则完成模糊查询,显示查询结果,如图 3 所示。
3.用户点击删除,则弹出提示框,用户点击确定后,删除选中数据并显示最新数据,如图 4 和图 5 所示。
4.用户点击“新增”链接,则打开新增页面,填写完相关信息后点击添加按钮,增加银行账户信息数据到数据库,且页面跳转到列表页面展示最新数据,如图 6 和图 7 所示。
三、 数据库设计
- 创建数据库(bank)。
- 创建数据表(t_account),结构如下
四、推荐实现步骤
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数据库
bank
2、项目Java代码
目录结构
Bank
JAR包:
src
com.controller
BankController.java
package com.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 org.springframework.web.servlet.ModelAndView;
import com.dao.TAccountMapper;
import com.entity.TAccount;
@Controller
public class BankController {
@Resource
TAccountMapper tAccountMapper;
//主界面表格显示
@RequestMapping("/BankList")
public ModelAndView MainList(String name) {
List<TAccount> selectAll = tAccountMapper.selectAll(name);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("selectAll", selectAll);
modelAndView.setViewName("bankList");
return modelAndView;
}
//删除
@RequestMapping("/delList")
public String delList(int id) {
int deleteByPrimaryKey = tAccountMapper.deleteByPrimaryKey(id);
if (deleteByPrimaryKey>0) {
return "redirect:/BankList.do";
}
return "redirect:/BankList.do";
}
//跳转修改界面的方法
@RequestMapping("/updListT")
public String updListT(Model model,int id) {
List<TAccount> select = tAccountMapper.selectByPrimaryKey(id);
model.addAttribute("select", select);
return "updList";
}
//修改
@RequestMapping("/updList")
public String updList(TAccount account,Model model) {
int updateByPrimaryKey = tAccountMapper.updateByPrimaryKey(account);
if (updateByPrimaryKey>0) {
return "redirect:/BankList.do";
}
return "redirect:/BankList.do";
}
//跳转添加界面
@RequestMapping("/addListT")
public String addList() {
return "addList";
}
//添加
@RequestMapping("/addList")
public String addList(TAccount account,Model model) {
int insert = tAccountMapper.insert(account);
if (insert>0) {
return "redirect:/BankList.do";
}
return "redirect:/BankList.do";
}
}
com.dao
TAccountMapper.java
package com.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.entity.TAccount;
public interface TAccountMapper {
int deleteByPrimaryKey(Integer id);
int insert(TAccount record);
List<TAccount> selectByPrimaryKey(Integer id);
List<TAccount> selectAll(@Param("name")String name);
int updateByPrimaryKey(TAccount record);
}
TAccountMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis//DTD Mapper 3.0//EN" "http://mybatis/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.TAccountMapper" >
<resultMap id="BaseResultMap" type="com.entity.TAccount" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="number" property="number" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="money" property="money" jdbcType="DECIMAL" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_account
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.entity.TAccount" >
insert into t_account (id, number, name,
money)
values (#{id,jdbcType=INTEGER}, #{number,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{money,jdbcType=DECIMAL})
</insert>
<update id="updateByPrimaryKey" parameterType="com.entity.TAccount" >
update t_account
set number = #{number,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
money = #{money,jdbcType=DECIMAL}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select id, number, name, money
from t_account
where id = #{id}
</select>
<select id="selectAll" resultMap="BaseResultMap" >
select id, number, name, money
from t_account
<where>
<if test="name!= null">name like "%"#{name}"%" </if>
</where>
</select>
</mapper>
com.entity
TAccount.java
package com.entity;
import java.math.BigDecimal;
public class TAccount {
private Integer id;
private String number;
private String name;
private BigDecimal money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number == null ? null : number.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
}
mybatis
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis//DTD Config 3.0//EN"
"http://mybatis/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 别名 -->
<typeAliases>
<package name="com.entity" />
</typeAliases>
</configuration>
spring
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns="http://www.springframework/schema/beans"
xmlns:p="http://www.springframework/schema/p"
xmlns:context="http://www.springframework/schema/context"
xmlns:aop="http://www.springframework/schema/aop"
xmlns:tx="http://www.springframework/schema/tx"
xsi:schemaLocation="http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans-4.2.xsd
http://www.springframework/schema/context
http://www.springframework/schema/context/spring-context-4.2.xsd
http://www.springframework/schema/aop
http://www.springframework/schema/aop/spring-aop-4.2.xsd
http://www.springframework/schema/tx
http://www.springframework/schema/tx/spring-tx.xsd
http://www.springframework/schema/mvc
http://www.springframework/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 指定spring容器读取db.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 将连接池注册到bean容器中 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="Url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
<!-- 设置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 设置Mapper扫描包 -->
<property name="basePackage" value="com.dao" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解方式管理AOP事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
xmlns="http://www.springframework/schema/beans"
xmlns:context="http://www.springframework/schema/context"
xmlns:aop="http://www.springframework/schema/aop"
xmlns:tx="http://www.springframework/schema/tx"
xmlns:mvc="http://www.springframework/schema/mvc"
xsi:schemaLocation="http://www.springframework/schema/beans
http://www.springframework/schema/beans/spring-beans-4.2.xsd
http://www.springframework/schema/context
http://www.springframework/schema/context/spring-context-4.2.xsd
http://www.springframework/schema/aop
http://www.springframework/schema/aop/spring-aop-4.2.xsd
http://www.springframework/schema/tx
http://www.springframework/schema/tx/spring-tx-4.2.xsd
http://www.springframework/schema/mvc
http://www.springframework/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 配置Controller扫描 -->
<context:component-scan base-package="com.controller" />
<!-- 配置注解驱动 -->
<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/bank?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/2001/XMLSchema-instance" xmlns="http://java.sun/xml/ns/javaee" xsi:schemaLocation="http://java.sun/xml/ns/javaee http://java.sun/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Bank</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-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%>/BankList.do";
</script>
</body>
</html>
addList.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加界面</title>
</head>
<style>
#warp {
text-align: center;
width: 500px;
margin: 100px auto;
}
tr {
width: 150%;
height: 40px;
}
.lable {
width: 200px;
text-align: center;
font-size: 16px;
}
.input {
width: 390px;
}
.text {
width: 390px;
height: 40px;
}
</style>
<body>
<div id="warp">
<h1>添加信息</h1>
<form action="addList.do" method="post">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td class="lable">姓名:</td>
<td class="input"><input class="text" type="text"
placeholder="请输入姓名" name="name" id="" value="" /></td>
</tr>
<tr>
<td class="lable">卡号:</td>
<td class="input"><input class="text" type="text"
placeholder="请输入卡号" name="number" id="" value="" /></td>
</tr>
<tr>
<td class="lable">余额:</td>
<td class="input"><input class="text" type="text"
placeholder="请输入余额" name="money" id="" value="" /></td>
</tr>
</table>
<div id="">
<input
style="width: 100px; margin-top: 10px; padding: 5px; background-color: #EFEFEF;"
type="submit" value="添加" /><input
style="width: 100px; margin-top: 10px; padding: 5px; background-color: #EFEFEF; margin-left: 10%"
type="reset" value="重置" />
</div>
</form>
</div>
</body>
</html>
bankList.jsp
<%@page import="com.entity.TAccount"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>-
<%@ taglib uri="http://java.sun/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#warp {
width: 800px;
margin: 0 auto;
text-align: center;
}
table {
margin: 0 auto;
text-align: center;
width: 100%;
}
th {
font-weight: bold;
background-color: #808080;
color: #fff;
}
tr td:hover {
background-color: #9b9b9b;
}
a {
text-decoration: none;
}
a:hover {
color: red;
}
</style>
<title>银行账户信息管理系统</title>
</head>
<body>
<div id="warp">
<h1>银行账户信息管理系统</h1>
<fieldset id="" align="left">
<legend>搜索</legend>
<form action="BankList.do" method="post">
姓名: <input type="text" name="name" /> <input type="submit"
value="搜索" />
</form>
</fieldset>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<th>序号</th>
<th>姓名</th>
<th>卡号</th>
<th>余额</th>
<th>功能</th>
</tr>
<c:forEach items="${selectAll }" var="selectAll">
<tr>
<td>${selectAll.id }</td>
<td>${selectAll.name }</td>
<td>${selectAll.number }</td>
<td>${selectAll.money }</td>
<td><a href="javascript:void(0);"
onclick="del(${selectAll.id })">删除</a>
<a href="updListT.do?id=${selectAll.id }">修改</a>
</td>
</tr>
</c:forEach>
<tr align="right">
<td colspan="5"><a href="addListT.do">新增 </a>共计${selectAll.size()}条数据</td>
</tr>
</table>
</div>
</body>
<script type="text/javascript">
function del(id) {
if (confirm("确定删除该数据?")) {
window.location.href="delList.do?id="+id;
}
}
</script>
</html>
updList.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改界面</title>
</head>
<style>
#warp {
text-align: center;
width: 500px;
margin: 100px auto;
}
tr {
width: 150%;
height: 40px;
}
.lable {
width: 200px;
text-align: center;
font-size: 16px;
}
.input {
width: 390px;
}
.text {
width: 390px;
height: 40px;
}
</style>
<body>
<div id="warp">
<h1>修改信息</h1>
<form action="updList.do" method="post">
<table border="1" cellspacing="0" cellpadding="0">
<c:forEach items="${select }" var="select">
<input type="hidden" name="id" id="" value="${select.id }" />
<tr>
<td class="lable">姓名:</td>
<td class="input"><input class="text" type="text"
placeholder="请输入姓名" name="name" id="" value="${select.name}" />
</td>
</tr>
<tr>
<td class="lable">卡号:</td>
<td class="input"><input class="text" type="text"
placeholder="请输入卡号" name="number" id="" value="${select.number}" />
</td>
</tr>
<tr>
<td class="lable">余额:</td>
<td class="input"><input class="text" type="text"
placeholder="请输入余额" name="money" id="" value="${select.money}" />
</td>
</tr>
</c:forEach>
</table>
<div id="">
<input
style="width: 100px; margin-top: 10px; padding: 5px; background-color: #EFEFEF;
type="submit" value="保存" /><input
style="width: 100px; margin-top: 10px; padding: 5px; background-color: #EFEFEF; margin-left: 10%"
type="reset" value="重置" />
</div>
</form>
</form>
</div>
</body>
</html>
更多推荐
基于Spring MVC + Spring + MyBatis的【银行账户信息管理系统】
发布评论