将类交给spring管理,你确定不会出问题?(求大神指导)

z1246879396 z1246879396 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这里是我的一个小项目。
首先给出我认为解决这个问题必须要看的代码。
Services类
@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。
@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
密码错误

这个是使用注解装配services的,第一次登陆进去之后,第二次错误的用户名密码并不会刷新User:
/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

这他妹妹的是为啥?
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
z1246879396
Web 开发 81,108 成员 341,721 内容