您的位置:首页 > 数据库 > Oracle

springmvc+mybatis+oracle简单登录

2017-05-17 18:10 344 查看
之前整合了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过滤
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: