以下将呈现的是我最近学习的一个内容,账户的增删查改功能的实现(Web版)。
账户管理系统(web版)
一.界面设计
打开系统欢迎页面(index.jsp),点击“进入账户管理”页面。在账户管理页面列出所有账户信息,可以
1.点击“添加账户”,打开一个账户信息录入界面,点击“确定”,将录入的信息保存到数据库中,并且回到账户管理页面,用户会看到已经添加的账户。
2.在对应账户点击“修改”,将打开一个账户信息修改界面,点击确定,将修改数据库中的账户信息,并且回到账户管理页面,用户会看到已经修改的账户。
3.在对应账户点击“删除”,将在数据库中删除该账户,账户管理页面将看不到这个账户信息。
二 . 实现过程
1.准备数据(表account)
2.创建一个maven web app项目,添加依赖:
(1)pom.xml中添加spring-webmvc,可以将需要的spring和spring mvc相关的包都添加简历。
(2)添加spring-jdbc(JdbcTemplate),mysql依赖,jstl依赖
(3) 建立springmvc-config.xml,在其中配置视图解析器和包扫描(controller及dao)
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 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context.xsd">
9 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
10 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
11 <property name="url" value="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8"/>
12 <property name="username" value="root"/>
13 <property name="password" value="515350"/>
14 </bean>
15 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
16 <property name="dataSource" ref="dataSource"/>
17 </bean>
18 <context:component-scan base-package="controller,dao"/>
19 <!-- 配置试图解析器 -->
20 <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
21 <property name="prefix" value="/WEB-INF/jsp/account/"/>
22 <property name="suffix" value=".jsp"/>
23 </bean>
24 </beans>
(4)在web.xml中配置前端控制器
1<!DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4<web-app id="WebApp_1526344760507">
5 <display-name>Archetype Created Web Application</display-name>
6 <!-- 配置前端控制器 -->
7 <servlet>
8 <servlet-name>springmvc</servlet-name>
9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10 <init-param>
11 <param-name>contextConfigLocation</param-name>
12 <param-value>classpath:springmvc-config.xml</param-value>
13 </init-param>
14 </servlet>
15 <servlet-mapping>
16 <servlet-name>springmvc</servlet-name>
17 <url-pattern>/</url-pattern>
18 </servlet-mapping>
19</web-app>
(5)修改index.jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3<html>
4<body>
5<h2>账户管理系统</h2>
6<a href="account/selectAccounts">点击进入系统!</a>
7</body>
8</html>
3.创建处理账户相关请求的AccountController,处理显示列表请求/account/selectAccounts
1package controller;
2import java.util.List;
3import javax.servlet.http.HttpSession;
4import model.Account;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.context.ApplicationContext;
7import org.springframework.context.support.ClassPathXmlApplicationContext;
8import org.springframework.stereotype.Controller;
9import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.RequestMapping;
11import dao.AccountDao;
12@Controller
13@RequestMapping("/account")
14public class AccountController {
15 @Autowired
16 private AccountDao accountDao;
17 @RequestMapping("/selectAccounts")
18 public String selectAccounts(HttpSession session){
19 List<Account> accounts=accountDao.selectAccounts();
20 session.setAttribute("accounts", accounts);
21 return "/accounts";//逻辑网址
22 }
23 @RequestMapping("/toInsertAccounts")
24 public String toInsertAccounts() {
25 return "/insertFrom";
26 }
27 @RequestMapping("/insertFrom")
28 public String insertAccount(Account account,HttpSession session) {
29 //插入到数据库中
30 int rows = accountDao.insertAccount(account);
31 //同步页面的数据
32 if(rows>0) {
33 List<Account> accounts=(List<Account>) session.getAttribute("accounts");
34 accounts.add(account);
35 session.setAttribute("accounts", accounts);
36 }
37 return "/accounts";
38 }
39 @RequestMapping("/toUpdateForm")
40 public String toUpdateForm(int id,HttpSession session,Model model) {
41 //获得要修改的账户信息
42 Account account=null;
43 List<Account> accounts=(List<Account>) session.getAttribute("accounts");
44 for(Account a:accounts) {
45 if(a.getId()==id) {
46 account=a;
47 break;
48 }
49 }
50 model.addAttribute("account", account);
51 return "/updateForm";
52 }
53 @RequestMapping("/updateForm")
54 public String updatetAccount(Account account,HttpSession session) {
55 //插入到数据库中
56 int rows = accountDao.updateAccount(account);
57 //同步页面的数据
58 if(rows>0) {
59 List<Account> accounts=(List<Account>) session.getAttribute("accounts");
60 for(Account a:accounts) {
61 if(a.getId()==account.getId()) {
62 a.setName(account.getName());
63 a.setBalance(account.getBalance());
64 break;
65 }
66 }
67 session.setAttribute("accounts", accounts);
68 }
69 return "/accounts";
70 }
71 @RequestMapping("/deleteAccount")
72 public String deleteAccount(int id,HttpSession session) {
73 //删除数据库
74 int rows = accountDao.deleteAccount(id);
75 //同步页面的数据
76 if(rows>0) {
77 List<Account> accounts=(List<Account>) session.getAttribute("accounts");
78 for(Account a:accounts) {
79 if(a.getId()==id) {
80 accounts.remove(a);
81 break;
82 }
83 }
84 session.setAttribute("accounts", accounts);
85 }
86 return "/accounts";
87 }
88}
4.创建账户列表页面accounts.jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ page isELIgnored="false" %>
4 <!-- JSTL标签 -->
5 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
6<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
7<html>
8<head>
9<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10<title>账户管理</title>
11</head>
12<body>
13<center>
14<h2>账户管理</h2>
15<a href="./toInsertAccounts">添加账户</a>
16<table border="1">
17<tr><td>#</td><td>姓名</td><td>余额</td><td>操作</td></tr>
18<c:forEach var="account" items="${accounts }">
19<tr><td>${account.id }</td><td>${account.name }</td><td>${account.balance }</td><td><a href="./toUpdateForm?id=${account.id}">修改</a> <a href="./deleteAccount?id=${account.id}">删除</a></td></tr>
20</c:forEach>
21</table>
22</center>
23</body>
24</html>
5.创建账户添加页面insertForm.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>添加账户</title>
8</head>
9<body>
10<table>
11<form action="./insertFrom">
12<tr><td>用户id:</td><td><input type="text" name="id"/></td></tr>
13<tr><td>账户名称:</td><td><input type="text" name="name"/></td></tr>
14<tr><td>账户余额:</td><td><input type="text" name="balance"/></td></tr>
15<tr><td><input type="submit" value="确定"/></td></tr>
16</form>
17</table>
18</body>
19</html>
6.创建账户修改页面updateForm.jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@ page isELIgnored="false" %>
4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8<title>修改账户</title>
9</head>
10<body>
11<form action="./updateForm">
12<input type="hidden" name="id" value="${account.id}"/>
13账户名称:<input type="text" name="name" value="${account.name}"/>
14账户余额:<input type="text" name="balance" value="${account.balance}"/>
15<input type="submit" value="确定"/>
16</form>
17</body>
7.创建访问account表的AccountDao。
1package dao;
2import java.sql.ResultSet;
3import java.sql.SQLException;
4import java.util.List;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.context.ApplicationContext;
7import org.springframework.context.support.ClassPathXmlApplicationContext;
8import org.springframework.jdbc.core.JdbcTemplate;
9import org.springframework.jdbc.core.RowMapper;
10import org.springframework.stereotype.Repository;
11import model.Account;
12@Repository
13public class AccountDao {
14 @Autowired
15 private JdbcTemplate jdbcTemplate;
16 public List<Account> selectAccounts(){
17 List<Account> accounts=null;
18 accounts=jdbcTemplate.query("select * from account1",new RowMapper(){
19 public Account mapRow(ResultSet rs, int index) throws SQLException {
20 // TODO Auto-generated method stub
21 Account a=new Account();
22 a.setId(rs.getInt("id"));
23 a.setName(rs.getString("name"));
24 a.setBalance(rs.getDouble("balance"));
25 return a;
26 }
27 });
28 return accounts;
29 }
30 public int insertAccount(Account account) {
31 return jdbcTemplate.update("insert into account1 values(?,?,?)",account.getId(),account.getName(),account.getBalance());
32 }
33 public int updateAccount(Account account) {
34 return jdbcTemplate.update("update account1 set name=?,balance=? where id=?",account.getName(),account.getBalance(),account.getId());
35 }
36 public int deleteAccount(int id) {
37 return jdbcTemplate.update("delete from account1 where id=?",id);
38 }
39 public static void main(String[] args){
40 ApplicationContext context=new ClassPathXmlApplicationContext("springmvc-config.xml");
41 AccountDao dao=(AccountDao) context.getBean("accountDao");
42 List<Account> accounts=dao.selectAccounts();
43 for(Account a:accounts){
44 System.out.println(a.getId()+","+a.getName()+","+a.getBalance());
45 }
46 }
47}
三.效果展示
运行项目后呈现
点击“点击进入系统”
点击“添加账户”
点击“修改”
点击“删除”即可删除
更新数据库
·end·
—如果喜欢,快分享给你的朋友们吧—
我们一起愉快的玩耍吧
关注 码之成长 获取更多学习资源