2021-05-12 14:32:11
SpringMVC+Spring+Mybatis實現最簡單的登入驗證
SpringMVC+Spring+Mybatis實現最簡單的登入驗證
1.匯入專案相關的jar包
2.建立專案結構
3.組態檔的設定及程式碼
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test1
jdbc.username=root
jdbc.password=
#定義初始連線數
jdbc.initialSize=0
#定義最大連線數
jdbc.maxActive=20
#定義最大空閒
jdbc.maxIdle=20
#定義最小空閒
jdbc.minIdle=1
#定義最長等待時間
jdbc.maxWait=60000
log4j.properties:
#定義LOG輸出級別
log4j.rootLogger=INFO,Console,File
#定義紀錄檔輸出目的地為控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定紀錄檔輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#檔案大小到達指定尺寸的時候產生一個新的檔案
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
log4j.appender.File.File = logs/ssm.log
#定義檔案最大大小
log4j.appender.File.MaxFileSize = 10MB
# 輸出所以紀錄檔,如果換成DEBUG表示輸出DEBUG以上級別紀錄檔
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n
spring-mybatis.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="com.cn.ft" />
<!-- 引入組態檔 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化連線大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 連線池最大數量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 連線池最大空閒 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 連線池最小空閒 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 獲取連線最大等待時間 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的設定對映檔案 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml檔案 -->
<property name="mapperLocations" value="classpath:com/cn/ft/mapping/*.xml"></property>
</bean>
<!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.ft.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
<context:component-scan base-package="com.cn.ft.controller" />
<!--避免IE執行AJAX時,返回JSON出現下載檔案 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 -->
</list>
</property>
</bean>
<!-- 定義跳轉的檔案的前字尾 ,檢視模式設定 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 這裡的設定我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 -->
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 組態檔上傳,如果沒有使用檔案上傳可以不用設定,當然如果不配,那麼組態檔中也不必引入上傳元件包 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 預設編碼 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 檔案大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 記憶體中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
Account.Java
1 package com.cn.ft.entity; 2 3 public class Account { 4 private Integer id; 5 private String user; 6 private String paw; 7 private Integer age; 8 9 public Integer getId() { 10 return id; 11 } 12 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 17 public String getUser() { 18 return user; 19 } 20 21 public void setUser(String user) { 22 this.user = user; 23 } 24 25 public String getPaw() { 26 return paw; 27 } 28 29 public void setPaw(String paw) { 30 this.paw = paw; 31 } 32 33 public Integer getAge() { 34 return age; 35 } 36 37 public void setAge(Integer age) { 38 this.age = age; 39 } 40 41 }
AccountDao.java
1 package com.cn.ft.dao; 2 3 import com.cn.ft.entity.Account; 4 5 public interface AccountDao { 6 public Account getByName(String user); 7 }
accountMapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.cn.ft.dao.AccountDao">
<select id="getById" parameterType="int" resultMap="accountMap">
select * from account where id = #{id}
</select>
<select id="getByName" parameterType="string" resultMap="accountMap">
select * from account where acc = #{name}
</select>
<resultMap id="accountMap" type="com.cn.ft.entity.Account">
<id property="id" column="id" />
<result property="user" column="acc" />
<result property="paw" column="password" />
<result property="age" column="age" />
</resultMap>
</mapper>
AccountController.java
1 package com.cn.ft.controller; 2 3 import javax.annotation.Resource; 4 import javax.servlet.http.HttpServletRequest; 5 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 import com.cn.ft.dao.AccountDao; 11 import com.cn.ft.entity.Account; 12 13 @Controller 14 @RequestMapping("/account") 15 public class AccountController { 16 @Resource 17 private AccountDao dao; 18 19 @RequestMapping("/showAccount") 20 public String toShowAccount(HttpServletRequest request, Model model) { 21 String user = request.getParameter("user"); 22 String paw = request.getParameter("paw"); 23 if (user == null || user == "") { 24 model.addAttribute("msg", "賬號為空"); 25 return "login"; 26 } 27 Account account = dao.getByName(user); 28 if (!account.getPaw().equals(paw)) { 29 model.addAttribute("msg", "密碼錯誤"); 30 return "login"; 31 } 32 model.addAttribute("acc", account); 33 return "showAccount"; 34 } 35 36 @RequestMapping("/toLogin") 37 public String toLogin() { 38 return "login"; 39 } 40 }
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>web</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- Spring組態檔 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 編碼過濾器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 設定Spring監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 新增對SpringMVC的支援 -->
<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-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4.資料庫
資料庫欄位以及資料資訊:
5.頁面
login.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="showAccount.do" method="post">
<div>
賬號:<input type="text" name="user" placeholder="請輸入賬號">
</div>
<div>
密碼:<input type="password" name="paw" placeholder="請輸入密碼">
</div>
<div>
<span>${msg }</span>
</div>
<div>
<input type="submit" value="登入">
</div>
</form>
</body>
</html>
showAccount.jsp
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Account</title>
</head>
<body>
<h1>測試</h1>
<h2>ID:${acc.id }</h2>
<h3>USER:${acc.user }</h3>
<h4>PAW:${acc.paw }</h4>
<h5>AGE:${acc.age }</h5>
</body>
</html>
6.測試
將專案新增到伺服器,小編用的是Tomcat在瀏覽器中輸入一下網址存取:
http://localhost:8080/專案名稱/account/toLogin.do
效果圖:
輸入資料庫中儲存的賬號和密碼登入成功效果:
在過程中,小編遇到了兩個BUG,都是因為缺少jar包引起的錯誤,若您也遇到相同的報錯資訊可以往這方面想,一下是小編的報錯資訊(僅供參考):
以上解決方案:缺少 commons-fileupload-1.3.2.jar
以上解決方案:缺少 jackson-mapper-asl-1.9.13.jar 和 jackson-core-asl-1.9.13.jar
以上就是小編這次的SSM(SpringMVC+Spring+Mybatis)學習之旅!如果有大神看到有哪裡不好的地方,希望您告訴小編改進改進!
註:以上的組態檔是小編收集整理起來的,並非全部原創,感謝分享的博主。
MyBatis入門學習教學 http://www.linuxidc.com/Linux/2015-02/113771.htm
Java實戰應用:Mybatis實現單表的增刪改 http://www.linuxidc.com/Linux/2014-06/103456.htm
Mybatis呼叫PostgreSQL儲存過程實現陣列入參傳遞 http://www.linuxidc.com/Linux/2016-09/135541.htm
[Java][Mybatis]物理分頁實現 http://www.linuxidc.com/Linux/2014-04/99889.htm
Mybatis快速入門教學 http://www.linuxidc.com/Linux/2013-06/85762.htm
Mybatis入門程式 http://www.linuxidc.com/Linux/2016-07/133637.htm
MyBatis中對映檔案和實體類的關聯性 http://www.linuxidc.com/Linux/2016-09/134942.htm
Mybatis的關於批次資料操作的測試 http://www.linuxidc.com/Linux/2012-05/60863.htm
Mybatis中對List<Object> 物件List的批次處理插入操作 http://www.linuxidc.com/Linux/2014-02/96916.htm
相關文章