SSM框架下实现登录注册的示例代码
程序员文章站
2022-06-06 16:07:19
基本配置:jdk1.8 tomcat 8 myeclipse先打好地基:spring配置文件 application.xml:
基本配置:jdk1.8 tomcat 8 myeclipse
先打好地基:
spring配置文件 application.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/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语句的shop.xml --> <bean id="sqlsession" class="org.mybatis.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="org.mybatis.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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/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>
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" xmlns:web="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_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>classpath:applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.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>classpath:springmvc.xml</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) { this.email = email; } public string getpassword() { return password; } 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); }
shop.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="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) { sm.register(name, email, password); } @override public userinfo login(string email, string password) { userinfo user=sm.login(email, password); if(user!=null &&user.getpassword().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) { ss.regist(name, email, password); // 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.login(user.getemail(), user.getpassword()); 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 登录注册内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!