struts2 action类
public class T_dealer_accountAction extends ActionSupport{
private static Logger log = Logger.getLogger(T_dealer_accountAction.class.getName());
private T_dealer_accountService service;
private List<T_dealer_account> t_dealer_accounts;
private T_dealer_account t_dealer_account;
private int id;
private int[] ids;
PaginationSupport paginationSupport;
//no inject
public T_dealer_accountAction(){
this.service = (T_dealer_accountService)EjbProxy.getInstance().lookup("T_dealer_accountBean#com.xuka.ejb3.T_dealer_account");
}
//inject
public T_dealer_accountAction(T_dealer_accountService service){
this.service = service;
}
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
//HttpServletResponse response = ServletActionContext.getResponse();
//HttpSession session = request.getSession();
//ActionContext.getContext().getSession().put("msg", "Hello World from Session!");
String message ="查找所有记录操作成功!";
int pageSize = 10;
int startIndex = 0;
try{
if(request.getParameter("pageSize")!=null){
pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
}
if(request.getParameter("startIndex")!=null){
startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
}
this.paginationSupport = service.listAll(pageSize, startIndex );
message ="查找所有记录操作成功!";
System.out.println("-------------T_dealer_accountAction list method---------"+message);
}catch(Exception e){
message ="查找所有记录操作失败!";
log.debug("-------------T_dealer_accountAction list method ---------"+message+" "+e);
}
return SUCCESS;
}
public String save() {
String message ="添加操作失败!";
try{
service.add(t_dealer_account);
this.t_dealer_account = new T_dealer_account();
message ="添加操作成功!";
System.out.println("-------------T_dealer_accountAction add method ---------"+message);
}catch(Exception e){
message ="添加操作失败!";
log.error("-------------T_dealer_accountAction add method ---------"+message+" "+e);
return ERROR;
}
return SUCCESS;
}
public String update(){
String message ="修改操作失败!";
int rows = 0;
try{
rows = service.edit(t_dealer_account);
message ="修改操作成功!";
System.out.println("-------------T_dealer_accountAction edit method ---------"+message);
}catch(Exception e){
message ="修改操作失败!";
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+" "+e);
return ERROR;
}
if(rows ==0){
return ERROR;
}else{
return SUCCESS;
}
}
public String trade(){
HttpServletRequest request = ServletActionContext.getRequest();
String referer = request.getHeader("referer");
if (referer == null||!referer.startsWith("http://"+request.getServerName())){
System.out.println("---------非法请求 trade() 方法---------");
return ERROR;
}
//根据当前登陆的用户session中的信息,进行设置setDealerno,setFromuser
T_user_detail sessionUser = (T_user_detail)request.getSession().getAttribute("user");
if(sessionUser==null){
System.out.println("---------未登录,非法请求 trade() ,不能请求 trade() 方法---------");
return ERROR;
}
t_dealer_account.setDealerno(sessionUser.getDealerno());
t_dealer_account.setFromuser(String.valueOf(sessionUser.getDealerid()));
String message ="操作失败!";
int rows = 0;
try{
t_dealer_account.setAcctip(request.getRemoteAddr());
t_dealer_account.setAmount((int)(t_dealer_account.getAmount()*100)); //元
rows = service.edit(t_dealer_account);
message ="操作成功!";
}catch(Exception e){
message ="操作失败!";
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+" "+e);
return ERROR;
}
if(rows == 1 ){
//修改当前用户的货币数量
sessionUser.setAmount(sessionUser.getAmount()-(int)(t_dealer_account.getAmount())); //点
request.getSession().setAttribute("user", sessionUser);
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+"--------rows="+rows);
return SUCCESS;
}else{
System.out.println("-------------T_dealer_accountAction edit method ---------"+message+"--------rows="+rows);
return ERROR;
}
}
public String bill(){
System.out.println("-------------T_dealer_accountAction bill method ---------");
HttpServletRequest request = ServletActionContext.getRequest();
String referer = request.getHeader("referer");
if (referer == null || !referer.startsWith("http://"+request.getServerName())){
System.out.println("---------非法请求 bill() 方法---------");
return ERROR;
}
//根据当前登陆的用户session中的信息,进行设置setDealerno,setFromuser
T_user_detail sessionUser = (T_user_detail)request.getSession().getAttribute("user");
if(sessionUser==null){
System.out.println("---------未登录,非法请求 bill() ,不能请求 bill() 方法---------");
return ERROR;
}
t_dealer_account.setDealerno(sessionUser.getDealerno());
t_dealer_account.setFromuser(String.valueOf(sessionUser.getDealerid()));
String message ="操作失败!";
int rows = 0;
try{
t_dealer_account.setAcctip(request.getRemoteAddr());
t_dealer_account.setAmount((int)(t_dealer_account.getAmount()*100)); //把人民币转化成点,1元=100点
t_dealer_account.setAr((int)t_dealer_account.getAmount());
t_dealer_account.setDaylimit(365);
t_dealer_account.setTcode("FV0001");
t_dealer_account.setBillno(System.currentTimeMillis()); //纪录关联
t_dealer_account.setCardkind(18);
t_dealer_account.setMno(0);
t_dealer_account.setUserid(t_dealer_account.getTouser());
System.out.println("-------------T_dealer_accountAction bill t_dealer_account.getCztype="+t_dealer_account.getCztype());
System.out.println("--------- InvokeBill selectMethod temp.getGzno()="+t_dealer_account.getGzno());
System.out.println("--------- InvokeBill selectMethod temp.getSysno()="+t_dealer_account.getSysno());
if(t_dealer_account.getCztype()!=null&&t_dealer_account.getCztype().equals("1")){
System.out.println("-------------T_dealer_accountAction bill getCztype=1 method ---------");
t_dealer_account.setSysmno(0);
t_dealer_account.setSysmnoamount(0);
}else if(t_dealer_account.getCztype()!=null&&t_dealer_account.getCztype().equals("2")){
System.out.println("-------------T_dealer_accountAction bill getCztype=2 method ---------");
//获取汇率
T_puser t_puser = new T_puser();
t_puser.setSysno(t_dealer_account.getSysno());
T_puserService t_puserService = new T_puserServiceImpl();
t_puser = (T_puser) t_puserService.findRationBySysno(t_puser);
t_dealer_account.setSysmnoamount(t_dealer_account.getAr()/((int) t_puser.getRatio()));
t_dealer_account.setSysmno(t_puser.getOutmno());
t_dealer_account.setTcode2("FV0002");
System.out.println("--------- t_dealer_account.getAr()="+t_dealer_account.getAr());
System.out.println("--------- (int) t_puser.getRatio()="+(int) t_puser.getRatio());
System.out.println("--------- t_dealer_account.getSysmnoamount()="+t_dealer_account.getSysmnoamount());
}else{
System.out.println("-------------T_dealer_accountAction bill getCztype= null method ---------");
return ERROR;
}
rows = service.bill(t_dealer_account);
message ="操作成功!";
}catch(Exception e){
message ="操作失败!";
System.out.println("-------------T_dealer_accountAction bill method ---------"+message+" "+e);
e.printStackTrace();
return ERROR;
}
if(rows == 1 ){
//修改当前用户的货币数量
sessionUser.setAmount(sessionUser.getAmount()-(int)(t_dealer_account.getAmount())); //点
//request.getSession().setAttribute("user", sessionUser);
System.out.println("-------------T_dealer_accountAction bill method ---------"+message+"--------rows="+rows);
return SUCCESS;
}else{
message ="操作失败!";
System.out.println("-------------T_dealer_accountAction bill method ---------"+message+"--------rows="+rows);
return ERROR;
}
}
public String view(){
setT_dealer_account(find());
return SUCCESS;
}
public String edit(){
setT_dealer_account(find());
return SUCCESS;
}
public List<T_dealer_account> getT_dealer_accounts() {
return t_dealer_accounts;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int[] getIds() {
return ids;
}
public void setIds(int[] ids) {
this.ids = ids;
}
//public void prepare() throws Exception {
// if (id != null)
// T_dealer_account = service.find(id);
//}
public PaginationSupport getPaginationSupport() {
return paginationSupport;
}
public void setPaginationSupport(PaginationSupport paginationSupport) {
this.paginationSupport = paginationSupport;
}
public T_dealer_account getT_dealer_account() {
return t_dealer_account;
}
public void setT_dealer_account(T_dealer_account t_dealer_account) {
this.t_dealer_account = t_dealer_account;
}
}
EJB3 接口、类
import com.common.utils.PaginationSupport;
public interface T_dealer_accountService {
public Object add(Object obj);
public int edit(Object obj);
public int bill(Object obj);
public int deleteByPrimaryKey(Object obj);
public Object findByPrimaryKey(Object obj);
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Remote(T_dealer_accountService.class)
@Stateless(mappedName = "T_dealer_accountServiceBean")
public class T_dealer_accountServiceBean implements
T_dealer_accountService {
private T_dealer_accountServiceImpl service;
public T_dealer_accountServiceBean(){
System.out.println("---------------------start Construct Method --------------------------");
this.service = new T_dealer_accountServiceImpl();
System.out.println("---------------------end Construct Method -----------------------");
}
public Object add(Object obj){
return service.add(obj);
}
public int edit(Object obj){
return service.edit(obj);
}
public int bill(Object obj){
return service.bill(obj);
}
public int deleteByPrimaryKey(Object obj){
return service.deleteByPrimaryKey(obj);
}
public Object findByPrimaryKey(Object obj){
return service.findByPrimaryKey(obj);
}
public PaginationSupport listAll(int pageSize, int startIndex){
return service.listAll(pageSize,startIndex);
}
public T_dealer_accountService getService() {
return service;
}
public void setService(T_dealer_accountServiceImpl service) {
this.service = service;
}
}
public PaginationSupport listAll(int pageSize, int startIndex);
}
持久层 ibaits2.3
public class T_dealer_accountDAOImpl implements T_dealer_accountDAO{
public Object add(Object obj){
Object retObj = null;;
try{
retObj = DaoConfig.getInstance().insert("T_dealer_account.insertT_dealer_account", (T_dealer_account)obj);
}catch(Exception e){
System.out.println("------error="+e);
}
return retObj;
}
//xxxxxxxx操作
public int edit(Object obj) {
//更新结果标量
int rows =0, rows1 = 0, rows2 = 0;
SqlMapClient conn = null;
//账户状态
T_dealer_account temp = (T_dealer_account)obj;
String fromUserLock = null, toUserLock = null;
boolean fromUserLockFlag = false, toUserLockFlag = false;
try {
conn = DaoConfig.getInstance();
fromUserLock = Cache.getLock(temp.getFromuser());
toUserLock = Cache.getLock(temp.getTouser());
} catch (Exception e1) {
System.out.println("------xxxxxxx获取连接失败="+e1);
}
try{
conn.startTransaction();
//如果两个对象有一个或者全部为锁定状态,则要等待,不能对这2个账户进行更新操作
if(fromUserLock.equals("LOCK")||toUserLock.equals("LOCK")){
System.out.println("------账户被锁定,xxxxx失败,清等一会再试-----");
return 0;
}
//同时锁定2账户,如果失败,则返回失败,如果成功,执行下面转卡操作
fromUserLockFlag = Cache.setLock(temp.getFromuser());
toUserLockFlag = Cache.setLock(temp.getTouser());
//如果锁定2账户成功,则执行更新账户操作,否则不执行。
if( fromUserLockFlag && toUserLockFlag){
//更新账户tosuer
rows1 = conn.update("T_dealer_account.updateT_dealer_accountByPrimaryKey", (T_dealer_account)obj);
//更新账户fromuser
rows2 = conn.update("T_dealer_account.updateT_dealer_accountByPrimaryKey2", (T_dealer_account)obj);
System.out.println("------事务rows1="+rows1+"-----事务rows2="+rows2);
//如果2个账户都更新成功,则提交事务,否则,回滚事务
if(rows1==1&&rows2==1){
conn.commitTransaction();
rows =1;
System.out.println("------事务一致----- ");
}
}else{
System.out.println("------锁定账户失败,不能xxxxxx,请等一会再试-----");
return 0;
}
}catch(Exception e){
rows = 0;
System.out.println("------转卡失败="+e);
}finally{
try {
//关闭事务
conn.endTransaction();
} catch (Exception e) {
e.printStackTrace();
System.out.println("------关闭事务失败-----"+e);
}
try {
//插入日志,可以在拦截器做,但考虑程序的清晰,决定放在此处作
T_dealer_log t_dealer_log = new T_dealer_log();
t_dealer_log.setDealerno(temp.getDealerno());
t_dealer_log.setAcctip(temp.getAcctip());
System.out.println("------temp.getAcctip()="+temp.getAcctip());
t_dealer_log.setFromuser(temp.getFromuser());
t_dealer_log.setTouser(temp.getTouser());
t_dealer_log.setAmount((int)temp.getAmount());
t_dealer_log.setType(1);
t_dealer_log.setStatus(rows);
t_dealer_log.setMemo("xxxxxx");
conn.insert("T_dealer_account.insertT_dealer_log",t_dealer_log);
} catch (Exception e) {
System.out.println("------插入日志失败-----"+e);
}
//释放锁定账户,否则造成死锁,如果是前面代码锁定,则释放,如果是别人锁定的,需要别人去释放
//不能释放别人的锁定,否则会造成线程不安全
if(fromUserLockFlag){
Cache.setUnLock(temp.getFromuser());
}
if(toUserLockFlag){
Cache.setUnLock(temp.getTouser());
}
}
return rows;
}
public int deleteByPrimaryKey(Object pk){
int rows = 0;
try{
T_dealer_account key = new T_dealer_account();
key.setId((Integer)pk);
rows = DaoConfig.getInstance().delete("T_dealer_account.deleteT_dealer_accountByPrimaryKey", key);
}catch(Exception e){
System.out.println("------error="+e);
}
return rows;
}
public Object findByPrimaryKey(Object pk){
Object retobj = null;
try{
T_dealer_account key = new T_dealer_account();
key.setId((Integer)pk);
retobj = (T_dealer_account) DaoConfig.getInstance().queryForObject("T_dealer_account.selectT_dealer_accountByPrimaryKey", key);
}catch(Exception e){
System.out.println("------error="+e);
}finally{
return retobj;
}
}
public PaginationSupport listAll(int pageSize, int startIndex){
PaginationSupport ps = null;
try{
T_dealer_account t_dealer_account = new T_dealer_account();
t_dealer_account.setStartNo( startIndex );
t_dealer_account.setEndNo( startIndex + pageSize );
int totalCount = (Integer)DaoConfig.getInstance().queryForObject("T_dealer_account.selectT_dealer_accountCountAll", t_dealer_account);
List items = DaoConfig.getInstance().queryForList("T_dealer_account.selectT_dealer_accountAll", t_dealer_account );
//List items = DaoConfig.getInstance().queryForList("T_dealer_account.selectT_dealer_accountAll",null, startIndex, pageSize);
ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
}catch(Exception e){
System.out.println("------error="+e);
}finally{
return ps;
}
}
}