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

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;

@Repository
public 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;


@Controller
public 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配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	">
	<context:component-scan base-package="com" />
	 <bean id="dataSource"  
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName"  
            value="com.mysql.jdbc.Driver">  
        </property>  
        <property name="url"  
            value="jdbc:mysql://localhost:3306/mydata">  
        </property>  
        <property name="username" value="root"></property>  
        <property name="password" value="1996112lin"></property>  
    </bean>  
  
    <bean id="jdbcTemplate"  
        class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"  
        lazy-init="false">  
        <property name="dataSource">  
            <ref bean="dataSource" />  
        </property>  
    </bean>  
	
	
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

	<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	</beans>

Web.XMl:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
    <servlet>
  	<servlet-name>spring</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 统一字符编码 -->
  <filter>
  	<filter-name>encoding</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>encoding</filter-name>
  	<url-pattern>*.do</url-pattern>
  </filter-mapping>
</web-app>
发布了210 篇原创文章 · 获赞 60 · 访问量 18万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览