SSH整合之全注解

时间:2024-01-11 12:52:38

                                  SSH整合之全注解

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

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOgAAABGCAIAAABJ3U+xAAAFgUlEQVR4nO2bT2/cRByG/RH6FfotYI8c8xEqceFgwZkjPbEVILWizpZWEEAoqCKR0pRNoC4SFCQOFVIPPdByqKGAVJGoIlG0IbvrOJI5eNc7/z2zm9j+Zd5HP1XbeJzxn2dnZjd+g3zKKx+9QKFIVJ7nAcRFkSuIiyJZanFzAFqJs7g/xR8oq75DBmA+cdPjE6GebL91Tt1Nok4QdKKk6eMAAs7ifv/t1VGajdOTcZpNX5w8e3D5aZvcjcNgikk6i2YQt6U4i3t/6/rROFu58+jGVw+vffHzeys/DsfZX79cf/bg8pPtN+9vfVjfsWtIok4Yz17rvLNsBtqJs7jbd3uDo+xwmA2G2eEwOzzKDoez2v66V9+x25BEnSAoBV20GWgNzuJubtw8OMru/PB0/btfv/zmMTv0HvyXbW7c1PeVRJ1yamYlmc3Y5U/jMAjCeLZDJ0q4/a0dW1jcOGTHYvUpFG3iYhsG7lpwFndt/eO9wfHe4Hh/cLwn1dr6J7qekiic3lJ2ZubEiKPJq8Llyc9LWyaqxKGtuqcw3rLHpzmF6TsPI3aN2Iq7dGWTLeWI+/IgXb39mVW3xYiaa53hxzlRVWGrhDBSL9RM21d5Ci7vJHBa2Ip74bV32NrZT4X6Zz/d2U8/XV01dMZ8ii/v9NQeXg7Giul/mQZJ1JFGY7V+xeZKpwzNVG8hcbVQ9UYCp4/DUuH1tTfYkkfcF/+mtz6/remouONTNwQxS/3YyddK3GqEX+XajOlaewoQt34cxA2uLLH198uxXNHKurofxWccSRNmxm2juPpTgLj1Yyvuw7dflasca//cHT3fHT3fHV+9taHuh10GTsbXyRq3/LwjDm5zihuHzJ78GoA9CMtmgriqU4C4TWAr7r1LF3+7tsTWvUsXf98Z/bEzKv4tXrx/466uJ+6jkPjJRlykLjTisutebhTlB3q7Z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qKPJm7KFo8UcC3K2oI3cdmdpxSmOuarQpPKbgibGRioTDd2F7M3KK9vi6ldPYNuOAxouY0SmDoA2aYbxyArg28pjQPR9yXnjlLYj9oxGHv49G+0ejj1dXdUkv8aN2HUiIvdhdNUrkXEnFMhGr1lHTW3o9Xvp4SvNBBSk4ngsuPnEVqtOm81h2IwU/SBMOLPVG5JTYoyWnsW3qgCrBreGqBkJBHo9oTk5wRMw0G5zcyLnXe/7q2msQ4sr59J/RrU9v63wUZaSSXVOSUzXKOMDJW44ZkDPuG6+N+kX+/MmZnHBcsp4jOdUuSnkw1qqqKlupNvhQwCIo4bwRW86tnLMjJ7uyBPHXs6EIZ2U92U1oX5RITj5syeQURpFWFApIKkPVdf7prs6tMC3aDUuzl1ZlsKBturI2FTnxXeh5Da+09qDNZGZypgko7iUjp6E2o5VTvdjOkJwP3nxZRLRm/nEweHIweHIwvHprQz63jlNBr5KiCSSd9XG+C4/+MDnh4Y1slHTvHvj6x7Xh2TK8Dn4m5ZZrM2aV8CaTV9hxSmMNyeQ0PIBJZ6QkZUF7fNTEE1URoFYV1aIJB0KmtmkCCsnvVYAG7BmumVF6iFGQTAR6aVzBzZ6c5v81pmpuym81Jkft8hcDUeQmFYXqpD7uzo0i391pvhdBp//oWQuvIw2JueVVmCQ26NWI8CpFqU3vsexGCvGKjv4ZY7oCRxcguAGruHmUtbqAYj2oJdQGmhmnBzZJNREQ07LTPp+ydnfpgoh6c7ve3Ko3t95e3qo3t+rL2/XlLWXGExYKs/nMwPiVxkJBvZtNQD7kJLxomAk5jb8EWCigt2WpQOQkZEEu5Gw3LJC18tp+4cF9Z5IGRE5CFuS1cqKXfv+vgjbaXmauBYicBEJBQeQkEAqKaclJQkIyFyFykpAUVIicJCQFFSInCUlBhScngUAoFIicBEJBQeQkEAqK/wB/4l08pehraQAAAABJRU5ErkJggg==" alt="" />

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

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或者为以下形式

SSH整合之全注解

Spring用注解配置事务

SSH整合之全注解

按以上配置配即可