spring mvc + mybatis + mysql 调整的一个简单的登录例子

WBOY
发布: 2016-06-07 16:24:54
原创
1593 人浏览过

spring mvc + mybatis + mysql 整合的一个简单的登录例子 今天用spring跟mybatis整合写了一个简单的登录例子,第一次整合,给自己做个笔记,可能注释写的有点少,做的不足的地方谢谢指出,也分享给需要的朋友,下面给出登录失败和成功的效果图: 这个登录例子

spring mvc + mybatis + mysql 整合的一个简单的登录例子
今天用spring跟mybatis整合写了一个简单的登录例子,第一次整合,给自己做个笔记,可能注释写的有点少,做的不足的地方谢谢指出,也分享给需要的朋友,下面给出登录失败和成功的效果图:

 

     这个登录例子用的工具是myeclipse8.6+mysql5.1,使用到的技术有spring3.0+mybatis3.2.3+mybatis-spring-1.1.1(这个是spring跟mybatis整合的包),项目的整体结构如图:

 

    现在我们要做的就是在myeclipse工具里新建一个web项目,并且添加spring 支持,不懂的朋友可以查看http://blog.csdn.net/ooliuyunoo/article/details/19908661

    项目新建完之后我们就把项目分次序把项目新建起来:

   1: 新建vo类,代码如下:

     

package com.li.vo;

public class UserVO {
	private int id;
	private String name;
	private String pwd;
	public UserVO(){}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getPwd() {
		return pwd;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
}
登录后复制


 

    2:  新建UserDaoIMP接口:

package com.li.IMP;

import com.li.vo.UserVO;

public interface UserDaoIMP {
	public UserVO selectUser(UserVO uservo);
	public int insertUser(UserVO uservo);
	public int updaqteUser(UserVO uservo);
	public int deleteUserById(int user_id);
}
登录后复制


 

   3: 新建UserDaoIMP.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>

	<mapper namespace="com.li.IMP.UserDaoIMP">
		<select id="selectUser" parametertype="com.li.vo.UserVO" resulttype="com.li.vo.UserVO">
			select * from user where name=#{name} and pwd=#{pwd}
		</select>
		<insert id="insertUser" parametertype="com.li.vo.UserVO" flushcache="true">
			insert into user(name,pwd) values(#{name},#{pwd})
		</insert>
		<update id="updateUser" parametertype="com.li.vo.UserVO">
			update user set name=#{name} where id=#{id}
		</update>
		<delete id="deleteUser" parametertype="int">
			delete from user where id=#{id}
		</delete>
	</mapper>
登录后复制


 

    4:新建UserServiceIMP服务接口:

package com.li.service;

import com.li.vo.UserVO;

public interface UserServiceIMP {
	public UserVO selectUser(UserVO uservo);
	public int insertUser(UserVO uservo);
	public int updaqteUser(UserVO uservo);
	public int deleteUserById(int user_id);
}
登录后复制


 

     5:新建UserService继承服务接口:

package com.li.service;

import com.li.IMP.UserDaoIMP;
import com.li.vo.UserVO;

public class UserService implements UserServiceIMP {
	private UserDaoIMP userdao;
	public int deleteUserById(int userId) {
		// TODO Auto-generated method stub
		return this.deleteUserById(userId);
	}

	public int insertUser(UserVO uservo) {
		// TODO Auto-generated method stub
		return this.userdao.insertUser(uservo);
	}

	public UserVO selectUser(UserVO uservo) {
		// TODO Auto-generated method stub
		return this.userdao.selectUser(uservo);
	}

	public int updaqteUser(UserVO uservo) {
		// TODO Auto-generated method stub
		return this.userdao.updaqteUser(uservo);
	}

	public void setUserdaoIMP(UserDaoIMP userdaoIMP) {
		this.userdao = userdaoIMP;
	}

	public UserDaoIMP getUserdaoIMP() {
		return userdao;
	}

}
登录后复制


 

    6:新建一个登陆控制器:

package com.li.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.li.IMP.UserDaoIMP;
import com.li.service.UserService;
import com.li.vo.UserVO;

public class LoginController implements Controller {
	private UserService userService;
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String name=request.getParameter("userName");
		String password=request.getParameter("password");
		//System.out.println("name-----"+name+"----password-----"+password);
		UserVO uservo=new UserVO();
		uservo.setName(name);
		uservo.setPwd(password);
		Map<string object> model=new HashMap<string object>();
		if(userService.selectUser(uservo)!=null){
			uservo=userService.selectUser(uservo);
			System.out.println("能查到信息");
			model.put("uservo", uservo);
			return new ModelAndView("WEB-INF/Main.jsp",model);
		}else{
			System.out.println("查不到信息");
			model.put("error", "用户名或密码错误");
			return new ModelAndView("WEB-INF/Login.jsp",model);
		}
		
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public UserService getUserService() {
		return userService;
	}


}
</string></string>
登录后复制


 

    7: 新建一个mybatis-config.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
<mappers>
    <mapper resource="com/li/IMP/UserDaoIMP.xml"></mapper><!--mapper对应的xml配置文件-->
</mappers>
</configuration>
登录后复制

 

 8:配置 applicationCotext.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:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value=""></property>
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.li.IMP.UserDaoIMP"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	<bean id="userService" class="com.li.service.UserService">
		<property name="userdaoIMP" ref="userDao"></property>
	</bean>
	<bean id="LoginController" class="com.li.controller.LoginController">
		<property name="userService" ref="userService"></property>
	</bean>
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="login.do">LoginController</prop>
			</props>
		</property>
	</bean>

</beans>
登录后复制


 

     9: 修改index.jsp的body部份为:

 

  
    <forward page="WEB-INF/Login.jsp"></forward>
  
登录后复制


 

    10:添加login.jsp:

 

  





  
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  
  
  
    	
登录后复制


${error}


 

11: 添加main.jsp:





  
    <base href="<%=basePath%>">
    
    <title>My JSP 'Main.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  
  
  
    进入主页面<br>
    你的基本信息如下:<br>
    id:${uservo.id}<br>
    用户名:${uservo.name}<br>
    密码:${uservo.pwd}<br>
  
登录后复制

 


 

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!