Springmvc + mybatis + mysql: A simple logon example

Source: Internet
Author: User
Springmvc + mybatis + mysql integrated a simple login example today using spring and mybatis integrated to write a simple login example, the first integration, give yourself a note, there may be a little less comments. If you do not have enough comments, thank you for pointing out and sharing them with your friends. The following shows the logon Failure and Success examples:

A simple login example integrating spring mvc + mybatis + mysql today I used spring and mybatis to write a simple login example. I integrated it for the first time and made a note for myself, there may be a little less comments. If you do not have enough comments, thank you for pointing out and sharing them with your friends. The following shows the logon Failure and Success examples:

A simple logon example of spring mvc + mybatis + mysql Integration
Today, I wrote a simple login example using spring and mybatis integration. For the first integration, I made a note for myself. Maybe I wrote a little less comments. Thank you for pointing out the shortcomings, we also share the information with our friends who need it. The following describes the logon failures and successes:

The tool used in this login example is myeclipse8.6 + mysql5.1, the technology used is spring3.0 + mybatis3.2.3 + mybatis-spring-1.1.1 (this is spring and mybatis integrated package), the overall structure of the project

Now we have to do is create a web project in myeclipse tool, and add spring support, do not understand friends can view http://blog.csdn.net/ooliuyunoo/article/details/19908661

After the project is created, we create the Project in order:

1: Create a vo class. The Code is as follows:

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: Create the UserDaoIMP interface:

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: Create the UserDaoIMP. xml configuration file:

 
 
  select * from user where name=#{name} and pwd=#{pwd}
  
   insert into user(name,pwd) values(#{name},#{pwd})
  
  
   update user set name=#{name} where id=#{id}
  
  
   delete from user where id=#{id}
  
 


4: Create the UserServiceIMP service interface:

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: Create a UserService inherited service interface:

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 stubreturn this.deleteUserById(userId);}public int insertUser(UserVO uservo) {// TODO Auto-generated method stubreturn this.userdao.insertUser(uservo);}public UserVO selectUser(UserVO uservo) {// TODO Auto-generated method stubreturn this.userdao.selectUser(uservo);}public int updaqteUser(UserVO uservo) {// TODO Auto-generated method stubreturn this.userdao.updaqteUser(uservo);}public void setUserdaoIMP(UserDaoIMP userdaoIMP) {this.userdao = userdaoIMP;}public UserDaoIMP getUserdaoIMP() {return userdao;}}


6. Create a login controller:

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 stubString 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
 
  
Model = new HashMap
  
   
(); If (userService. selectUser (uservo )! = Null) {uservo = userService. selectUser (uservo); System. out. println ("information available"); model. put ("uservo", uservo); return new ModelAndView ("WEB-INF/Main. jsp ", model);} else {System. out. println ("No information found"); model. put ("error", "username or password error"); return new ModelAndView ("WEB-INF/Login. jsp ", model) ;}} public void setUserService (UserService userService) {this. userService = userService;} public UserService getUserService () {return userService ;}}
  
 


7. Create a New mybatis-config.xml profile:

 
 
      
   
   
  
 

8: Configure applicationCotext. xml:

 
 
  
   
   
   
   
  
  
   
   
  
  
   
   
  
  
   
  
  
   
  
  
   
    
     
      LoginController
     
    
   
  
 


9: Modify the body of index. jsp:

      
   


10: Add login. jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'Login.jsp' starting page    
 
 
     
 
 
               ${error}  


11: Add main. jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>              My JSP 'main. jsp 'starting page    
 
 
     
 
 
       Go to the homepage
Your basic information is as follows:
Id: $ {uservo. id}
Username: $ {uservo. name}
Password: $ {uservo. pwd}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.