SSM框架 Spring整合Spring MVC与MyBatis 实现用户登录

初学SSM框架 实现用户登录

一.环境搭建

创建一个web项目,配置Tomcat服务器,将所需jar包复制在WEB-INF\lib目录下
所需的jar包有:

  1. Spring所需的jar包,共12个
  2. Spring MVC依赖的组件:aspectjweaver-1.8.6.jar,aopalliance-1.0.jar,cglib-3.2.0.jar,commons-logging-1.1.3.jar
  3. MySQL数据库驱动包:mysql-connector-java-5.1.18-bin.jar
  4. 连接池核心包:c3p0-0.9.5.2.jar
  5. c3p0连接池依赖包:mchange-commons-java-0.2.11.jar
  6. 打印日志所需jar包:log4j-1.2.17.jar
  7. mybatis必备jar包:mybatis-3.4.1.jar
  8. 整合spring:mybatis-spring-1.3.1.jar

jar包的下载看这里
jar包下载方法

注意
这里jar包版本不匹配可能会报错
其中的mybatis-3.4.1.jar ,mybatis-spring-1.3.1.jar要和spring-aop-4.3.5.jar,还有其他的版本组合,可以自行查找

二.SSM框架的配置文件

SSM框架的配置文件有三个,分别是applicationContext.xml(spring整合mybatis),springmvc.xml(spring整合springMVC),web.xml
配置文件的存放位置
applicationContext.xml,springmvc.xml放在src中,web.xml在WEB-INF下

1. 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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://mybatis.org/schema/mybatis-spring
	http://mybatis.org/schema/mybatis-spring-1.2.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!--扫描com.news.dao中的所以接口,之后可以进行依赖注入-->
	<mybatis-spring:scan base-package="com.an.Dao"/>
	<!--开启spring的Bean自动扫描机制-->
	<context:component-scan base-package="com.an.entity"/>
	<!--配置数据源-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/animals"></property>
			<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
			<property name="user" value="数据库用户名"></property>
			<property name="password" value="数据库密码"></property>
			<property name="minPoolSize" value="5"></property>
			<property name="maxPoolSize" value="10"></property>
	 </bean>
	 <!--配置sqlsessionfactorybean-->
	 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"></property>
	 </bean>
	 <!--配置MapperScannerConfigurer-->
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="basePackage" value="com.an.Dao"></property>
			<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 </bean>
	 	 <!--配置DataSourceTransactionManager-->
	 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"></property>
	 </bean>
	  <!--启动基于注解的声明式事务管理配置-->
	 <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

2. 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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
	<context:component-scan base-package="com.an.controller"/>
		<context:component-scan base-package="com.an.Service.impl"/>
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/"></property>
			<property name="suffix" value=".jsp"></property>
	 </bean>
	 <mvc:annotation-driven/>
	 <mvc:default-servlet-handler/>
</beans>

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>animals</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <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的dispatcherservlet -->
  <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!-- 配置SpringMVC配置文件的位置和名称 -->
  		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
  </servlet>
  <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 避免中文乱码 -->
	<filter>
		<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
</web-app>

注意
有错误的话可以试一试把注释去掉

三.数据库设计 用户信息表Users

其中包括id,loginname,loginpwd。
id为主键且自增
在这里插入图片描述

四.根据数据表创建实体类Users

package com.an.entity;
public class Users {
	private int id;
	private String loginname;
	private String loginpw;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getLoginpw() {
		return loginpw;
	}
	public void setLoginpw(String loginpw) {
		this.loginpw = loginpw;
	}
}

五.Dao层开发

创建UsersDao接口,其中的方法hiMyBatis的注解,用于登录验证

package com.an.Dao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.an.entity.Users;
//用于登录验证
public interface UsersDao {
	//根据登录名和密码查询
	@Select("select * from users where loginname=#{loginname} and loginpw=#{loginpw}")
	public Users getUsersByCond(@Param("loginname") String loginname,@Param("loginpw") String loginpw);
}

六.Service层开发

创建接口UsersService,用于登录验证

package com.an.Service;
import com.an.entity.Users;
//用于登录验证
public interface UsersService {
	public Users login(String loginname,String loginpw);
}

七.Service层的实现类

package com.an.Service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.an.Dao.UsersDao;
import com.an.Service.UsersService;
import com.an.entity.Users;
//实现登录方法
@Service("usersService")
public class UsersServiceImpl implements UsersService{
	@Autowired
	private UsersDao usersDao;
	@Override
	public Users login(String loginname, String loginpw) {
		// TODO Auto-generated method stub
		return usersDao.getUsersByCond(loginname, loginpw);
	}
}

八.控制器开发

package com.an.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.an.Service.UsersService;
import com.an.entity.Users;
@Controller
@RequestMapping("/user")
public class UsersController {
	@Autowired
	private UsersService usersService;
	@RequestMapping("/login")
	public String login(Users u) {
		Users user = usersService.login(u.getLoginname(), u.getLoginpw());
		if(user!=null&&user.getLoginname()!=null) {
			return "index";
		}else {
			return "redirect:/login.jsp";
		}
	}
}

九.页面设计

1.login.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>
	<form action="user/login" method="post">
		用户名:<input type="text" name="loginname"/><br>
		密码:<input type="text" name="loginpw"/><br>
	    <input type="submit" value="登录"/><br>
	</form>
</body>
</html>

2.index.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>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值