SSM(Spring + SpringMVC + Mybatis)框架整合并实现登录功能
2019-06-01 15:10
316 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39339240/article/details/90729385
之前项目中需要用到SSM框架,所以本人在此整理一下整合的完整过程和细节,如有不对的地方请多多指教。
Spring框架:实现业务对象管理,最常见的便是ICO(控制反转)
开发工》具:eclipse
Tomcat:Tomcat 7
jdk: 1.8
接下来开始创建项目进行整合
File --> new --> Maven Project --> next --> 选择
–>next --> 输入自己的项目名称点击Finish,此时maven项目就创建好了
配置此文件导入三大框架整合所需要的所有依赖包
login.jsp
到这里SSM框架的整合基本算是完成了,可以在后续的代码中添加自己想要实现的功能。
前言
之前项目中需要用到SSM框架,所以本人在此整理一下整合的完整过程和细节,如有不对的地方请多多指教。
三大框架以及各层的作用
Spring框架:实现业务对象管理,最常见的便是ICO(控制反转)
SpringMVC框架:负责请求的转发和视图管理
Mybatis框架:作为数据对象的持久化引擎,主要是通过配置mapper.xml文件来直接给Dao接口提供对数据库进行操作的sql语句,使数据库底层操作变得更加透明
整合框架过程中还需要用到四个层:Dao层(数据访问层)、Service层(业务层)、Pojo层(实体层)、Controller层(控制层)
Dao层:主要作用是封装对数据库操作的方法(增删查改)
Service层:主要作用是封装具体的业务方法,然后在实现类中调用Dao层的方法来对数据进行操作
Pojo层:没啥好说的…个人理解就是对象实体
Controller层:主要作用是负责调用业务层的方法来实现具体的功能
环境搭建
开发工》具:eclipse
Tomcat:Tomcat 7
jdk: 1.8
接下来开始创建项目进行整合
File --> new --> Maven Project --> next --> 选择
–>next --> 输入自己的项目名称点击Finish,此时maven项目就创建好了
pom.xml
配置此文件导入三大框架整合所需要的所有依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nrt.ssm</groupId> <artifactId>MySSM</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>MySSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <!-- 使用junit4 用注解方式 --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 补全项目依赖 --> <!-- 1:日志 java日志:slf4j,log4j,logback,common-logging slf4j 是规范/接口 日志实现:log4j,logback,common-logging 使用:slf4j + logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.1</version> </dependency> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.1</version> </dependency> <!-- 2:数据库相关依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> <scope>runtime</scope> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- MyBatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!-- mybatis整合spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> <!--:3:servlet web相关依赖 --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- 4:spring依赖 --> <!-- 1)spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!-- 2)spring dao层依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!-- 3)spring web相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!-- 4)spring test相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.0.RELEASE</version> </dependency> </dependencies> <build> <finalName>MySSM</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
创建数据库表 user
pojo层(User)
package com.nrt.pojo; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return username; } public void setName(String username) { this.username = username; } public String getPwd() { return password; } public void setPwd(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
dao层(UserDao)
package com.nrt.dao; import com.nrt.pojo.User; public interface UserDao { /** * * 查找用户 */ User findByUser(User user); }
service层(UserService)
package com.nrt.service; import com.nrt.pojo.User; public interface UserService { /* * 登录验证 * */ User checkLogin(User user); }
service层的实现层(UserServiceImpl)
package com.nrt.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.nrt.dao.UserDao; import com.nrt.pojo.User; import com.nrt.service.UserService; @Service("userService") public class UserServiceImpl implements UserService{ @Resource private UserDao userDao; public User checkLogin(User user) { User u = userDao.findByUser(user); return u; } }
controller层(UserController)
package com.nrt.controller; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.SessionAttributes; import com.nrt.pojo.User; import com.nrt.service.UserService; @Controller @RequestMapping("/") @SessionAttributes("username") public class UserController { @Resource(name = "userService") private UserService userService; //测试前台能否正常访问 @RequestMapping(value = "/toLogin") public String toIndex() { return "login"; } //登录功能实现 @RequestMapping(value = "/checkLogin") public String checkLogin(@RequestParam("username")String username,@RequestParam("password")String password,Model model) { User user = new User(); user.setName(username); user.setPwd(password); if(userService.checkLogin(user) != null) { model.addAttribute("username",username); return "homePage"; }else { return "fail"; } }
配置web.xml文件
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <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> <servlet> <servlet-name>MySSM</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要加载的配置文件 spring-dao.xml,spring-service.xml,spring-web.xml Mybatis -> spring -> springMVC --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MySSM</servlet-name> <!-- 默认匹配所有的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <display-name>Archetype Created Web Application</display-name> </web-app>
Spring整合Mybatis(spring-dao.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据库连接的参数 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30"></property> <property name="minPoolSize" value="10"></property> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false"></property> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="1000"></property> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="typeAliasesPackage" value="com.nrt.pojo"></property> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 自动扫描dao包下的接口注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.nrt.dao"></property> </bean> </beans>
Spring整合SpringMVC(spring-web.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package="com.nrt.controller" /> <!-- 注意:不加这个配置,前台的jsp页面将无法使用css,js等静态文件--> <mvc:default-servlet-handler/> <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> <mvc:resources location="/WEB-INF/fonts/" mapping="/fonts/**"/> </beans>
Spring配置事务管理(spring-service.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.nrt.service" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
mybatis的配置文件(mybatis-config.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 设置主键自增 --> <setting name="useGeneratedKeys" value="true"/> <setting name="useColumnLabel" value="true"/> <!-- 驼峰名转换 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=各位的数据库密码
UserDao接口的映射文件 (UserDaoMapper.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.nrt.dao.UserDao"> <select id="findById" resultType="User" parameterType="User"> select * from user where username = #{username} </select> </mapper>
前台jsp页面(bootstrap框架搭建)
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录界面</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"> </head> <body> <div style="padding: 100px 100px 10px;text-align: center;"> <form action="checkLogin.do" method="post" class="bs-example bs-example-form" role="form"> <div class="input-group input-group-lg container" style="width: 500px;"> <span class="input-group-addon glyphicon glyphicon-user"></span> <input name="username" type="text" class="form-control" placeholder="请输入用户名"> </div><br> <div class="input-group input-group-lg container" style="width: 500px;"> <span class="input-group-addon glyphicon glyphicon-lock"></span> <input name="password" type="password" class="form-control" placeholder="请输入密码"> </div><br> <div style="text-align: center;"> <button type="submit"style="width: 80px;text-align: center;">登录</button> </div> </form> </div> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script> </body> </html>
homePage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>首页</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script> <style type="text/css"> body { padding-top: 50px; padding-left: 50px; } </style> </head> <body> <div class="navbar navbar-fixed-top navbar-inverse" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">晶天美食</a> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="toHomePage.do"><span class="glyphicon glyphicon-home"></span> 首页</a></li> <li><a href="toShoppingCart.do"><span class="glyphicon glyphicon-shopping-cart"></span> 购物车</a></li> <li><a href="#support"><span class="glyphicon glyphicon-headphones"></span> Support</a></li> </ul> <a><span class="glyphicon glyphicon-user" style="position: absolute;right: 100px;top: 15px;color: white;"> ${username }</span></a> <a href="loginOut.do"><span class="glyphicon glyphicon-off" style="position: absolute;right: 30px;top: 15px;color: white;">退出</span></a> </div> </div> </div> <div> </div> </body> </html>
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 失败啦!!! </body> </html>
效果
到这里SSM框架的整合基本算是完成了,可以在后续的代码中添加自己想要实现的功能。
相关文章推荐
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 使用SSM框架搭建Web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 整合maven+spring+springmvc+mybatis实现ssm通用增删查改基础开发框架
- Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】
- Spring+Spring MVC+MyBatis实现SSM框架整合详细教程
- 完整使用SSM框架(Spring SpringMVC Mybatis)搭建web服务器实现登录功能
- 整合maven+spring+springmvc+mybatis实现ssm通用增删查改基础开发框架
- Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】
- SSM框架:SpringMVC + Spring + MyBatis实现简单的增删改查功能
- Java EE互联网轻量级框架整合开发 SSM框架(Spring MVC+Spring+MyBatis)和Redis实现
- 使用SSM(Spring+SpringMVC+Mybatis)框架搭建服务器实现登录验证
- Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】
- Spring+Spring MVC+MyBatis实现SSM框架整合详细教程
- 使用maven,实现ssm(spring+springmvc+mybatis)三大框架的整合DEMO
- Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)