[1] 在数据库中创建用户信息表
[2] 搭建SSM开发环境
-
使用idea创建登录功能的web项目
-
在web-inf目录下创建lib文件夹,并导入SSM的jar包.
-
在src下创建MVC的包结构
-
在src下创建并配置SSM的xml文件
① 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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"default-autowire="byName"><!--属性文件扫描--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--配置注解扫描--><context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan><!--配置数据源bean--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${mysql.driver}"></property><property name="url" value="${mysql.url}"></property><property name="username" value="${mysql.username}"></property><property name="password" value="${mysql.password}"></property></bean><!--配置工厂bean--><bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"></bean><!--配置Mapper扫描bean--><bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="factory"></property><property name="basePackage" value="com.bjsxt.mapper"></property></bean><!--配置事务管理bean--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean><!--配置事务管理方法--><tx:advice id="advice" transaction-manager="transactionManager"><tx:attributes><tx:method name="sel*" read-only="true"/><tx:method name="ins*"/><tx:method name="up*"/><tx:method name="del*"/></tx:attributes></tx:advice><!--配置事务管理切面--><aop:config><aop:pointcut id="mp" expression="execution(* com.bjsxt.service.impl.*.*(..))"/><aop:advisor advice-ref="advice" pointcut-ref="mp"></aop:advisor></aop:config>
</beans>
② db.properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/502
mysql.username=root
mysql.password=1234
③ log4j.properties
log4j.rootCategory=infolog4j.logger.com.bjsxt.mapper=debug, CONSOLE,LOGFILE
log4j.logger.com.bjsxt.advice=debug, CONSOLE,LOGFILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %c-%d-%m%nlog4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=D:/axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=- %c-%d-%m%n
④ springmvc.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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--配置注解扫描--><context:component-scan base-package="com.bjsxt.controller"></context:component-scan><!--配置注解解析器--><mvc:annotation-driven></mvc:annotation-driven><!--配置静态资源放行--><mvc:resources mapping="/js/**" location="/js/"></mvc:resources><mvc:resources mapping="/css/**" location="/css/"></mvc:resources><mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
</beans>
- 配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><!--配置Spring容器的配置文件路径--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationcontext.xml</param-value></context-param><!--配置Spring的监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--配置SpringMVC的Servlet--><servlet><servlet-name>mvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>mvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--配置编码过滤器--><filter><filter-name>code</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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>code</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>
[3] 实现登录功能
- 创建登录页面(简单的login.jsp)
<%--Created by IntelliJ IDEA.User: wyyDate: 2021/6/28Time: 16:05
--%>
<%String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><base href="<%=basePath %>"/><title>Title</title>
</head>
<body><h3>SSM整合案例之简单登录</h3><hr><form action="userLogin">用户名: <input type="text" name="uname" value=""><br>密码: <input type="password" name="pwd" value=""><br><input type="submit" value="登录"></form>
</body>
</html>
- 创建控制器类,并声明处理登录请求的单元方法
package com.bjsxt.controller;import com.bjsxt.pojo.User;
import com.bjsxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class UserController {//声明业务层属性@Autowiredprivate UserService userService;//声明单元方法:处理登录请求@RequestMapping("userLogin")public String userLogin(String uname,String pwd){//处理请求User user = userService.selUserInfoService(uname, pwd);//响应结果if(user!=null){return "redirect:/main.jsp";}else{return "redirect:/login.jsp";}}}
- 创建登录业务代码
package com.bjsxt.service;import com.bjsxt.pojo.User;public interface UserService {//登录业务方法User selUserInfoService(String uname, String pwd);
}
package com.bjsxt.service.impl;import com.bjsxt.mapper.UserMapper;
import com.bjsxt.pojo.User;
import com.bjsxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserServiceImpl implements UserService {//声明mapper层属性@Autowiredprivate UserMapper userMapper;//登录业务方法@Overridepublic User selUserInfoService(String uname, String pwd) {return userMapper.userLoginMapper(uname,pwd);}
}
- 创建登录mapper层代码
package com.bjsxt.mapper;import com.bjsxt.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;public interface UserMapper {//用户登录@Select("select * from t_user where uname=#{uname} and pwd=#{pwd}")User userLoginMapper(@Param("uname") String uname,@Param("pwd") String pwd);}