简单搭建注解ssh项目(二)

本文通过实战演示如何使用Spring框架中的注解进行开发,包括@Component、@Repository、@Service、@Transactional等注解的应用,并详细介绍了实体类、DAO层、Service层及Action层的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上面的一篇文章大家应该看懂了吧?

可能大家在启动这个项目的时候会出现一个错误:


不要紧,这个因为我们少了一个hibernate的方言,因为我们把它给注释了


改成下图即可


那么接下来我们继续使用注解:

1.使用注解我们用到以下几个常用的注解:

1.注解普通类:@Component

2.注解Dao:@Repository

3.注解Service:@Service

4.注解事务:@Transactional

5.注解Action:@Action

6.注入属性:@Resource

1.生成的实体类(如果不会生成自己手写也可以):

package com.yy.po;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * BankCust entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "BANK_CUST", schema = "SCOTT")
public class BankCust implements java.io.Serializable {

	// Fields

	private String cardId;
	private Double balance;

	// Constructors

	/** default constructor */
	public BankCust() {
	}

	/** full constructor */
	public BankCust(String cardId, Double balance) {
		this.cardId = cardId;
		this.balance = balance;
	}

	// Property accessors
	@Id
	@Column(name = "CARD_ID", unique = true, nullable = false)
	public String getCardId() {
		return this.cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	@Column(name = "BALANCE", nullable = false, precision = 6)
	public Double getBalance() {
		return this.balance;
	}

	public void setBalance(Double balance) {
		this.balance = balance;
	}

}
2.创建Dao层

package com.yy.dao.impl;

import org.springframework.stereotype.Repository;

import com.yy.dao.BankDao;
import com.yy.po.BankCust;
import com.yy.util.HibernateUtil;
@Repository("bankDao")
public class BankDaoImpl extends HibernateUtil implements BankDao {

	@Override
	public BankCust queryById(String id) {
		return (BankCust)this.getSessionFactory().getCurrentSession().get(BankCust.class, id);
	}

	@Override
	public void updateById(BankCust bc) {
		this.getSessionFactory().getCurrentSession().update(bc);
	}

}
3.创建service:

package com.yy.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yy.dao.BankDao;
import com.yy.po.BankCust;
import com.yy.service.BankService;
@Service("bankService")
@Transactional
public class BankServiceImpl implements BankService {
	@Resource
	private BankDao bankDao;
	@Override
	public BankCust queryById(String id) {
		return bankDao.queryById(id);
	}
	@Override
	public void updateById(BankCust bc) {
		bankDao.updateById(bc);
	}

}
4.创建action

package com.yy.action;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.yy.action.config.BaseAction;
import com.yy.po.BankCust;
import com.yy.service.BankService;
import com.yy.service.CustomerService;

@Action(value = "bank", results = {
		@Result(location = "/success.jsp", name = "success", type = "redirect"),
		@Result(name = "error", location = "/fail.jsp") })
@Namespace("/bank")
public class BankAction extends BaseAction {
	@Resource
	private BankService bankService;
	@Resource
	private CustomerService customerService;
	private BankCust bank;

	public void exist() throws IOException {
		bank = bankService.queryById(bank.getCardId());
		if (bank == null) {
			this.getWrite().write("false");
		} else {
			this.getWrite().write("true");
		}
	}

	public BankCust getBank() {
		return bank;
	}

	public void setBank(BankCust bank) {
		this.bank = bank;
	}
}
5.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
	<script>
		var flag=false;
		function checkCard(value){
			$.post('bank/bank!exist.action',{'bank.cardId':value},function(data){
				if(data=='true'){
					$("#showMsg").html("可以使用!!");
					flag=true;
				}else{
					$("#showMsg").html("卡号不存在!!");
					flag=false;
				}
			});
		}
	</script>
  </head>
  
  <body>
   
   <input type="text" onblur="checkCard(this.value)" name="bank.cardId"/><span id="showMsg"></span>
  </body>
</html>

6.访问

失去焦点如果现实如下内容后台没有出现错误则表示注解项目已经成功!



大功告成!事务大家可以通过一个DML操作测试即可!










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值