更新時(shí)間:2022-01-10 10:45:47 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1328次
Myeclipse 2017 CI 1
MySQL 5.7
Tomcat 8.5


在數(shù)據(jù)庫中新建user表

有幾個(gè)主要的配置文件,先了解下每個(gè)配置文件的作用。
1. web.xml:當(dāng)服務(wù)啟動(dòng)時(shí)首先會(huì)去加載web.xml這個(gè)資源文件,里面包括了對(duì)前端控制器、亂碼問題等配置。
2.applicatonContext.xml : 一般配置數(shù)據(jù)源,事物,注解等。
在這里我使用的是applicatonContext-*.xml的形式將DAO層、Service層、Transaction層分開配置,這樣便于管理
分別為applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml
分開配置時(shí),需要在web.xml中配置上下文位置
3.springmvc.xml: 里面配置的是控制層的 ,如視圖解析器靜態(tài)資源, mvc 文件上傳,攔截器等。
4.SqlMapConfig.xml: 該配置文件為MyBatis的配置文件,里面無需配置,一切交給spring管理,但是xml文件基礎(chǔ)配置要有。
持久層相關(guān)配置文件 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" />
<!-- 配置 數(shù)據(jù)源 -->
<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">
<!-- 設(shè)置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 設(shè)置數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource" />
<!-- 別名包掃描 -->
<property name="typeAliasesPackage" value="com.zhu.pojo" />
</bean>
<!-- 配置Mapper掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 設(shè)置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">
<!-- 事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源 -->
<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" />
<!-- 配置注解驅(qū)動(dòng) -->
<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這個(gè)無需設(shè)置,但是必須要有
<?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.先寫一個(gè)接口UserMapper.java
package com.zhu.mapper;
import com.zhu.pojo.User;
public interface UserMapper {
//登錄驗(yàn)證
User logincheck(User user);
//注冊(cè)
void register(User user);
}
2.在寫一個(gè)UserMapper.xml與之相關(guān)聯(lián)
<?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層了,先寫一個(gè)接口:UserService.java,內(nèi)容與UserMapper.java一樣
4.然后寫一個(gè)類實(shí)現(xiàn)UserService.java,代碼比較簡單,不做過多解釋,大家不要忘了要給給它標(biāo)記@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層的代碼主要是調(diào)用Service層實(shí)現(xiàn)的方法,實(shí)現(xiàn)一些操作,并與前端進(jìn)行交互。
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;
//轉(zhuǎn)向登錄頁面
@RequestMapping("tologin")
public String tologin(){
return "login";
}
//登錄驗(yàn)證
@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","賬號(hào)或密碼錯(cuò)誤");
return "login";
}
}
//轉(zhuǎn)向注冊(cè)頁面
@RequestMapping("toregister")
public String toregister(){
return "register";
}
//注冊(cè)
@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";
}
}
以上就是關(guān)于“SSM登錄注冊(cè)功能的實(shí)現(xiàn)”介紹,大家如果想了解更多相關(guān)知識(shí),不妨來關(guān)注一下動(dòng)力節(jié)點(diǎn)的SSM視頻教程,里面的課程內(nèi)容詳細(xì)全面,通俗易懂,由淺到深,適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對(duì)大家能夠有所幫助。
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)