springmvc+mybatis+oracle簡單登錄

之前整合了SpringMVC和mybatis
http://blog.csdn.net/nmj2015/article/details/72353936
現在我們在之前的基礎上做一個簡單的登錄功能。
關於註解:
Controller:@Controller
Service:@Service(“userServiceImpl”)
Dao:@Component(“usersDao”)
其中@Autowired是由SpringMVC自動匹配相應對象
一. 在dao中新建根據user_name查詢Priv_User信息的方法,以便於我們通過用戶名查詢用戶信息後與密碼匹配

public Priv_User queryByUserName(Object user_name);

並在mapper中實現此方法

<select id="queryByUserName" resultMap="BaseResultMap" parameterType="Object">
        select
        <include refid="Base_Column_List" />
        from PRIV_USER where user_name = #{user_id}
    </select>

二. 建立Priv_UserService接口

package eesofa.cn.service;  

import eesofa.cn.bean.Priv_User;


public interface Priv_UserService {

    /****
     * 登錄
     * @param user_name
     * @param password
     * @return
     */
    public Priv_User login(String user_name, String password);
}

三. 實現此接口,在這裏用@Autowired來註解Priv_UserDao,並且處理一些登錄邏輯

@Service("priv_UserService")
public class IPriv_UserService implements Priv_UserService {

    @Autowired
    private Priv_UserDao priv_UserDao;

    @Override
    public Priv_User login(String user_name, String password) {
        // TODO Auto-generated method stub
        if (user_name != null && password != null && !user_name.equals("")&& !password.equals("")) {
            Priv_User user = priv_UserDao.queryByUserName(user_name);
            if (user!=null&&user.getPassword()!=null&&user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

}

四. 在Crotoller中寫登錄方法,如果登錄成功跳轉到success,失敗跳轉fail

@Autowired
    private Priv_UserService priv_UserService;

    @RequestMapping("/login")
    public String login(String user_name, String password) {
        Priv_User priv_User= priv_UserService.login(user_name, password);
        if(priv_User!=null){
            return "success";
        }
        return "fail";
    }

五. 在index.jsp中寫form來登錄

<form action="login" method="post">
        <fieldset>
        <legend>登錄</legend>
            <p>
                <label>姓名:</label> <input type="text" id="user_name" name="user_name"
                    tabindex="1">
            </p>
            <p>
                <label>密碼:</label> <input type="text" id="password" name="password"
                    tabindex="2">
            </p>
            <p id="buttons">
                <input id="reset" type="reset" tabindex="4" value="取消"> <input
                    id="submit" type="submit" tabindex="5" value="登錄">
            </p>
        </fieldset>
    </form>

六. 測試。
源碼:http://download.csdn.net/detail/nmj2015/9845288
後面準備寫一下log的配置和session過濾

發佈了37 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章