1. 程式人生 > >最簡單的ssh框架搭建:spring4+struts2+hibernate5。使用部分註解(常用的幾個註解)

最簡單的ssh框架搭建:spring4+struts2+hibernate5。使用部分註解(常用的幾個註解)

前一篇寫了沒有使用註解的方式:https://blog.csdn.net/Handsome2013/article/details/86301083

這一篇是有部分需要使用註解:這個是在上一篇的基礎上寫的。

改變1:在dao層的SSHDaoImpl.java上添加了註釋@Repository

package cqupt.ssh.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import org.springframework.stereotype.Repository;

import cqupt.ssh.dao.SSHDao;
import cqupt.ssh.entity.Students;
import cqupt.ssh.utils.HibernateUtils;
@Repository
public class SSHDaoImpl implements SSHDao {

	@Override
	public List<Students> getStudents() {
		// 1獲得session
		Session session = HibernateUtils.openSession();
		// 2控制事務
		Transaction tx = session.beginTransaction();
		// 3.執行操作
		// 書寫hql
		String hql = "from Students";
		Query query = session.createQuery(hql);
		List<Students> list = query.list();
//		System.out.println(list);
		// 4.提交事務,關閉資源
		tx.commit();
		session.close();
		return list;
	}

}

改變二:在service層:在SSHServiceImpl中多註釋:@Service       下邊在引入dao的時候使用了註解@Autowired,就不需要寫set方法 了

package cqupt.ssh.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cqupt.ssh.dao.SSHDao;
import cqupt.ssh.entity.Students;
import cqupt.ssh.service.SSHService;
@Service
public class SSHServiceImpl implements SSHService {
	@Autowired
	private SSHDao sSHDao ;
	
	@Override
	public List<Students> getStudents() {
		// TODO Auto-generated method stub
		return sSHDao.getStudents();
	}

}

改變3:在action層,在SSHAction.java上使用了註解@Controller        引入service的時候,就使用註釋@Autowired同時省略了set方法。

package cqupt.ssh.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cqupt.ssh.entity.Students;
import cqupt.ssh.service.SSHService;

@Controller
public class SSHAction {
	@Autowired
	private SSHService sSHService ;
	

	public String getStudents() {
		List<Students> list = sSHService.getStudents();
		/*通過ActionContext.getContext().put("message",message);
		 * 直接放入將資料request域中,不獲取request本身,獲取其 中的Map;*/
		ActionContext.getContext().put("message", list.get(0).getS_name());
		return "success";//根據這個返回的”success"到struts配置中可以找到對應的頁面。

	}
}

改變4,在於配置applicationContext.xml上的變化:使用掃描註解方式。就省去了之前直接寫bean的形式。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context  
                http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- 由於在三層中使用了註解,所以需要掃描,去找到那些註解 -->
	<context:component-scan base-package="cqupt.ssh"></context:component-scan>
	

</beans>

其他就是完全一樣,然後執行,訪問,結果一樣。

注意事項:開始的時候也是報錯,是因為配置上缺少東西,報錯如下: 元素 "context:component-scan" 的字首 "context" 未繫結。

是自動裝配與掃描有問題,了context的名稱空間的問題

解決方式:新增點名稱空間部分東西:

在xmlns後新增:

context="http://www.springframework.org/schema/context"

以及: 在xsi:中新增:

http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-2.5.xsd