初学SSM试着做了个登录注册实例检验一下,顺便把代码搬上来了
废话不多说直接撸
数据库版本:mysql-8.0.13(版本较高,jar包和一些语法会有出入)
开发工具:Eclipse
俩字段,很简单,只是方便测试
CREATE TABLE test (
name varchar(20) NOT NULL,
pwd varchar(20) DEFAULT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
b8_c301
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
1
springmvc
*.action
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
//注意数据库版本
//注意数据库版本,8.0版本中url要设置时区,并且xml配置文件中要使用“&”来代替“&”
//直接加载的sql映射文件,省去配置mybatis-config.xml的步骤
package com.cn.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cn.entiy.User;
import com.cn.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/golog")
public String golog() {
return "login";
}
@RequestMapping("/goreg")
public String goreg() {
return "register";
}
@RequestMapping("/register")
public String login(User user) {
userService.saveUser(user);
return "succ";
}
@RequestMapping("/login")
public String register(String name,String pwd) {
User user=userService.findUser(name, pwd);
if(user!=null) {
return "succ";
}else {
return "fail";
}
}
}
package com.cn.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.cn.entiy.User;
@Repository
@Mapper
public interface UserMapper {
void saveUser(User user);
User findUser(@Param("name")String name,@Param("pwd")String pwd);
}
insert into test (name,pwd) values (#{name},#{pwd})
package com.cn.entiy;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [name=" + name + ", pwd=" + pwd + "]";
}
}
package com.cn.service;
import org.springframework.stereotype.Service;
import com.cn.entiy.User;
public interface UserService {
void saveUser(User user);
User findUser(String name, String pwd);
}
package com.cn.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cn.dao.UserMapper;
import com.cn.entiy.User;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapepr;
@Override
public void saveUser(User user) {
// TODO Auto-generated method stub
userMapepr.saveUser(user);
}
@Override
public User findUser(String name, String pwd) {
// TODO Auto-generated method stub
return userMapepr.findUser(name, pwd);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
注册
succ.jsp和fail.jsp随便写点测试文字就行了,我就不沾上来了
将项目部署到tomcat上发布,然后在浏览器中输入以下地址,会有以下注册界面(密码框的格式为text,所以是明文)出现,点击提交会返回succ.jsp
在浏览器中输入以下地址,会有如下登录界面,点击提交如果匹配数据库返回succ.jsp,未匹配则返回fail.jsp
最后贴一个为什么要在spring中扫描service,在springmvc中扫描controller的贴
2019年11月22日更新