人生第一把ssm實現login

話不多說開整

1.項目結構


2.配置文件

a.web.xml


b.springmvc.xml



c.sqlmapper.xml(雞肋雞肋)


d.applicationContext-dao.xml


e.applicationContext-service.xml


f.applicationContext-trans.xml


3.pojo實體類(數據庫就省了吧!)

package com.ithyl.pojo;


public class User {
private int id;
private String username;
private String password;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}


4.dao與數據庫建立聯繫

a...

package com.ithyl.mapper;


import com.ithyl.pojo.User;


public interface UserDao {
   public User selectByUN(String username);
   public User selectByPW(String password);
}

b:

<?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
          "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
    <!--namespace用於與DAO層的接口類進行綁定,這樣我們無需實現DAO層的接口
    類,其接口類就能夠自動的找到相關的SQL語句進行綁定實現-->
  <mapper namespace="com.ithyl.mapper.UserDao">
     <!--select表示查詢,它的id名稱必須與DAO層接口的方法名相同,否則無法綁定-->
     <select id="selectByUN" parameterType="string" resultType="com.ithyl.pojo.User">
         select * from tb_user where username = #{username}
     </select>
 
     <select id="selectByPW" parameterType="int" resultType="com.ithyl.pojo.User">
 
          select * from tb_user where password= #{password}
     </select>
 
 
</mapper>

5.service業務層先來個藉口寫抽象方法

package com.ithyl.service;


public interface UserService {
public boolean login(String username,String password);
}

在寫個實現類、、、、、、、、、、、、

package com.ithyl.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.ithyl.mapper.UserDao;
import com.ithyl.pojo.User;
//@Service("UserService") 註解用於標示此類爲業務層組件,在使用時會被註解的類會自動由
    //spring進行注入,無需我們創建實例
@Service("UserService")
public class UserServiceImpl implements UserService {
    //自動注入iuserdao 用於訪問數據庫
    @Autowired
    UserDao Mapper;

   //登錄方法的實現,從jsp頁面獲取username與password
    public boolean login(String username, String password) {
//        System.out.println("輸入的賬號:" + username + "輸入的密碼:" + password);
        //對輸入賬號進行查詢,取出數據庫中保存對信息
        User user = Mapper.selectByUN(username);
        if (user != null) {
//            System.out.println("查詢出來的賬號:" + user.getUsername() + "密碼:" + user.getPassword());
//            System.out.println("---------");
            if (user.getUsername().equals(username) && user.getPassword().equals(password))
                return true;
 
        }
        return false;

   }


}

6.業務當然要有控制類controller來控制

package com.ithyl.controller;


import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.ithyl.pojo.User;
import com.ithyl.service.UserService;


//@Controller註解用於標示本類爲web層控制組件
@Controller
//@RequestMapping("/user")用於標定訪問時對url位置
//@RequestMapping("/user")
//在默認情況下springmvc的實例都是單例模式,所以使用scope域將其註解爲每次都創建一個新的實例
@Scope("prototype")
public class Login {
  //自動注入業務層的userService類
  @Autowired
      UserService userService;


  //login業務的訪問位置爲/user/login
  @RequestMapping("/login")
     public String login(User user,HttpServletRequest request){
      //調用login方法來驗證是否是註冊用戶
      boolean loginType = userService.login(user.getUsername(),user.getPassword());
      if(loginType){
          //如果驗證通過,則將用戶信息傳到前臺
          request.setAttribute("user",user);
          //並跳轉到success.jsp頁面
          return "success";
      }else{
          //若不對,則將錯誤信息顯示到錯誤頁面
          request.setAttribute("message","用戶名密碼錯誤");
          return "error";
      }
  }、

jsp就算了吧!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章