超詳細spring入門實例詳解(完整版)

spring框架在我們的開發中用到的很多,前期也要理解了一下spring的基本概念

現在準備寫一個spring框架的入門實例,以一個登陸認證簽到模塊為例子;

超詳細spring入門實例詳解(完整版)

實例說明:

登陸簽到模塊其實很簡單,首先用戶輸入網址,然後系統進入歡迎頁面,在歡迎頁面填寫賬號和密碼的表單,點擊提交到服務端,檢查用戶名密碼,是否匹配;

如果匹配就記錄登陸日誌,顯示登陸成功,在登陸成功頁面有一個點擊簽到功能,點擊簽到會送5個積分,一天只能簽到一次,這裡要進行驗證一下。

如果登陸不成功就返回到登陸頁面,並給出提示:

超詳細spring入門實例詳解(完整版)

整體流程:

1.用戶登陸網址,web服務會進入默認頁面,login.jsp;

2.login.jsp是一個帶有賬號密碼的表單,用戶要輸入賬號密碼點擊提交;

3.提交到web服務器裡,web服務器會找到我們的spring配置,並根據配置找到我們的controller;

4.在controller裡面,調用service方法,根據用戶名和密碼查詢是否存在匹配的用戶;

5.service去調用我們的dao方法,dao方法與數據庫進行交互查詢並得到最終結果;

6.如果沒有找到與之匹配的用戶,那麼就返回登陸頁面,並給出錯誤提示;

7.如果找到與之匹配的用戶,進入歡迎頁面打印用戶信息。

8.點擊簽到,判斷該用戶是今天第一次登陸則可以點擊簽到,如果不是則不能點擊。

9.點擊簽到後,會找到其對應的controller,然後去調用service和dao方法,給該用戶增加5積分;

準備工作:

數據庫:

在進行開發之前要先做一點準備工作,其中最主要的就是創建數據庫,這裡我採用的是MySQL數據庫,簡單好用;

<code>##用戶表
create table sys_user(
user_id int auto_increment primary key,
user_name varchar(32),
user_password varchar(32),
credits int,
last_visit datetime,
last_ip varchar(32)
##用戶登錄日誌表
create table sys_login(
login_id int auto_increment primary key,
user_id int,


/<code>


<code>login_sign int,
login_ip varchar(32),
login_datetime datetime
)/<code>

這裡我們再初始化一條記錄,方便我們登陸;

<code>insert into sys_user(user_name,user_password) values('admin','123456');/<code>

配置信息:

1.首先創建一個web項目

然後找到WebRoot下面的WEB_INF下面的web.xml;配置我們的登陸頁面;

<code><welcome-file-list>
\t<welcome-file>login.jsp/<welcome-file>
/<welcome-file-list>/<code>

2.在web.xml裡面配置一下spring的基本信息;

spring可以將所有的配置信息都統一到一個文件中,也可以放置到多個文件中,對於簡單的項目來說,由於配置信息少,一個文件就夠了,這裡我們使用spring的擴展容器,不適用spring的父容器,

採用servlet加載的方式得到我們的擴展容器;

<code>
\t<servlet>
\t\t<servlet-name>dispatcherServlet/<servlet-name>
\t\t<servlet-class>org.springframework.web.servlet.DispatcherServlet/<servlet-class>
\t\t
\t\t
\t\t
\t\t<load-on-startup>1/<load-on-startup>
\t/<servlet>

\t<servlet-mapping>
\t\t<servlet-name>dispatcherServlet/<servlet-name>
\t\t<url-pattern>//<url-pattern>
\t/<servlet-mapping>/<code>

3.導入架包,這裡使用的是spring4 版本;導入到WebRoot下面WEB_INF下面的lib下面

超詳細spring入門實例詳解(完整版)

配置一下spring容器,以後需要什麼功能再添加:

<code>

<beans>\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\txmlns:context="http://www.springframework.org/schema/context"
\txmlns:mvc="http://www.springframework.org/schema/mvc"
\txmlns:aop="http://www.springframework.org/schema/aop"
\txmlns:tx="http://www.springframework.org/schema/tx"
\txsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
\t\thttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
\t\thttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
\t\thttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
\t\thttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

\t
\t<component-scan>
\t<bean>\t\tclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
\t\t<property>
\t/<bean>
\t
\t<bean>
\t\t<property>
\t\t<property>
\t\t<property>
\t\t<property>
\t/<bean>
\t
\t
\t<bean>
\t<property>
\t/<bean>
\t
\t
\t<bean>\tclass="org.springframework.jdbc.datasource.DataSourceTransactionManager">
\t<property>
\t/<bean>

\t
\t
\t<config>
<pointcut>
<advisor>
/<config>

<advice>
<attributes>
<method>
/<attributes>
/<advice>
\t
\t
\t<bean>
\t\t<property>
\t\t<property>
\t/<bean>
\t
\t<aspectj-autoproxy>
\t
\t<annotation-driven>

/<beans>
/<code>

持久層:

現在市面有很多ORM框架,像hibernate,mybatis,springjpa這些我都用過,個人最喜歡用mybatis了,使用持久層主要是對數據的訪問和操作與數據庫進行交互。

spring對這些框架提供了良好的支持,今天就用一個比較原始的框架springJDBC作為持久層的實現技術。

模型層:

模型層也叫實體類,它代表了與真實世界的事物,也可以用來表示業務的狀態。

一般來說實體類屬於業務層,和我們的業務直接掛鉤的,但是它貫穿了整個MVC,並被持久化到數據庫中。

模型層使得我們以面向對象的形式開發項目,為程序的擴展和維護帶來了很大的方便,一個模型往往對應於數據庫的一張表,但是也可以擴展出自己想要的模型,這使得我們的程序更加靈活。

在java程序中,一個模型也代表一個javabean;

用戶模型:用戶模型與之對應的是我們的sys_user表,一下省略get,set和構造器方法;

<code>/*/
* 用戶模型
*/
public class SysUser implements Serializable{
\tprivate int userId; //用戶主鍵
\tprivate String userName; //用戶賬戶名
\tprivate String userPassword;//用戶密碼
\tprivate int credits; //積分

\tprivate String lastIp; //IP地址
\tprivate Date lastVisit; //登陸時間/<code>

登陸日誌模型:

用戶登陸成功之後就會產生一條登陸日誌,所以我們會有一個登陸日誌模型;

<code>public class SysLogin implements Serializable{
\tprivate int loginId; //登陸日誌主鍵
\tprivate int userId; //用戶外鍵
\tprivate String loginIp; //登陸IP
\tprivate int loginSign; //是否簽到
\tprivate Date loginDatetime; //登陸時間/<code>

好了,準備工作做完了,接下來我們就一步一步的來做項目;

屬性文件:

<code>db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/
db.basename=test
db.username=root
db.password=root/<code>

一步一步構造項目:

我們再構造項目的時候一定要一步一步的來,你可以從下到上,先寫dao,再寫service,最後寫control。當然你也可以從外到內再到下,先寫jsp,再去調用control,接著往下走直到進入持久層。

那我們就先寫一個登陸頁面的jsp;

<code>/<code>


<code>

得到control之後,我們要去調用service方法,接著再去創建一個UserService;在開發的時候,我們儘量使用接口繼承,這樣可以達到,高內聚低耦合。

public interface UserService {
\t
public boolean hasMatchUser(SysUser user);


<code>@Service
public class UserServiceImpl implements UserService{
\t
\t@Autowired
\tprivate UserDao userDao;
\tpublic boolean hasMatchUser(SysUser user) {
\t\tint count=userDao.getMatchCount(user.getUserName(),user.getUserPassword());
\t\tif(count>=1)
\t\t\treturn true;
\t\treturn false;
\t}/<code>

這裡的service方法會接著往下調用Dao方法:它與我們的數據庫進行交互,

<code>ublic interface UserDao {
\tpublic int getMatchCount(String userName,String userPassword); //根據用戶名和密碼獲取匹配的用戶/<code>


<code>@Repository
public class UserDaoImpl implements UserDao{
\t
\t@Autowired
\tprivate JdbcTemplate jdbcTemplate;
\tpublic int getMatchCount(String userName,String userPassword) {
\t\tString sql=" select count(*) from sys_user where user_name=? and user_password=? ";
\t\tint count=jdbcTemplate.queryForInt(sql,new Object[]{userName,userPassword});
\t\treturn count;
\t}/<code>

到現在,我們的基本登陸方法都已經寫好了,開始啟動程序測試一下,測試成功如果沒有問題就接著完善功能,如果有問題,可能就是細節上的錯誤,這個錯誤就要通過Debug和搜索引擎進行修改了

接下來把我們的方法都給完善一下。

首先登陸進去之後,我們會去匹配賬號和密碼,如果沒有匹配到就返回登陸頁面,重新登陸,如果登陸成功之後我們就添加登陸日誌。

在添加登陸日誌的時候我們會去判斷一下他今天是否簽到

我們取出此賬號最後一次的登陸時間判斷它是否與今天的時間相等,如果不相等,那麼就證明他今天沒有登陸過,我們會給userBean添加一個標示。

<code>private boolean signStatus; //簽到狀態/<code>

在登陸的時候修改簽到狀態,然後在頁面進行簽到流程的控制;

<code>
登陸成功!

SysUser user=(SysUser)request.getSession().getAttribute("user");
if(!user.isSignStatus()){
%>
\t
\t
/<code>

點擊簽到後,會去修改他的積分,在修改積分後會添加一條登陸日誌,修改他的狀態為已經簽到;

<code>@RequestMapping(value="login")
\tpublic String login(SysUser user,HttpServletRequest request){
\t\tif(!userService.hasMatchUser(user))
\t\t\treturn "login";
\t\t//添加登陸日誌
\t\ttry {
\t\t\tuser=userService.findUserByName(user.getUserName());
\t\t\tSysLogin login=loginService.getLogin(user.getUserId());
\t\t\tint toDate=TestUtil.toDate(new Date()); //獲得今天的時間
\t\t\tint yestDate=TestUtil.toDate(login.getLoginDatetime()); //獲得最後一次的登陸時間
\t\t\tif(toDate==yestDate){
\t\t\t\tif(login.getLoginSign()==1)
\t\t\t\tuser.setSignStatus(true);
\t\t\t}
\t\t\trequest.getSession().setAttribute("user", user); //登陸成功之後把用戶放入session裡面
\t\t\tloginService.insertLogin(user);
\t\t} catch (Exception e) {
\t\t\te.printStackTrace();

\t\t}
\t\treturn "success";
\t}/<code>

所以我們會有查詢最後一次登陸信息的方法,添加登陸日誌的方法,修改登陸日誌的方法,修改用戶信息的方法。

以上就是超詳細spring入門實例詳解(完整版)下面展示了部分資料,希望也能幫助到大家,對編程感興趣的朋友,如果能幫到你請點贊、點贊、點贊:

整理的 pdf 文檔:

超詳細spring入門實例詳解(完整版)

超詳細spring入門實例詳解(完整版)

超詳細spring入門實例詳解(完整版)

超詳細spring入門實例詳解(完整版)

源碼分析專題部分課程:

超詳細spring入門實例詳解(完整版)

超詳細spring入門實例詳解(完整版)


獲取方式

點贊,收藏並轉發文章後點擊小編頭像或暱稱,關注後私信回覆:【11】 即可

舉手之勞,非常感謝!!!


分享到:


相關文章: