SSM框架整合---实现简单登录注册功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nocol123/article/details/73719237

1、新建工程,整合jar包

2、数据库(oracle)键表,包含id,name,password三个字段

3、配置db.propertise文件,用于配置数据源

url=jdbc:oracle:thin:@localhost:1521:XE
user=oracle
passwd=123
driver=oracle.jdbc.OracleDriver

3、配置spring相关文件,取名为spring.xml,放在src目录下,详见配置文件

<?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">
	
	<!--引入propertise文件  -->
		<!--传统方式引入  -->
<!-- 	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!-- 		<property name="locations" value="classpath:db.properties"></property> -->
<!-- 	</bean> -->
	
	<!--简化方式  -->
	<context:property-placeholder location="classpath:db.properties"/>	
	
	<!--1.配置数据源:c3p0  -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driver}" />
		<property name="jdbcUrl" value="${url}" />
		<property name="user" value="${user}" />
		<property name="password" value="${passwd}" />
	</bean>
	
	<!--2.配置mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="typeAliasesPackage" value="com.ssm.bean" />
	</bean>
	
	<!-- 3. 自动扫描mybatis映射文件和接口的包 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ssm.dao"></property>
	</bean>
	
	<!--4.配置事务管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!--5.开启注解进行事务管理   transaction-manager:引用上面定义的事务管理器-->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

4、配置springmvc相关文件,取名为springmvc.xml,同样放在src目录下

<?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.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
		
	<!--1.开启Springioc 自动扫描注解包  -->
  	<context:component-scan base-package="com.ssm"/>
  	
  	<!--2. 开启注解 -->
	<mvc:annotation-driven />
  	
  	<!--3.配置视图解析器  -->
  	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  		<property name="prefix" value="/"></property>
  		<property name="suffix" value=".jsp"></property>
  	</bean>
  	
  	<!--4.注解映射器(可省)  -->
<!--   	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
  	

  	<!--5.配置适配器(不需时可省)  -->
<!--   	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  		在业务方法的返回值和权限之间使用@ResponseBody注解表示返回值对象需要转成JSON文本 
  		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
			</list>
		</property>
  	</bean> -->
</beans>     

5、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" id="WebApp_ID" version="3.0">
  <display-name>ssm</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>
  
  <!--Spring核心监听器  -->
  <!--在服务器启动时加载Spring容器,且只会加载一次  -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  
  <!--配置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:springmvc.xml</param-value>
		</init-param>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

  <!--配置由Spring 提供的针对中文乱码的编码过滤器 -->
  <!-- 编码过滤器 -->
	<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>
  
</web-app>

6、持久层功能实现

userDao.java (映射接口)

package com.ssm.dao.user;

import org.apache.ibatis.annotations.Param;

import com.ssm.bean.User;

/**
 * 持久层映射接口
 * @author Nocol
 *
 */
public interface UserDao {
	
	//添加用户
	public void addUser(User user);
	
	//根据用户名和密码查询用户
	//注解的两个参数会自动封装成map集合,括号内即为键
	public void findUserByNameAndPwd(@Param("name")String name, @Param("password")String password);
	
}

UserDao.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.ssm.dao.user.UserDao">
	
	<!--注意sql语句没有分号结尾  -->
	
	<insert id="addUser" parameterType="User">
		insert into t_user(id,name,password) values(#{id},#{name},#{password})
	</insert>
	
	<!--注意这里的参数类型是parameterType而不是parameterMap,因为返回的是单个类型  -->
	<select id="findUserByNameAndPwd"  parameterType="map"  resultType="User">
		select t.name,t.password from t_user t where name=#{name} and password=#{password}
	</select>
	
</mapper>

7、业务层功能实现

UserService.java

package com.ssm.service;

import com.ssm.bean.User;

public interface UserService {
	//用户注册
	void regist(User user);
	//用户登录
	void login(String name, String password);

}
UserServiceImpl.java

package com.ssm.service.Imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.bean.User;
import com.ssm.dao.user.UserDao;
import com.ssm.service.UserService;

/**
 * 业务层
 * @author Nocol
 *
 */

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public void regist(User user) {
		// TODO Auto-generated method stub
		userDao.addUser(user);
	}

	@Override
	public void login(String name, String password) {
		// TODO Auto-generated method stub
		userDao.findUserByNameAndPwd(name,password);
	}
}

8、控制层功能实现

UserAction.java

package com.ssm.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.bean.User;
import com.ssm.service.UserService;

/**
 * 控制层
 * @author Nocol
 *
 */
@Controller
@RequestMapping("/user")
public class UserAction {
	//注入Service
	@Autowired
	private UserService userService;
	
	@RequestMapping("regist")
	public String regist(User user,Model model){
		
		System.out.println("用户注册:"+user.getName()+user.getPassword());
		
		user.setId(1);
		userService.regist(user);
		
		model.addAttribute("msg", "注册成功");
		//注册成功后跳转success.jsp页面
		return "success";
	}
	
	@RequestMapping("login")
	public String login(String name,String password,Model model){
		
		System.out.println("用户登录:"+name+password);
		
		/*Map<String, String> map=new LinkedHashMap<String,String>();
		
		map.put("name", user.getName());
		map.put("password", user.getPassword());*/
		
		userService.login(name,password);
		
		model.addAttribute("msg", "登录成功");
		
		return "success";
	}
}
9、jsp页面实现

regist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/user/regist.action">
		<table border="1">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="text" name="password"></td>
			</tr>
			<tr>
				<td><input type="submit" value="注册"></td>
			</tr>
		</table>
	</form>
</body>
</html>
login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/user/login.action">
		<table border="1">
			<tr>
				<td>用户名</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="text" name="password"></td>
			</tr>
			<tr>
				<td><input type="submit" value="登录"></td>
			</tr>
		</table>
	</form>
</body>
</html>
success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
	${msg }</br>
	<a href="${pageContext.request.contextPath }/login.jsp">去登录</a>
</body>
</html>



没有更多推荐了,返回首页