首页 > 代码库 > SSH整合之全注解

SSH整合之全注解

                                                                        SSH整合之全注解

使用注解配置,需要我们额外引入以下jar包

技术分享

技术分享

我们下面从实体层开始替换注解

技术分享
 1 package cn.hmy.beans;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 import javax.persistence.Table;
 7 
 8 @Entity
 9 @Table
10 public class Stock {
11 
12     @Id
13     @GeneratedValue
14     private int sid;
15     private String sname;
16     private int count;
17     
18     public int getSid() {
19         return sid;
20     }
21     public void setSid(int sid) {
22         this.sid = sid;
23     }
24     
25     public String getSname() {
26         return sname;
27     }
28     public void setSname(String sname) {
29         this.sname = sname;
30     }
31     public int getCount() {
32         return count;
33     }
34     public void setCount(int count) {
35         this.count = count;
36     }
37 }
Stock.java

Dao层替换 给实现类添加注解即可

技术分享
 1 package cn.hmy.dao.impl;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.Transaction;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.jdbc.core.support.JdbcDaoSupport;
10 import org.springframework.stereotype.Repository;
11 
12 import cn.hmy.beans.Stock;
13 import cn.hmy.dao.StockDao;
14 
15 @Repository("stockDao")
16 public class StockDaoImpl  implements StockDao{
17 
18     @Autowired
19     private SessionFactory sessionFactory;
20     
21     //添加股票
22         public int addStock(Stock stock) {
23             
24             Serializable count = sessionFactory.getCurrentSession().save(stock);
25             return ((Integer)count).intValue();
26         }
27         
28         public List<Stock> getAllStock() {
29             String sql="from Stock ";
30             List<Stock> list = sessionFactory.getCurrentSession().createQuery(sql).list();
31             return list;
32         }
33         
34     
35     public SessionFactory getSessionFactory() {
36         return sessionFactory;
37     }
38     public void setSessionFactory(SessionFactory sessionFactory) {
39         this.sessionFactory = sessionFactory;
40     }
41 
42     
43     
44 
45 
46 
47 }
StockDaoImpl

Service层也是替换实现类 使用注解

技术分享
 1 package cn.hmy.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.beans.factory.annotation.Qualifier;
 7 import org.springframework.stereotype.Service;
 8 import org.springframework.transaction.annotation.Isolation;
 9 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional;
11 
12 
13 import cn.hmy.beans.Stock;
14 import cn.hmy.beans.StockException;
15 
16 import cn.hmy.dao.StockDao;
17 import cn.hmy.service.StockService;
18 
19 @Service("service")
20 public class StockServiceImpl implements StockService{
21 
22     //植入Dao对象
23     @Autowired
24     @Qualifier("stockDao")
25     private StockDao stockDao;
26     
27     @Transactional
28     public int addStock(Stock stock) {
29         return stockDao.addStock(stock);
30     }
31 
32     public List<Stock> getAllStock() {
33         
34         return stockDao.getAllStock();
35     }
36     
37     public StockDao getStockDao() {
38         return stockDao;
39     }
40 
41     public void setStockDao(StockDao stockDao) {
42         this.stockDao = stockDao;
43     }
44 
45 
46     
47 
48 }
StockService

Action层 使用注解

技术分享
 1 package cn.hmy.action;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 import java.util.List;
 5 
 6 import javax.annotation.Resource;
 7 
 8 import org.apache.struts2.convention.annotation.Action;
 9 import org.apache.struts2.convention.annotation.Namespace;
10 import org.apache.struts2.convention.annotation.ParentPackage;
11 import org.apache.struts2.convention.annotation.Result;
12 import org.springframework.context.annotation.Scope;
13 import org.springframework.stereotype.Controller;
14 
15 import cn.hmy.beans.Stock;
16 import cn.hmy.service.StockService;
17 
18 import com.opensymphony.xwork2.ActionSupport;
19 
20 @Controller
21 @Scope("prototype")
22 @Namespace("/stock")
23 @ParentPackage("struts-default")
24 public class AddStockAction extends ActionSupport{
25     private Stock stock;
26     //植入Service对象
27     @Resource
28     private StockService stockService;
29     
30 
31     
32 
33     @Action(value="http://www.mamicode.com/addStockAction",results={@Result(name="success",location="/addSuccess.jsp")})
34     public String addStock() throws UnsupportedEncodingException{
35         
36         //stock.setSname(new String(stock.getSname().getBytes("iso-8859-1"),"utf-8"));
37         int addStock = stockService.addStock(stock);
38         System.out.println("添加股票 "+addStock);
39         
40         
41         
42         return SUCCESS;
43     }
44     
45     
46         
47     
48     
49     
50     public Stock getStock() {
51         return stock;
52     }
53 
54     public void setStock(Stock stock) {
55         this.stock = stock;
56     }
57     public StockService getStockService() {
58         return stockService;
59     }
60 
61     public void setStockService(StockService stockService) {
62         this.stockService = stockService;
63     }
64 
65 }
AddStockAction.java

这里我们需要注意applicationContext.xml配置文件中的配置信息

引入包扫描器

技术分享

将小配置文件的引入换为以下形式

技术分享

如果用上面的扫描器,我们必须保证sessionFactory的为Hibernate5或者为以下形式

技术分享

Spring用注解配置事务

技术分享

按以上配置配即可

SSH整合之全注解