SSH整合之全注解
SSH整合之全注解
使用注解配置,需要我们额外引入以下jar包
我们下面从实体层开始替换注解
package cn.yxj.beans; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table@Table(name="stock") public class Stock { @Id @GeneratedValue @Column(name="sid" ) private int sid; private String sname; private int count; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
Dao层替换 给实现类添加注解即可
package cn.yxj.dao.impl; import java.io.Serializable; import java.util.List; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.stereotype.Repository; import cn.yxj.beans.Stock; import cn.yxj.dao.StockDao; @Repository("stockDao") //它用于将数据访问层(DAO 层 ) 的类标识为 Spring Bean public class StockDaoImpl implements StockDao{ @Autowired private SessionFactory sessionFactory; //添加股票 public int addStock(Stock stock) { Serializable count = sessionFactory.getCurrentSession().save(stock); return ((Integer)count).intValue(); } public List<Stock> getAllStock() { String sql="from Stock "; List<Stock> list = sessionFactory.getCurrentSession().createQuery(sql).list(); return list; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
Service层也是替换实现类 使用注解
package cn.yxj.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import cn.yxj.beans.Stock; import cn.yxj.beans.StockException; import cn.yxj.dao.StockDao; import cn.yxj.service.StockService; @Service("service") public class StockServiceImpl implements StockService{ //植入Dao对象 @Autowired @Qualifier("stockDao") private StockDao stockDao; @Transactional public int addStock(Stock stock) { return stockDao.addStock(stock); } public List<Stock> getAllStock() { return stockDao.getAllStock(); } public StockDao getStockDao() { return stockDao; } public void setStockDao(StockDao stockDao) { this.stockDao = stockDao; } }
Action层 使用注解
package cn.yxj.action; import java.io.UnsupportedEncodingException; import java.util.List; import javax.annotation.Resource; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import cn.yxj.beans.Stock; import cn.yxj.service.StockService; import com.opensymphony.xwork2.ActionSupport; @Controller @Scope("prototype") @Namespace("/stock") @ParentPackage("struts-default") public class AddStockAction extends ActionSupport{ private Stock stock; //植入Service对象 @Resource private StockService stockService; @Action(value="addStockAction",results={@Result(name="success",location="/addSuccess.jsp")}) public String addStock() throws UnsupportedEncodingException{ //stock.setSname(new String(stock.getSname().getBytes("iso-8859-1"),"utf-8")); int addStock = stockService.addStock(stock); System.out.println("添加股票 "+addStock); return SUCCESS; } public Stock getStock() { return stock; } public void setStock(Stock stock) { this.stock = stock; } public StockService getStockService() { return stockService; } public void setStockService(StockService stockService) { this.stockService = stockService; } }
这里我们需要注意applicationContext.xml配置文件中的配置信息
引入包扫描器
<!-- 包扫描器 -->
<context:component-scan base-package="cn.yxj"></context:component-scan>
将小配置文件的引入换为以下形式
<property name="packagesToScan" value="cn.yxj.beans">
如果用上面的扫描器,我们必须保证sessionFactory的为Hibernate5或者为以下形式
Spring用注解配置事务
按以上配置配即可