使用ssm框架实现一个简单的注册功能

发布时间:2023-05-22 08:30

首先需要一个数据库, 里面存放id,用户名,密码

sql语句

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

前端页面regist.jsp

<%@ page contentType=\"text/html;charset=UTF-8\" language=\"java\" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action=\"register\" method=\"post\" >
    用户名:<input type=\"text\" name=\"username\"/><br/>
    密码:<input type=\"password\" name=\"password\"/><br/>
    <input type=\"submit\" value=\"注册\"/><br/>
</form>
</body>
</html>

控制器registerController

@Controller
public class UsersController {
    @Autowired
    private UserService userService;
    //先到注册页面去
    @RequestMapping(\"to_register\")
    public String to_register(){
        return \"regist\";
    }
    //实现注册的方法
    @RequestMapping(\"register\")
    public String register(Users users)  {

        int index = userService.insUser(users);
        if (index>0){
        	//添加成功,跳转到成功页面
            return \"main\";
        }else{
        //反之 重定向到注册页
            return \"redirect:register\";
        }

    }~

UserServiceImpl

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public int insUser(Users users) {
        return userMapper.insUser(users);
    }
}~

UserMapper代码

@Repository
public interface UserMapper {
    int insUser(Users users);
}

UserMapper.xml

<mapper namespace=\"com.hu.mapper.UserMapper\">
<insert id=\"insUser\" parameterType=\"Users\">
            insert into users values (default ,#{username}, #{password})
        </insert>
</mapper>~

注册页面还是很简单的,主要就是写一个添加(insert)用户的方法,
然后往数据库中去插入数据就可以了,然后每层调用每层,明天写一个登录的方法~~~~~~~~

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号