SSH整合注意事项

Sping4+Hibernate3+Struts2注解配置整合注意事项:

①在Spring配置文件aplicationContext.xml文件中配置自动扫描Serivce服务类和Dao类的注解包

<context:component-scan base-package="com.aclik.action,com.aclik.dao.impl,com.aclik.service.impl" />

 ②在struts2的struts.xml配置文件中配置action访问Action类,不然调用Service的Bean类时会出现指针异常

    Action的注解配置:

@Component("bookAction") 
@Scope("prototype")
public class BookAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	@Resource
	BookService bookService;
	//查询书本的信息
	public String getAllBookInfo(){
		//List<Book> books = bookService.findBookPriceByIsbn();
		Book book = bookService.findBookById(1);
		System.out.println(book.getBookName());
		return "success";
	}
}

   struts.xml配置:

   

<struts>
	<package name="user" extends="struts-default">
	    <!-- 此处的class属性值应该和Action类的@Component("bookAction")注解值一致  -->
	    <action name="test" class="bookAction" method="getAllBookInfo">
	       <result name="success">/test.jsp</result>
	    </action>
	</package>
</struts> 

  

③在Service服务类中调用Dao类操作数据表时候,应使用为实体类名称,而不是数据表名称

   

@Override
public Book findBookById(Integer id) {
	String hql="from Book  where id = 1";
	Book book = baseDAO.get(hql, new Object[]{});
	return book;
}

猜你喜欢

转载自aclik.iteye.com/blog/2360019