前言:本文是在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>
测试url:http://localhost/jt-programer/testMVC.do?id=18
后台测试结果:
网页结果:
测试成功、