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 }
Account.java
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 }
AccountDao.java
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 }
AccountController.java
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
MyBatis 的详细介绍:请点这里
MyBatis 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140411.htm