SSM フレームワークは、ユーザーのログインおよび登録機能を実装します。


構造

 SysUserController.java

パッケージ lx.controller; 

lx.entity.model.SysUser をインポートします。
lx.service.SysUserService をインポートします。
org.springframework.beans.factory.annotation.Autowired をインポートします。
org.springframework.stereotype.Controller をインポートします。
org.springframework.ui.Model をインポートします。
org.springframework.web.bind.annotation.GetMapping をインポートします。
org.springframework.web.bind.annotation.PostMapping をインポートします。
org.springframework.web.bind.annotation.RequestMapping をインポートします。
org.springframework.web.bind.annotation.ResponseBody をインポートします。

インポート javax.servlet.http.HttpServletRequest; 

@Controller 
@RequestMapping("/user") 
public class SysUserController {  
    @Autowired
    private SysUserService sysUserService; 
    /** 
     * ログイン ページ
     * @return 
     */ 
    @GetMapping("/tologin") 
    public String login() { 
        System.out.println("nnn"); 
        return "/sysUser/login"; 
    } 


    @ PostMapping("/login") 
    @ResponseBody 
    public String login(SysUser sysUser, HttpServletRequest request){ 
        SysUser find = sysUserService.checkLogin(sysUser.getAccount(),sysUser.getPassword()); 
        if(find==null){ 
            return "ログイン検証に失敗しました! アカウントが間違っているか、パスワードが間違っているか、ユーザーが無効になっています!"; } 
        //
        ログイン成功
        request.getSession().setAttribute("loginUser", find);
        「OK」を返します。
    } 
// -----------增加用户
    @GetMapping("/add") 
    public String add(Model model){ 
        SysUserentity=new SysUser(); getMessage();  
} 
    }
        エンティティ.setStatus(0); 
        model.addAttribute("アイテム",エンティティ); 
        "/sysUser/register" を返します。
    } 
    //新增-post 
    @PostMapping("/add") 
    @ResponseBody 
    public String add(SysUserentity){ 
        System.out.println("234"); 
        { 
            sysUserService.insert(entity);を試してください。
            「OK」を返します。
        } catch (例外 e) { 
            return e.getMessage(); 
        SysUser.java实体类
package lx.entity.model; 
//ユーザー エンティティ クラス
public class SysUser { 
    private int id;//主キー
    private String name;//ユーザー名
    private String account;//ログイン アカウント
    private String パスワード;//ログイン パスワード
    private int status ;//ステータス 0 正常、1 無効

    public int getId() { 
        return id; 
    } 

    public void setId(int id) { 
        this.id = id; 
    } 

    public String getName() { 
        return name; 
    } 

    public void setName(String名前 ) { 
        this.name = 名前; 
    } 

    public String getAccount() {
        アカウントを返す; 
    }

    public void setAccount(String account) { 
        this.account = アカウント; 
    public 

    String getPassword() {
        パスワードを返します。
    public 

    void setPassword(String パスワード) { 
        this.password = パスワード; 
    public 

    int getStatus() {
        ステータスを返します。
    public 

    void setStatus(int status) { 
        this.status = ステータス; 
    } 
}
SysUserMapper.java
パッケージ lx.mapper; 

lx.entity.model.SysUser をインポートします。
org.apache.ibatis.annotations.Param をインポートします。
org.mybatis.spring.annotation.MapperScan をインポートします。
org.springframework.stereotype.Repository をインポートします。

@MapperScan 
@Repository 
public Interface SysUserMapper { 

    //登录验证
    SysUser checkLogin(@Param("account")String アカウント, @Param("password")String パスワード); 

    int insert(SysUser レコード); 
}
SysUserServiceImpl.java
パッケージ lx.service.impl; 

lx.entity.model.SysUser をインポートします。
lx.mapper.SysUserMapperをインポートします。
lx.service.SysUserService をインポートします。
org.springframework.beans.factory.annotation.Autowired をインポートします。
org.springframework.stereotype.Service をインポートします。
org.springframework.transaction.annotation.Transactional をインポートします。

@Service 
@Transactional 
public class SysUserServiceImplimplements SysUserService { 
@Autowired 
    private SysUserMapper sysUserMapper; 
    @Override 
    public SysUser checkLogin(String アカウント, String パスワード) { 
        System.out.println(account+password+"llllx"); 
        return sysUserMapper.checkLogin(アカウント,パスワード); 
    }

    @Override 
    public int insert(SysUser Record) { 
        System.out.println(record+"1111"); 
        return sysUserMapper.insert(レコード); 
    } 
}
SysUserService.java
パッケージ lx.service; 

lx.entity.model.SysUser をインポートします。

public Interface SysUserService { 
// 登录
    SysUser checkLogin(String アカウント, String パスワード); 
// 增加用户
   int insert(SysUser Record); 
}

applicationContext.xml

<?xml version="1.0"coding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance" 
       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-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/mvc  
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <!-- クラスをスキャンして Bean の作成と自動依存関係の注入を完了します --> 
    <context:component-scan base-package="lx"/>
    <mvc:annotation-driven /> 
<!-- <!– 静态资源访问 –>--> 
<!-- <mvc:resources location="/images/" Mapping="/images/**"/>--> <!-- <mvc:resources 
location=" /css/" マッピング="/css/**"/>--> 
<!-- <mvc:resources location="/js/" マッピング="/js/**"/>--> <! 
    - - 配置ビュー画像解析器 --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="viewClass" value="org.springframework.web.servlet.view. 
    <!--数据库構成 -->
    <bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="locations"> 
            <list> 
                <value>classpath:jdbc.properties</value> 
            </list> 
        </property> 
    </bean> 
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <!-- データソース注入 --> 
        <property name="dataSource" ref="dataSource"/ > 
        <!-- 映射文件扫描 --> 
        <property name="mapperLocations" value="classpath:SysUserMapper.xml" /> 
        <!-- 别名包 --> 
        <property name="typeAliasesPackage" value="lx.entity.model"></property> 
    </bean>model"></property> 
    <!-- データベース接続プール --> 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close ">
        <!--基本プロパティ url、ユーザー、パスワード --> 
        <property name="driverClassName" value="${driver}"/> 
        <property name="url" value="${url}"/> 
        <property name="username" value="${username}"/> 
        <property name="password" value="${password}"/> 
        <!-- 配置初期化大小、最小、最大 --> 
        <property name= "initialSize" value="10"/> 
        <property name="minIdle" value="10"/> 
        <property name="maxActive" value="50"/> 
        <!-- 接続タイムアウトを取得するための待ち時間を設定します --> 
        <property name="maxWait" value="60000"/> 
        <!-- 閉じる必要があるアイドル状態の接続を検出する間隔を設定するのにどのくらいの時間がかかりますか。ミリ秒単位 -- > 
    <!--ロードマッパーインターフェイス--> 
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
    </bean> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <!-- スキャンされたインターフェイス パッケージ SSM0411.mapper --> 
        <property name="basePackage" value="lx.mapper"/> 
        < property name="sqlSessionFactoryBeanName" value="sqlSession"></property> 
    </bean> 
    <!-- トランザクション マネージャーの構成 --> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource. DataSourceTransactionManager" > 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 
    <!-- インターセプター--> 
    <mvc:interceptors> 
        <!-- 国際化操作インターセプター --> 
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 
    </mvc:interceptors>
</beans> SysUserMapper.xml

<?xml version="1.0"coding="UTF-8" ?> 
<!DOCTYPE マッパー PUBLIC "-//mybatis.org//DTD マッパー 3.0//EN" "http://mybatis.org/dtd/mybatis -3-mapper.dtd" > 
<mapper namespace="lx.mapper.SysUserMapper" > 
    <resultMap id="BaseResultMap" type="lx.entity.model.SysUser" > 
        <id column="id" property="id" jdbcType="INTEGER" /> 
        <result column="name" property="name" jdbcType="VARCHAR" /> 
        <result column="account" property= "アカウント" jdbcType="VARCHAR" /> 
        <result column="パスワード" property="パスワード" jdbcType="VARCHAR" /> 
        <result column="status" property="status" jdbcType="INTEGER" /> 
    </結果マップ>
<!-- ログイン アカウントに基づいてエンティティを取得します --> 
<select id="checkLogin" resultMap="BaseResultMap"> 
    select * 
    from sys_user 
    where account=#{account}、password=#{password}、status=0 
</選択 >
<insert id="insert"parameterType="lx.entity.model.SysUser" > 
    sys_user (ID、名前、アカウント、パスワード、ステータス)
    値 (#{id}、#{name}、#{account}、 #{パスワード}、#{ステータス}) 
</挿入>

jdbc.プロパティ

driver=com.mysql.cj.jdbc.Driver 
#mytest\u662F\u672C\u5730\u6570\u636E\u5E93\u540D 
url=jdbc:mysql://localhost:3306/smm?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT %2B8 
#\u4E0B\u9762\u8F93\u5165\u81EA\u5DF1\u6570\u636E\u5E93\u7684\u8D26\u53F7\u548C\u5BC6\u7801 ユーザー名=root パスワード=502216 #\u5B9A\u4E49\u52 1D\ 
u59CB 
\ 
u8FDE \u63A5\u6570InitialSize 
=0 
#\u5B9A\u4E49\u6​​700\u5927\u8FDE\u63A5\u6570 
maxActive=20 
#\u5B9A\u4E49\u6​​700\u5927\u7A7A\u95F4 
maxIdle=20 
#\u5B9A\u4E49\u 6700\u5C0F \u7A7A\u95F4 
minIdle=1 
#\u5B9A\u4E49\u6​​700\u957F\u7B49\u5F85\u65F6\u95F4 
maxWait=60000

インデックス.jsp

<%-- 
  IntelliJ IDEA によって作成されました。
  ユーザー: lx
  日付: 2022/4/18
  時間: 13:52
  このテンプレートを変更するには、[ファイル] | [ファイル] を使用します。設定 | ファイルテンプレート。
--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>用户登
</title> 
    < 
    % -- <link href="/jxc/css/login.css" rel="stylesheet" />--%> 
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/ jquery.min.js"></script> 
    <%-- <script src="/jxc/js/jquery-merge-1.1.1.
        関数 onLogin(){ 
            // }); 
            $.post("ユーザー/ログイン",{
            var パスワード=$("#パスワード").val(); 
            console.log(アカウント+パスワード); 
            // $.ajax({ 
            // type: "post", 
            // url: "user/login", 
            // data: [account=account,passwrod=passwrod], 
            // dataType: "text", 
            // 成功: function (rdata) { 
            // if (rdata=="ok") { 
            // window.location.href="/home/index" 
            // } else { 
            //alert(rdata); 
            // } 
            // }
                アカウント:アカウント、
                パスワード:
                                href="ホーム/インデックス" } else {
                                    アラート(rdata); 
                                } 
                } 
            ) 
        } 
    </script> 
</head> 
<body> 
<div class="container"> 
    <h1>用户登录</h1> 
    <form> 
        <div class="row"> 
        </div> 
        <div class ="row"> 
            <span>登录密码:</span> 
            <input type="password" class="textBox" id="password" name="password" /> 
            <span>登录账号:< /span> 
            <input type="text" class="textBox" id="account" name="account" /> 
        </div> 
        <div class="row"> 
            <input type="button" class="btn2" value="login " οnclick="onLogin()" /> 
        </div> 
        <div class="row">
            アカウントをお持ちですか? <a href="${pageContext.request.contextPath }/user/add">アカウントの登録</a> 
        </div> 
    </form> 
</div> 
</body> 
</html>

register.jsp

<%-- 
  IntelliJ IDEA によって作成されました。
  ユーザー: lx
  日付: 2022/4/18
  時刻: 19:29
  このテンプレートを変更するには、[ファイル] | [ファイル] を使用します。設定 | ファイルテンプレート。
--%> 
<%@ ページ言語="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri=" 
http:/ /java.sun.com/jsp/jstl/core"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/ゆるい。
dtd"> <html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>新增</title> 
<%-- <link href="/ssm1/css/edit.css" rel="stylesheet" />--%> 
    <script src=" https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 
<%-- <script src="/ssm1/js/layer/layer.js"></script>--%> 
    <script type=" text/javascript"> 
        function onSave() { 
            let name=$("#name").val(); 
            let account=$(" #account").val(); 
            letpassword=$("#password").val() 
            console.log(name+account+password) 
            //1. 入力を確認します
            //ユーザー名
            if ($("#name ").val().trim() == ""){ 
                alert("ユーザー名を入力してください"); 
                $("#name").focus(); 
                return; 
            } 
            //ログインアカウント
            if ($(" #アカウント").val()。トリム() == "") {
                アラート("ログインアカウントを入力してください"); 
                $("#アカウント").focus(); 
                return; 
            }
            //2. データを送信
            $.post("add",{ 
<body> 
<form>post("追加",{
                    名前:名前,
                    アカウント:アカウント,
                    パスワード:パスワード
                }, 
                function (rdata){ 
                    if (rdata=="ok") { 
                        // window.location.href="/user/index" 
                    alter("注册成功") 
                    } else { 
                        alert( rdata); 
                    } 
                }); 
        } 
        function onCancel() { 
            //parent.layer.close(parent.layer.
    <fieldset> 
        <legend>用户情報</legend> 
</form> 
<div class="toolbar">
        <div class="editor-field"> 
            <input type="text" value="${item.name}" name="name" id="name" /> </div> 
        < 
        div class="editor- label">登录账号</div> 
        <div class="editor-field"> 
            <input type="text" value="${item.account}" name="account" id="account" /> 
        </ div> 
        <div class="editor-label">登录密码</div> 
        <div class="editor-field"> 
            <input type="password" value="${item.パスワード}" name="パスワード" id="パスワード" /> 
        </div> 
    </fieldset> 
    <input type="button" class="btn2" value="注册" οnclick="onSave()" /> 
    < input type="button" class="btn2" value="取消" οnclick="onCancel()" /> 
</div>
</body> 
</html>

views/home/index.jsp は、成功したログインの内容です


要約する

それを記録しなさい

おすすめ

転載: blog.csdn.net/weixin_60249074/article/details/124259558