SSM框架下实现登录注册的⽰例代码基本配置:jdk1.8  tomcat 8  MyEclipse
先打好地基:
spring配置⽂件 l:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx" xmlns:jdbc="/schema/jdbc"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="
/schema/context /schema/context/spring-context-3.0.xsd    /schema/beans /schema/beans/spring-beans-3.0.xsd
/schema/jdbc /schema/jdbc/spring-jdbc-3.0.xsd
/schema/tx /schema/tx/spring-tx-3.0.xsd
/schema/aop /schema/aop/spring-aop-3.0.xsd
/schema/mvc /schema/mvc/spring-mvc.xsd">
<!-- 通过注解,将Service的⽣命周期纳⼊Spring的管理 -->
<context:annotation-config />
<!-- 通过注解,将Service的⽣命周期纳⼊Spring的管理 -->
<context:component-scan base-package="service"></context:component-scan>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 配置数据源 -->
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;DatabaseName=Organic
</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<!-- 扫描存放SQL语句的l -->
<bean id="sqlSession" class="batis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="pojo"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 扫描Mapper,并将其⽣命周期纳⼊Spring的管理 -->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></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>
springMVC配置⽂件 :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx" xmlns:jdbc="/schema/jdbc"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/jdbc /schema/jdbc/spring-jdbc-3.0.xsd    /schema/aop /schema/aop/spring-aop-3.0.xsd
/schema/beans /schema/beans/spring-beans-3.0.xsd
/schema/context /schema/context/spring-context-3.0.xsd
/schema/tx /schema/tx/spring-tx-3.0.xsd
/schema/mvc /schema/mvc/spring-mvc-3.2.xsd">
<!-- 扫描Controller,并将其⽣命周期纳⼊Spring管理 -->
<context:component-scan base-package="controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 注解驱动,以使得访问路径与⽅法的匹配可以通过注解配置 -->
<mvc:annotation-driven/>
<!-- 静态页⾯,如html,css,js,images可以访问 -->
<mvc:default-servlet-handler />
<!-- 视图定位 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance"
xmlns="java.sun/xml/ns/javaee"
xmlns:web="java.sun/xml/ns/javaee"
xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>OrganicShopWithSSM</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的配置⽂件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</context-param>
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
<!-- spring mvc核⼼:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置⽂件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.do</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>
开始第⼀层啦:
pojo包:UserInfo 类
package pojo;
public class UserInfo {
private String uid;
private String name;
private String email;
private String password;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
}
public String getPassword() {
return password;
}
mybatis和springmvc
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo [uid=" + uid + ", name=" + name + ", email="
+ email + ", password=" + password + "]";
}
}
mapper层:(注意mybatis的xml⽂件也要放在mapper层)
ShopMapping.java:
其中@Param注解是为了和xml中的查询参数进⾏绑定
package mapper;
import org.apache.ibatis.annotations.Param;
import pojo.UserInfo;
public interface ShopMapper {
public void register(@Param("name")String name,@Param("email")String email,@Param("password")String password);  public UserInfo login(@Param("email")String email,@Param("password")String password);
public int findUser(@Param("email")String email);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.ShopMapper">
<select id="login" resultType="UserInfo" parameterType="String" >
select * from UserInfo where email=#{email} and password=#{password}
</select>
<select id="register" resultType="UserInfo">
insert into UserInfo(name,email,password) values (#{name},#{email},#{password})
</select>
<select id="findUser" resultType="int">
select count(*) from UserInfo where email=#{email}
</select>
</mapper>
service层:其实在写登陆的时候⽤了int类型,在想登陆也只要在数据库中查询表单输⼊的数据就⾏了,在mapper层的xml的⽂件中也写了 select count(*) 查询个数,但是结果并不好,因为我要做的还有设置session。
package service;
import pojo.UserInfo;
public interface ShopService {
//⽤户注册
void regist(String name,String email,String password);
//⽤户登录
UserInfo login(String email,String password);
//验证
int findUser(String email);
}
service实现层:service.Impl
package service.Impl;
import mapper.ShopMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.UserInfo;
import service.ShopService;
@Service
public class ShopServiceImpl implements ShopService {
@Autowired
public ShopMapper sm;
@Override
public void regist(String name, String email, String password) {
}
@Override
public UserInfo login(String email, String password) {
UserInfo user=sm.login(email, password);
if(user!=null &&Password().equals(password)){
return user;
}
return null;
}
@Override
public int findUser(String email) {
if(sm.findUser(email)==0){
return 0;
}
return 1;
}
}
controller层:
package controller;
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 pojo.UserInfo;
import service.ShopService;
@Controller
@RequestMapping("")
public class ShopController {
@Autowired
public ShopService ss;
@RequestMapping(value = "registerUser", method = RequestMethod.POST)
public String registerUser(String name, String email, String password) {
int findUser = ss.findUser(email);
if (findUser == 0) {
// System.out.println("可以注册");
return "login";
} else {
// System.out.println("注册失败");
return "register";
}
}
@RequestMapping(value = "loginUser", method = RequestMethod.POST)
public String loginUser(UserInfo user, HttpSession session) {
// 调⽤service⽅法
user = ss.Email(), Password());
if (user != null) {
session.setAttribute("u".user);
return "index";
}
return "login";
}
@RequestMapping("/outLogin")
public String outLogin(HttpSession session){
session.invalidate();
return "index";
}
}
在controller层当中,关于注册的格式要求还需要⾃⾏搜索⼀下,主要讲⼀下的是登陆。在登陆的这个⽅
法中传递了两个形式参数,UserInfo是实体类,HttpSssion是设置session的关键,后⾯通过session.setAttribute()设置session,这也是在上⽂中提到的需要session的部分。在后来的注销中可以使⽤session.invalidate。
到此这篇关于SSM框架下实现登录注册的⽰例代码的⽂章就介绍到这了,更多相关SSM 登录注册内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

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