structure
SysUserController.java
package lx.controller;
import lx.entity.model.SysUser;
import lx.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/user")
public class SysUserController {
@Autowired
private SysUserService sysUserService;
/**
//
Successful login
request.getSession().setAttribute("loginUser", find);
* Login page
* @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 "Login verification failed! Account error, Wrong password or user disabled!";
return "ok";
}
// -----------add user
@GetMapping("/add")
public String add(Model model){
SysUser entity=new SysUser();
entity.setStatus(0);
model.addAttribute("item",entity);
return "/sysUser/register";
}
//新增-post
@PostMapping("/add")
@ResponseBody
public String add(SysUser entity){
System.out.println("234");
try {
sysUserService.insert(entity);
return "ok";
} catch (Exception e) {
return e.getMessage();
}
}
}
SysUser.java entity class
package lx.entity.model; //User entity class public class SysUser { private int id;//Primary key private String name;//User name private String account;//Login account private String password;//Login password private int status ;//Status 0 normal, 1 disabled public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name ) { this.name = name; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
SysUserMapper.java
package lx.mapper; import lx.entity.model.SysUser; import org.apache.ibatis.annotations.Param; import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Repository; @MapperScan @Repository public interface SysUserMapper { //登录验证 SysUser checkLogin(@Param("account")String account, @Param("password")String password); int insert(SysUser record); }
SysUserServiceImpl.java
package lx.service.impl; import lx.entity.model.SysUser; import lx.mapper.SysUserMapper; import lx.service.SysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class SysUserServiceImpl implements SysUserService { @Autowired private SysUserMapper sysUserMapper; @Override public SysUser checkLogin(String account, String password) { System.out.println(account+password+"lllx"); return sysUserMapper.checkLogin(account,password); } @Override public int insert(SysUser record) { System.out.println(record+"1111"); return sysUserMapper.insert(record); } }
SysUserService.java
package lx.service; import lx.entity.model.SysUser; public interface SysUserService { // 登录 SysUser checkLogin(String account, String password); // 增加用户 int insert(SysUser record); }
applicationContext.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: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
<context:component-scan base-package="lx"/>
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- Scan classes to complete Bean creation and automatic dependency injection -->
<mvc:annotation-driven /> <
!-- <!– Static resource access–>--> <
!-- <mvc :resources location="/images/" mapping="/images/**"/>--> <!--
<mvc:resources location="/css/" mapping="/css/**"/>- ->
<!-- <mvc:resources location="/js/" mapping="/js/**"/>--> <!
-- Configure view resolver -->
<bean class="org.springframework .web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!--数据库配置 -->
<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 id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close ">
</bean>
<!-- database connection pool -->
<!-- 基本属性 url、user、password -->
<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"/>
<!-- Configure the waiting time for obtaining connection timeout -->
<!--Load mapper interface-->
</bean>
<!-- How long does it take to configure the interval to detect idle connections that need to be closed, in milliseconds -- >
<property name="maxWait" value="60000"/>
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Scanned interface package SSM0411.mapper -->
<property name="basePackage" value="lx.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSession"></property>
</bean>
<!-- Configure Transaction Manager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource. DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Interceptor-->
<mvc:interceptors>
<!-- Internationalization operation interceptor -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
</beans> SysUserMapper.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="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="account" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="status" property="status" jdbcType="INTEGER" /> </resultMap>
<!-- Obtain entity based on login account--> <select id="checkLogin" resultMap="BaseResultMap"> select * from sys_user where account=#{account} and password=#{password} and status=0 </select >
<insert id="insert" parameterType="lx.entity.model.SysUser" > insert into sys_user (id, name, account, password, status) values (#{id}, #{name}, #{account}, #{password}, #{status}) </insert>
jdbc.properties
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 username=root password=502216 #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570 initialSize=0 #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570 maxActive=20 #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F4 maxIdle=20 #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F4 minIdle=1 #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4 maxWait=60000
index.jsp
<%-- Created by IntelliJ IDEA. User: lx Date: 2022/4/18 Time: 13:52 To change this template use File | Settings | File Templates. --%> <%@ 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-migrate-1.1.1.js"></script>--%> //ajax login verification, successful Jump to the home page, otherwise an error message will be prompted <script> <%-- <script src="/jxc/js/layer/layer.js"></script>--%> function onLogin(){ var account=$("#account").val(); var password=$("#password").val(); console.log(account+password); // $.ajax({ // type: "post", // url: "user/login", // data: [account=account,passwrod=passwrod], // dataType: "text", // success: function (rdata) { // if (rdata=="ok") { // window.location.href="/home/index" // } else { // alert(rdata); // } // } // }); $.post("user/login",{ account:account, password:password }, function (rdata){ if (rdata=="ok") { window.location.href="home/index" } else { alert(rdata); } } ) } </script> </head> <body> <div class="container"> <h1>用户登录</h1> <form> <div class="row"> <span>登录账号:</span> <input type="text" class="textBox" id="account" name="account" /> </div> <div class="row"> <span>Login password:</span> <input type="password" class="textBox" id="password" name="password" /> </div> <div class="row"> <input type="button" class="btn2" value="login " οnclick="onLogin()" /> </div> <div class="row"> No account? <a href="${pageContext.request.contextPath }/user/add">Register account</a> </div> </form> </div> </body> </html>
register.jsp
<%-- Created by IntelliJ IDEA. User: lx Date: 2022/4/18 Time: 19:29 To change this template use File | Settings | File Templates. --%> <%@ page language="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/loose.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>--%> return; } <script type="text/javascript"> function onSave() { let name=$("#name").val(); let account=$("#account").val(); let password=$("#password").val() console.log(name+ account+password) //1. Verify input //username if ($("#name").val().trim() == ""){ alert("Please enter the username"); $(" #name").focus(); return; } //login account if ($("#account").val().trim() == "") { alert("Please enter the login account"); $ ("#account").focus(); //2. Submit data $.post("add",{ name:name, account:account, password:password }, function (rdata){ if (rdata=="ok") { // window.location.href="/user/index" alert("注册成功") } else { alert(rdata); } }); } function onCancel() { // parent.layer.close(parent.layer.index);//Close the pop-up window <div class="editor-label">Username</div> <legend>User Information</legend> <fieldset> </head> </script> } <body> <form> <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.password}" name="password" id="password" /> </div> </fieldset> </form> <div class="toolbar"> <input type="button" class="btn2" value="注册" οnclick="onSave()" /> <input type="button" class="btn2" value="取消" οnclick="onCancel()" /> </div> </body> </html>
views/home/index.jsp is the content of successful login
Summarize
record it