81,092
社区成员
发帖
与我相关
我的任务
分享
@Service
public class Services {
//登陆服务
@Autowired
User user;
@SuppressWarnings({ "deprecation", "unchecked" })
public User loginAuth(String username,String password) {
Session session = ((Hibernate)GlobleParams.ctx.getBean("hibernate")).getSession();
//查询不用事务管理
//Transaction transaction = session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("username",username));
c.add(Restrictions.eq("password", password));
List<User> list=c.list();
session.close();
for(User user : list){
System.out.println(user.getUsername());
System.out.println(user.getPassword());
this.user = user;
}
return user;
//transaction.commit();
}
@Controller
public class Login {
/* 这里使用注解装配这个类,下面在函数里还有一个自己创建的services类,使用注解的时候,一会给出登陆两次的截图(第一次密码正确,第二次密码错误)
@Autowired
Services services;*/
@RequestMapping(value = "/home.do",method=RequestMethod.POST)
public String loginAuth(@RequestParam("UserName") String username, @RequestParam("Password") String password, Model model,HttpSession session) {
Services services = new Services();
System.out.println(username+":"+password);
User user = services.loginAuth(username, password);
System.out.println(user);
if(user!=null){
System.out.println(user.getUsername()+":"+user.getPassword());
return "home";
}else{
System.out.println("密码错误");
model.addAttribute("pw_error", "密码错误");
return "login";
}
}
zfp:zfp
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
zfp
zfp
com.skyline.goods.dao.User@3445a609
zfp:zfp
/GoodsSupply
aaa:aaa
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
null
密码错误
/GoodsSupply
zfp:zfp
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
zfp
zfp
com.skyline.goods.dao.User@319886e3
zfp:zfp
/GoodsSupply
AAAAAAAAAAAA:AAAAAAAAAAAA
Hibernate:
select
this_.user_id as user_id1_0_0_,
this_.authentication_time as authenti2_0_0_,
this_.password as password3_0_0_,
this_.register_time as register4_0_0_,
this_.update_time as update_t5_0_0_,
this_.username as username6_0_0_
from
User this_
where
this_.username=?
and this_.password=?
com.skyline.goods.dao.User@319886e3
zfp:zfp
@Autowired
Services services;
我解释一下我的输出都代表什么。
最上面/GoodsSupply不用管是个目录。
紧接着
zfp:zfp(这个是我在前端输入的用户名和密码)
紧接着是hibernate的sql语句。
其实主要的是最后面一行的(zfp:zfp)这个代表我从数据库查出的user的username和password。
所以正确的情况应该是:最上面的展示前端输入的(zfp:zfp)和最后查询得到的(zfp:zfp)相等。
大家看第二个错误的情况即:我前端输入(AAAAAAAAAAAA:AAAAAAAAAAAA)查询得到(zfp:zfp)。
认真看的话应该能看出来吧。