1. 程式人生 > >SSM三大框架環境搭建之Spring

SSM三大框架環境搭建之Spring

前言:本文是在SSM三大框架環境搭建之mybatis基礎上寫的,如果有什麼疑問請點選前面的超連結參考之前的帖子。
上文我們已經寫好了mybatis的所有配置。
接下來我們來整體結合SSM三大框架的基礎上測試我們縮寫的這個小demo。
1.寫Service介面

package com.jt.service;
public interface testService {
	String findUserByid(int id);
}

2.寫Service的介面實現類

package com.jt.serviceImpl;

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

import com.jt.dao.testDao;
import com.jt.service.testService;

@Service
public class testServiceImpl implements testService {
	@Autowired
	private testDao testdao;
	
	public String findUserByid(int id) {
		String username = testdao.findUserByid(id);
		return username;
	}
}

3.改造我們之前寫的Controller

package com.jt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jt.service.testService;

@Controller
public class TestController {
	
	@Autowired
	private testService testservice;
	
	@RequestMapping("testMVC")
	@ResponseBody
	public String testMVC(int id){
		String username = testservice.findUserByid(id);
		System.out.println(username);
		return "pageMVC";
	}
}

4.改造web.xml
這裡還有一點需要注意:我們在配置springMVC的時候伺服器啟動之後會載入web.xml,我們在web.xml中配置的前端控制器中有一段程式碼是讓前端控制器去讀取我們的配置檔案,但是現在情況是我們的配置檔案有兩個一個是SpringMVC的配置檔案,另一個是mybatis的配置檔案,所以我們需要將兩個配置檔案合為一個,另外寫一個spring-config.xml將其他兩個配置檔案用import引入過來,讓web.xml載入我們的spring-config.xml豈不快哉。
程式碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>jt-programer</display-name>

	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-configs.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup> 
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
</web-app>
spring-config.xml


   <?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
       
       <context:component-scan base-package="com.jt"/>
       <import resource="classpath:springMVC-configs.xml"/>
       <import resource="classpath:springmybatis-configs.xml"/>
       
</beans>

網頁結果:
在這裡插入圖片描述

測試成功、