黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 SSM登錄注冊功能的實現

SSM登錄注冊功能的實現

更新時間:2022-01-10 10:45:47 來源:動力節點 瀏覽1153次

先說一下運行環境:

Myeclipse 2017 CI 1

MySQL 5.7

Tomcat 8.5

項目結構如下:

在數據庫中新建user表

有幾個主要的配置文件,先了解下每個配置文件的作用。

1. web.xml:當服務啟動時首先會去加載web.xml這個資源文件,里面包括了對前端控制器、亂碼問題等配置。

2.applicatonContext.xml : 一般配置數據源,事物,注解等。

在這里我使用的是applicatonContext-*.xml的形式將DAO層、Service層、Transaction層分開配置,這樣便于管理

分別為applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml

分開配置時,需要在web.xml中配置上下文位置

3.springmvc.xml: 里面配置的是控制層的 ,如視圖解析器靜態資源, mvc 文件上傳,攔截器等。

4.SqlMapConfig.xml: 該配置文件為MyBatis的配置文件,里面無需配置,一切交給spring管理,但是xml文件基礎配置要有。

持久層相關配置文件 applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<!-- 配置 讀取properties文件 jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties" /> 
	<!-- 配置 數據源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean> 
	<!-- 配置SqlSessionFactory -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 設置MyBatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 設置數據源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 別名包掃描 -->
		<property name="typeAliasesPackage" value="com.zhu.pojo" />
	</bean> 
	<!-- 配置Mapper掃描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 設置Mapper掃描包 -->
		<property name="basePackage" value="com.zhu.mapper" />
	</bean>
</beans>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<!-- 配置@Service類的包掃描 -->
	<context:component-scan base-package="com.zhu.service"/>
</beans>

applicationContext-trans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 
	<!-- 事務管理器 -->
	<bean id="transactionManager"	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 數據源 -->
		<property name="dataSource" ref="dataSource" />
	</bean> 
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 傳播行為 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice> 
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.zhu.service.*.*(..))" />
	</aop:config>
</beans>

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:p="http://www.springframework.org/schema/p"
	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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 配置Controller掃描 -->
	<context:component-scan base-package="com.zhu.controller" /> 
	<!-- 配置注解驅動 -->
	<mvc:annotation-driven /> 
	<!-- 配置視圖解析器 -->
	<bean	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前綴 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 后綴 -->
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

SqlMapConfig.xml這個無需設置,但是必須要有

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> 
</configuration>

配置文件寫好了,接下來就開始寫代碼

先從Mapper開始寫:

1.先寫一個接口UserMapper.java

package com.zhu.mapper; 
import com.zhu.pojo.User; 
public interface UserMapper {
	//登錄驗證
	User logincheck(User user);
	//注冊
    void register(User user);
}

2.在寫一個UserMapper.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.zhu.mapper.UserMapper">
    <select id="logincheck" parameterType="com.zhu.pojo.User" resultType="com.zhu.pojo.User">
        select * 
        from user
        <where>
			<if test="name != null and name != ''">
				and `name` = #{name}
			</if>
			<if test="pwd != null and pwd != ''">
				AND `pwd` = #{pwd}
			</if>
		</where>
    </select>    
    <insert id="register" parameterType="com.zhu.pojo.User">
       insert into 
       user(name,pwd)
       value(#{name},#{pwd})
    </insert>
</mapper>

3.接下來就是Service層了,先寫一個接口:UserService.java,內容與UserMapper.java一樣

4.然后寫一個類實現UserService.java,代碼比較簡單,不做過多解釋,大家不要忘了要給給它標記@Service

package com.zhu.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; 
import com.zhu.mapper.UserMapper;
import com.zhu.pojo.User;
@Service
public class UserServiceImpl implements UserService {
   //屬性注入
   @Autowired
   private UserMapper usermapper; 
	@Override
	public User logincheck(User user) {
		User u = usermapper.logincheck(user);
		return u;
	} 
	@Override
	public void register(User user) {
		usermapper.register(user);		
	} 
}

5.最后就是Controller層了,Controller層的代碼主要是調用Service層實現的方法,實現一些操作,并與前端進行交互。

UserController.java

package com.zhu.controller; 
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.RequestParam;
import com.zhu.pojo.User;
import com.zhu.service.UserService; 
@Controller
public class UserController {
	@Autowired
     private UserService userService;
	//轉向登錄頁面
	@RequestMapping("tologin")
	public String tologin(){
		return "login";	
	}
	//登錄驗證
	@RequestMapping("login")
	public String  login(@RequestParam("username") String username,
			@RequestParam("password") String password,Model model){
		User user = new User();
		user.setName(username);
		user.setPwd(password);
		if(userService.logincheck(user) != null){
			model.addAttribute("username",username);
			return "index";
		}
	    else{
	    	model.addAttribute("error","賬號或密碼錯誤");
	    	return "login";
	    }
		}
	//轉向注冊頁面
	@RequestMapping("toregister")
	public String toregister(){
		return "register";	
	}
	//注冊
	@RequestMapping("register")
	public String  register(@RequestParam("username") String username,
			@RequestParam("password") String password){
		User user = new User();
		user.setName(username);
		user.setPwd(password);
		userService.register(user);		
		return "login";
     }
}

以上就是關于“SSM登錄注冊功能的實現”介紹,大家如果想了解更多相關知識,不妨來關注一下動力節點的SSM視頻教程,里面的課程內容詳細全面,通俗易懂,由淺到深,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 成人羞羞免费视频 | 国产一级一级一级国产片 | 中国一级毛片国产高清 | 日韩欧美一区二区三区不卡 | 丝袜美女足淫美足美脚网站 | 日韩在线观看免费 | 国产日韩欧美 | 日韩精品手机在线 | 日本黄色的视频 | 欧美综合区自拍亚洲综合 | 最新日本免费一区二区三区中文 | 国产最强大片免费视频 | 暴力调教抖s浪荡总裁受文 白洁性荡生活l六 | 丁香综合五月 | 一二三四视频社区5在线高清视频 | 丁香激情六月天 | 亚洲第一欧美 | 国产精品久久久天天影视香蕉 | 日韩欧美视频一区二区在线观看 | 久久一区二区精品 | 成人深夜视频在线观看 | 五月婷婷激情六月 | 无遮挡男女激烈免费动态图 | 欧美vs日韩vs国产在线观看 | 九九99靖品| 精品亚洲综合在线第一区 | 久久精品五月天 | 亚洲国产亚洲综合在线尤物 | 亚洲九九色 | 在线播放亚洲精品 | 在线免费观看黄色小视频 | 久久久久网站 | 小明成人免费永久性看看 | xxxx日本69xxxxx| 日韩网站在线观看 | 成年人免费在线视频观看 | 亚洲成人日韩 | 97人人澡人人爽人人爱 | 色中色在线视频 | 成人精品视频一区二区三区尤物 | 国产欧美日韩视频怡春院 |