将类交给spring管理,你确定不会出问题?(求大神指导)
这里是我的一个小项目。
首先给出我认为解决这个问题必须要看的代码。
Services类
然后给出controller。
下面看输出结果(这个是不用注解的正确实现功能的):
这个是使用注解装配services的,第一次登陆进去之后,第二次错误的用户名密码并不会刷新User:
这他妹妹的是为啥?
首先给出我认为解决这个问题必须要看的代码。
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这他妹妹的是为啥?
...全文
相关推荐
Spring框架的核心作用是把对象的创建交给spring进行管理
.ioc底层原理使用技术 (1)xml配置文件 (2)dom4j解析xml (3)工厂设计模式 (4)反射
SSH整合方式二:将hibernate的配置交给Spring管理
hibernate + struts2+spring三大框架的整合 SSH整合方式二将hibernate的配置交给Spring管理
spring源码全部uml类图
spring2.6框架源码全部uml类图,请用Enterprise Architect打开查看,是学习spring框架不可多得的资料
Java注解@Transactional事务类内调用不生效问题及解决办法
主要介绍了Java注解@Transactional事务类内调用不生效问题及解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Spring如何解决循环依赖的问题
主要介绍了Spring是如何解决循环依赖的问题,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下


Web 开发
