springmvc springJDBC 簡單實訓銀行帳戶管理系統css
1.簡單介紹一下,在校時每週結束都會有一次學習總結,簡稱「實訓」,此次實訓內容是spring,由於是最近熱門框架,我就先從基礎方面開始練習,一路填坑,有點不容易。這是我第一次寫文章,可能不太完善,但願多多包涵。喜歡的同窗能夠關注一會兒。html
項目建完大概是這樣,看一下:java
——————————————————————————————————————————————————————————————mysql
2.項目搭建需求:
web
myEclipse + mysql + jdk1.6 + (spring 3.0 core Libraries+spring 3.0 AOP Libraries+spring 3.0 Web Libraries+spring 3.0+persistence JDBC Libraries+spring 3.0 persistence Core Libraries) + mysql5.17.jarspring
大體是這些,如仍是不明白能夠隨時聯繫我:1763907618+備註(博客園)sql
——————————————————————————————————————————————————————————————數據庫
3.數據庫搭建:session
創建數據庫名字 bankmvc
創建表格trade 和 user 字段以下:
————————————————————————————————————————————————————————————————————————————————————
4.代碼及功能的實現:
先看主要項目結構:
好了,不廢話開始咱們的第一步。
new--------web project-------輸入name-------finish
右鍵單擊項目 ---------MyEclipse-----------Add spring Capabilities 如圖:
接下來選擇spring3.0 選擇所須要的jar包;如圖:-------選擇好後------finish
將applicationContext.xml拖入到webroot/web-inf內,如圖:
同時將mysql驅動jar放入lib內。!!!!!
在src內創建四個包,我就不一一打了,如圖:
嗯,如今基本準備好了,就先擼一個登陸部分,直接上代碼吧:
在applicationContext,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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定鏈接數據庫的驅動 --> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <!-- 指定鏈接數據庫的URL --> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf-8"/> <!-- 指定鏈接數據庫的用戶名 --> <property name="user" value="你的數據庫名字"/> <!-- 指定鏈接數據庫的密碼 --> <property name="password" value="你的數據庫密碼"/> <!-- 指定鏈接數據庫鏈接池的最大鏈接數 --> <property name="maxPoolSize" value="40"/> <!-- 指定鏈接數據庫鏈接池的最小鏈接數 --> <property name="minPoolSize" value="1"/> <!-- 指定鏈接數據庫鏈接池的初始化鏈接數 --> <property name="initialPoolSize" value="1"/> <!-- 指定鏈接數據庫鏈接池的鏈接的最大空閒時間 --> <property name="maxIdleTime" value="20"/> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value> org.springframework.web.servlet.view.JstlView </value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/userAdd.do">userAddAction</prop> <prop key="/userLogin.do">userLoginAction</prop> </props> </property> </bean> <!---Action Definition--> <!-- 用戶內容 --> <bean class="com.service.UserService" id="userService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="com.dao.UserDao"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 用戶內容 用戶登錄--> <bean id="userLoginAction" class="com.controller.UserLoginAction"> <property name="userService" ref="userService"></property> <property name="commandClass" value="com.pojo.User"></property> <property name="success_view"> <value>main</value> </property> <property name="shibai_view"> <value>shibai</value> </property> </bean> <!-- 用戶內容 用戶註冊--> <bean id="userAddAction" class="com.controller.UserAddAction"> <property name="userService" ref="userService"></property> <property name="commandClass" value="com.pojo.User"></property> <property name="success_view"> <value>index</value> </property> </bean> </beans>
可能會有錯誤提示,別急,一步步來;
在web.xml裏修改,如:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
在index.jsp裏修改如:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登陸</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <form action="userLogin.do" method="post" > <input type="text" name="username" placeholder="登陸|帳號" /><br /> <input type="password" name="password" placeholder="輸入密碼" /><br /> <input type="submit" value="登 錄" /><br/> <input type="button" name="button" id="button" value="註冊" onClick="location.href='userAdd.jsp'" > </form> </body> </html>
創建main.jsp裏面添加幾句話就行,一會登陸成功跳轉的頁面。
創建userAdd.jsp,這是註冊頁面,順手寫了吧。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登陸</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <style type="text/css"> .div_th{ width: 150px; text-align: center;height: 40px;background-color: teal; font-size: 20px;font-family: "楷體";color: white; } .div_td{ width: 300px;height: 40px; background-color: teal; font-size: 20px;font-family: "楷體" } </style> </head> <body> <form id="form3" name="form3" action="userAdd.do" method="post"> <table border="1" align="center"> <tr > <th class="div_th" >帳號</th> <td class="div_td"> <input type="text" name="username" style="width: 95%"> </td> <th class="div_th">性別</th> <td class="div_td"> <select name="sex" title="性別"> <option value="男" >男</option> <option value="女" >女</option> </select> </td> </tr> <tr> <th class="div_th">密碼 </th> <td class="div_td"><input type="password" name="password" style="width: 95%"> </td> <th class="div_th">確認密碼</th> <td class="div_td"><input type="password" name="confirmpassword" style="width: 95%"></td> </tr> <tr> <th class="div_th">用戶年齡</th> <td class="div_td"><input type="text" name="userage" style="width: 95%"> </td> <th class="div_th">身份證號</th> <td class="div_td"><input type="text" name="idcard" style="width: 95%" maxlength="18"></td> </tr> <tr> <th class="div_th">聯繫電話</th> <td class="div_td"><input type="text" name="tel" style="width: 95%"></td> <th class="div_th">居住城市</th> <td class="div_td"><input type="text" name="city" style="width: 95%"></td> </tr> <tr> <th class="div_th">詳細地址</th> <td class="div_td"><input type="text" name="useradd" style="width: 95%"></td> <th class="div_th"></th> <td class="div_td"></td> </tr> </table> <input type="hidden" name="userflag" value="否"> <div style="text-align: center; background-color: teal"> <input type="submit" value="注 冊" name="submit" id="submit" style="width: 500px;height: 35px;color: white;background-color: orange;font-size: 20px;font-family: 楷體"> <br/> <input type="button" name="button" value="返 回" onClick="location.href='index.jsp'"style="width: 500px;height: 35px;color: white;background-color: greenyellow;margin-top: 1%; font-size: 20px;font-family: 楷體" > </div> </form> </body> </html>
在com.pojo裏創建User.java
package com.pojo; public class User { public static final int PAGE_SIZE=10; private int id; private String username; private String password; private String userflag; private int userage; private String idcard; private String tel; private String city; private String useradd; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserflag() { return userflag; } public void setUserflag(String userflag) { this.userflag = userflag; } public int getUserage() { return userage; } public void setUserage(int userage) { this.userage = userage; } public String getIdcard() { return idcard; } public void setIdcard(String idcard) { this.idcard = idcard; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getUseradd() { return useradd; } public void setUseradd(String useradd) { this.useradd = useradd; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public static int getPageSize() { return PAGE_SIZE; } }
上面那個至關好理解,也容易自動生成,不詳細說明了。
在com.dao裏創建UserDao.java代碼以下:
package com.dao; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.pojo.User; public class UserDao extends JdbcDaoSupport { //用戶---- 用戶登錄 @SuppressWarnings("unchecked") public List userLogin(User user){ JdbcTemplate jt=new JdbcTemplate(getDataSource()); String sql="select * from user where username='"+user.getUsername()+"' and password='"+user.getPassword()+"'"; List rows=jt.queryForList(sql); return rows; } //用戶---- 用戶註冊 public int userAdd(User user){ JdbcTemplate jt=new JdbcTemplate(getDataSource()); String sql = "insert into user (username, password, userflag,userage,idcard,tel,city,useradd,sex) " + "value('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getUserflag()+"','"+user.getUserage()+"','"+user.getIdcard()+"','"+user.getTel()+"','"+user.getCity()+"','"+user.getUseradd()+"','"+user.getSex()+"')"; int n=jt.update(sql); return n; } }
上面這個主要涉及到數據庫一些內容,仍是老樣子不懂諮詢我。
在com.service裏創建UserService.java.如:
package com.service; import java.util.List; import com.dao.UserDao; import com.pojo.User; public class UserService { UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } //用戶---- 用戶登錄 @SuppressWarnings("unchecked") public List userLogin(User user){ return userDao.userLogin(user); } //用戶---- 用戶註冊 public int userAdd(User user){ return userDao.userAdd(user); } }
在com.controller裏建立UserLoginAction.java 登陸
package com.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; import com.pojo.User; import com.service.UserService; @SuppressWarnings("deprecation") public class UserLoginAction extends SimpleFormController { public String success_view; public String shibai_view; public String getShibai_view() { return shibai_view; } public void setShibai_view(String shibaiView) { shibai_view = shibaiView; } public UserService userService; public String getSuccess_view() { return success_view; } public void setSuccess_view(String successView) { success_view = successView; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @SuppressWarnings("unchecked") protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object cmd,BindException ex)throws Exception{ User user=(User)cmd; List list=userService.userLogin(user); String username=user.getUsername(); HttpSession session=request.getSession(); session.setAttribute("user", list); session.setAttribute("username", username); if(list==null||list.size()<1){ return new ModelAndView(this.getShibai_view()); }else { return new ModelAndView(this.getSuccess_view()); } } }
在com.controller裏建立UserAddAction.java 註冊
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; import com.pojo.User; import com.service.UserService; @SuppressWarnings("deprecation") public class UserAddAction extends SimpleFormController { public String success_view; public UserService userService; public String getSuccess_view() { return success_view; } public void setSuccess_view(String successView) { success_view = successView; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object cmd,BindException ex)throws Exception{ User user=(User)cmd; int n=userService.userAdd(user); request.setAttribute("user", user.getUsername()); if(n>0){ request.setAttribute("userAddMes", "註冊成功"); }else{ request.setAttribute("userAddMes", "註冊失敗"); } return new ModelAndView(this.getSuccess_view()); } }
-____________________________________________________________________________________________________________________________________________
好了,登陸和註冊的功能已經完成,由於也是菜鳥,因此有不少地方,好比方法什麼的出現錯誤還望各位大神指出,在下就多多學習。學習在於積累,勤奮一下總會有所成功。
這個程序由於時間緊,因此寫的有點倉促,只是嚴格的按照要求把程序功能所有實現,而裏面的好多效驗什麼的沒有寫。切記!
個人源碼裏是有css,和js樣式的,因此還算美觀。
可是不知道怎麼上傳源碼,因此麻煩一下吧,須要源碼的直接聯繫我吧。(我的愛好,免費)
QQ:1763907618。+備註(博客園)
郵箱:1763907618@qq.com
我會一直在,期待小夥伴們的評論,哈哈!!!!