博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring+Spring MVC+Spring JDBC+MySql实现简单登录注册
阅读量:2442 次
发布时间:2019-05-10

本文共 3692 字,大约阅读时间需要 12 分钟。

Spring+Spring MVC+Spring JDBC+MySql实现简单登录注册

工程目录:

Model层:

package com.model;public class User {	private int id;	private String name;	private String password;	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 getPassword() {		return password;	}	public void setPassword(String password) {		this.password = password;	}	}

UserDao:

package com.userdao;public interface UserDao {		public boolean check(String name,String passwod);		public void addUser(String name,String password);		public boolean unit(String name);}

UserDaoImpl:

package com.userdaoimpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import com.userdao.UserDao;@Repositorypublic class UserDaoImpl implements UserDao{		@Autowired	JdbcTemplate jdbcTemplate;			public JdbcTemplate getJdbcTemplate() {		return jdbcTemplate;	}	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {		this.jdbcTemplate = jdbcTemplate;	}	@Override	public boolean check(String name, String passwod) {		String sql = "select count(id) from u2 where name=? and password=?";		int i  = jdbcTemplate.queryForInt(sql, new Object[]{name,passwod});		if (i>0) {			return true;		} else {			return false;		}	}	@Override	public void addUser(String name, String password) {			String sql = "insert into u2 (name,password) values(?,?)";			jdbcTemplate.update(sql,new Object[]{name,password});	}	@Override	public boolean unit(String name) {		String sql = "select count(id) from u2 where name=?";		int rs = jdbcTemplate.queryForInt(sql,new Object[]{name});		if (rs>0) {			return true;		}		return false;	}			}

Controller层:

package com.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.userdaoimpl.UserDaoImpl;@Controllerpublic class LoginController {		@Autowired	UserDaoImpl userDaoImpl;		public UserDaoImpl getUserDaoImpl() {		return userDaoImpl;	}	public void setUserDaoImpl(UserDaoImpl userDaoImpl) {		this.userDaoImpl = userDaoImpl;	}	@RequestMapping("/login")	public String login(String name,String password,Model model) {			boolean rs = userDaoImpl.check(name, password);			if (rs==true) {				model.addAttribute("name",name);				return "index";			} 			return "error";	}	@RequestMapping("/register")	public String register(String name,String password,Model model) {		boolean rs = userDaoImpl.unit(name);		if (rs==false) {			userDaoImpl.addUser(name, password);			return "login";		} 		return "error1";	}	}

Spring配置文件:

Web.XMl:

login.jsp
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
spring
/
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
*.do
你可能感兴趣的文章
joi 参数验证_使用Joi进行节点API架构验证
查看>>
react-notifications-component,一个强大的React Notifications库
查看>>
如何在Debian 10上设置SSH密钥
查看>>
如何在Debian 10上安装Node.js
查看>>
了解css_了解CSS的特异性
查看>>
emmet快速插入css_如何使用Emmet快速编写HTML
查看>>
graphql_GraphQL简介
查看>>
typescript 枚举_TypeScript枚举声明和合并
查看>>
flutter开发_加快Flutter开发的提示
查看>>
redis排序_如何在Redis中管理排序集
查看>>
使用Gatsby和Cosmic JS创建多语言网站
查看>>
redis 连接数据库_如何连接到Redis数据库
查看>>
如何在Ubuntu 18.04上对Redis服务器的性能进行基准测试
查看>>
如何在Ubuntu 18.04上安装Nginx
查看>>
如何在Ubuntu 20.04上安装Nginx
查看>>
如何构建和安装Go程序
查看>>
如何在Ubuntu 18.04上安装MySQL
查看>>
如何使用Node.js抓取网站
查看>>
锂离子电容_离子,电容和Android Studio
查看>>
用switch语句编写程序_如何在Go中编写switch语句
查看>>