struts2.2.1+hibernate3.6.0+spring3.0.5+proxool-0.9.1实例(5)

action代码:
package com.yj.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.yj.model.User;
import com.yj.service.UserService;

//配置action转发
@Results( { @Result(name = Action.SUCCESS, location = "/success.jsp"),
@Result(name = Action.ERROR, location = "/error.jsp") })
//把userAction注入到spring中
@Component("userAction")
//spiring中bean的作用域,一个bean定义对应多个对象实例
@Scope("prototype")
public class UserAction extends ActionSupport
{
/**
* Comment for <code>serialVersionUID</code><br>
*
*/
private static final long serialVersionUID = -1897649005162084989L;

private UserService userService;

private User user;

public User getUser()
{
return user;
}

public void setUser(User user)
{
this.user = user;
}

public UserService getUserService()
{
return userService;
}

//把UserService注入到userAction中
@Resource
public void setUserService(UserService userService)
{
this.userService = userService;
}

public String addUser()
{
if (userService.exits(user.getUsername()))
{
return ERROR;
}
userService.save(user);
return SUCCESS;
}
}


dao代码:
package com.yj.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.yj.model.User;

@Component
public class UserDAO
{
private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate()
{
return hibernateTemplate;
}

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
{
this.hibernateTemplate = hibernateTemplate;
}

public void save(User user)
{
hibernateTemplate.save(user);
}

@SuppressWarnings("unchecked")
public List<User> findByUsername(String username)
{
return hibernateTemplate.find("from User u where u.username = ?",
username);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值