大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

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

SSM登錄注冊功能的實現

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

先說一下運行環境:

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視頻教程,里面的課程內容詳細全面,通俗易懂,由淺到深,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。

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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 亚洲免费久久 | 亚洲视频中文字幕在线观看 | 99久久99久久免费精品蜜桃 | 亚洲毛片免费在线观看 | 国产欧美精品三区 | 婷婷丁香在线视频 | 久久996re热这里有精品 | 99久久免费中文字幕精品 | 四虎网站1515hh四虎免费 | 欧美成年黄网站色高清视频 | 日本中文字幕一区二区高清在线 | 尹人香蕉网在线观看视频 | 国产在线乱子伦一区二区 | 永久免费的啪啪免费的网址 | 玖玖精品国产 | 不卡网 | 狠狠色噜噜狠狠狠狠色综合网 | 深夜看片在线观看18 | 深夜激情网站 | 亚洲 国产 路线1路线2路线 | 国产亚洲精品自在久久不卡 | 色综合久久88色综合天天 | www操操 | 大学生久久香蕉国产线看观看 | 在线看污网站 | 日本午夜色| 99热这里只有精品99 | 免费精品久久久久久中文字幕 | 在线观看中文字幕亚洲 | 亚洲欧美伦理 | 久草视频在线网 | 国产麻豆之光e奶女教师 | 九九精品视频在线免费观看 | 欧美aaaa黄色一级毛片 | 久久久久久人精品免费费看 | 亚洲精品中文字幕一区在线 | 男人影院在线 | 天天操天天摸天天碰 | 九九视频只有精品 | 一级女人毛片 | 热er99久久6国产精品免费 |