基于SSM框架实现简单的登录注册的⽰例代码⼀、环境配置⼯程⽬录
在l添加依赖
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>userLogin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<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>
<!-- DAO框架:MyBatis依赖 -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis⾃⾝实现的spring整合依赖 -->
<dependency>
<groupId&batis</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.</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>userLogin</finalName>
<plugins>
<!-- define the project compile level -->
<!-- maven编译⽤的版本控制,控制jdk的版本不会回跳 -->
<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>
数据库驱动配置
jdbc.properties
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysystem?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root
jdbc.password=kongtao
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/tx
/schema/tx/spring-tx.xsd
/schema/mvc
/schema/mvc/spring-mvc-3.0.xsd
">
<!-- 配置springMVC -->
<!-- 1:开启springMVC注解模式 -->
<!-- 简化配置:
(1)⾃动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
(2)提供⼀系列:数据绑定,数字和⽇期的format @NumberFormat,@DataTimeFormat
xml,json默认读写⽀持
-->
<mvc:annotation-driven/>
<!-- 2:配置jsp显⽰ViewResolver -->
<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>
<!-- 3:扫描web相关的bean -->
<context:component-scan base-package="ller" />
</beans>
⼆、具体逻辑实现数据访问对象(DAO接⼝)
UserDao.java
package com.sy.dao;
import org.apache.ibatis.annotations.Param;
import ity.User;
public interface UserDao {
/**
* 查⽤户名和密码
* @param username 登录⽤户名
* @return
*/
User findByUsername(String username);
/**
* 注册⽤户和密码
*/
void registerByUsernameAndPassword(@Param("username")String username,                    @Param("password")String password);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sy.dao.UserDao">
<!-- ⽬的:为DAO接⼝⽅法提供sql语句配置 -->
<!-- 查询⽤户和密码进⾏登录 -->
<select id="findByUsername" resultType="User" parameterType="String">
select * from user where username = #{username}
</select>
<!--增加⽤户名和密码进⾏注册 -->
<insert id="registerByUsernameAndPassword">
insert into user (username,password)
values (#{username},#{password})
</insert>
</mapper>
控制器
UserController.java
package ller;
import javax.servlet.http.HttpSession;
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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import ity.User;
import com.sy.service.UserService;
@Controller
@RequestMapping("/user")
/
/这⾥⽤了@SessionAttributes,可以直接把model中的user(也就key)放⼊其中
//这样保证了session中存在user这个对象
@SessionAttributes("user")
public class UserController {
@Autowired
private UserService userServivce;
//正常访问login页⾯
@RequestMapping("/login")
public String login(){
return "login";
}
/
/表单提交过来的路径
@RequestMapping("/checkLogin")
public String checkLogin(User user,Model model){
//调⽤service⽅法
user = userServivce.Username(), Password());    //若有user则添加到model⾥并且跳转到成功页⾯
if(user != null){
model.addAttribute("user",user);
return "success";
}
return "fail";
}
//测试超链接跳转到另⼀个页⾯是否可以取到session值
@RequestMapping("/anotherpage")
public String hrefpage(){
return "anotherpage";
}
//注销⽅法
@RequestMapping("/outLogin")
public String outLogin(HttpSession session){
//通过session.invalidata()⽅法来注销当前的session
session.invalidate();ssm框架简单吗
return "login";
}
@RequestMapping("/regist")
public String regist(){
return "regist";
}
@RequestMapping("/doRegist")
public String doRegist(User user,Model model){
System.out.Username());
userServivce.Regist(user);
return "success";
}
}
实体类
package ity;
public class User {
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。