之前学了ssm后,分别尝试了mybatis,mybatis-spring和spring-spring MVC的小项目。虽然初次尝试会遇到小问题,但都很好解决。但是ssm三个一起整合,问题就很多了,也莫名其妙,甚至该web应用调试的时候,竟然调到了其他项目中!!
先说个Junit单元测试时遇到的问题,在用Junit测试业务层(Service包下的类)时,如果spring 和spring MVC的配置文件都在/WEB-INF/下时,junit的测试类是获取不到,因为这些配置文件不在classpath路径下。而使用spring MVC时这些配置文件会被访问到,因为spring的配置文件会在web.xml中指定,而spring MVC则默认配置文件在/WEB-INF/下,因此能找到。
好了,接来下看看整合过程。
1、项目结构
目录结构和之前的项目相似,我只是简单的拷贝而已,感兴趣的请看我之前的博客。但是要注意的一点是,mybatis的配置文件mybatis-config.xml放置的位置,需要放到resource下,否则会访问不到,比如我之前就放在了/WEF-INF/下了,因此没有访问到。原因就是我们在spring中指定mybatis配置的文件必须要在classpath路径下,如下图所示。
2、pom.xml
项目中用到json的视图,因此加入了json相关包的依赖。同时设置了编码,因为maven并不是默认utf-8编码的。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.luo.my</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>ssm</finalName>
</build>
</project>
3、web.xml配置文件
在<context-param>中指定了spring配置文件的位置,springMVC配置文件默认servlet-name加上-servlet,即dispatcher-servlet.xml。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>first ssm project</display-name>
<!-- 配置Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 配置ContextLoaderListener用以初始化Spring IoC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Servlet拦截配置 -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4、spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 启用扫描机制,并指定扫描对应的包。该元素默认启动<context:annotation-config/> -->
<context:component-scan base-package="com.luo.my"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 集成MyBatis -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:/mybatis-config.xml"/>
</bean>
<!-- 事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 使用注解定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 采用自动扫描方式创建mapper bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.luo.my"/>
<property name="SqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
</bean>
</beans>
5、spring mvc配置文件dispatcher-servlet.xml
里面有一点让我很纳闷,明明在applicationContext.xml中配置了包扫描,这里还需要,不配置就找不到类。总之,这里必须有context:component-scan。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用注解驱动 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.luo.my"/>
<!-- 定义视图解析器 -->
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
6、控制层MyController.java类
重点需要关注的是getStudent这个方法,接收一个需要sno,从数据库中查出数据后,设置Json的视图,也就是返回json数据,而不是jsp文件。
package com.luo.my.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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import com.luo.my.pojo.Student;
import com.luo.my.service.StudentService;
@Controller
public class MyController {
@Autowired
StudentService service=null;
@RequestMapping("/student")
public ModelAndView getStudent(String sno) {
Student student=null;
if(sno!=null) {
student=service.selectStudent(sno);
}
ModelAndView mView=new ModelAndView();
mView.addObject("student", student);
mView.setView(new MappingJackson2JsonView());
return mView;
}
@RequestMapping("/index")
public ModelAndView commonParams(@RequestParam(value="roleName",required=false) String roleName,@RequestParam(value="note",required=false) String note) {
ModelAndView mView=new ModelAndView();
mView.setViewName("index");
System.out.println("roleName:"+roleName);
System.out.println("note:"+note);
return mView;
}
}
7、其他层文件
其他的文件是之前的项目拷贝过来,这里就不在继续贴出来了,也没有改动。想查看的可以看我之前的博客。数据库的设计也在第一篇mybatis项目中有给出。
8、首页index.html
首页是html文件,当输入一个学号进行查询时,会通过ajax来获取学生信息,然后输出到table表格中。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.3.1.js"></script>
<script>
$(document).ready(function(){
$("#queryBtn").click(function(){
var sno=$("#sno").val();
$.post("/ssm/student.do",
{sno:sno},
function(data,status){
var text="<th>"+data.student.sno+"</th>"+
"<th>"+data.student.sname+"</th>"+
"<th>"+data.student.ssex+"</th>"+
"<th>"+data.student.sage+"</th>"+
"<th>"+data.student.sdept+"</th>";
var text1=" <tr>"+
"<th>no</th>"+
"<th>name</th>"+
"<th>sex</th>"+
"<th>age</th>"+
"<th>dept</th>"+
"</tr>";
$("#result").html(text1+text);
});
});
});
</script>
<style>
header{
background-color: #aca2a2;
color: white;
height: 100px;
font-size: 3em;
text-align: center;
position:relative;
}
header span{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#query{
width: 50%;
margin: 20px auto;
text-align: center;
table{
border-collapse:collapse;
margin:20px auto;
min-width: 50%;
}
th,td{
border:1px solid black;
text-align:left;
}
</style>
</head>
<body>
<header><span>查询学生</span></header>
<div id="query">
<input id="sno" type="text" name="sno"/>
<button id="queryBtn">查询</button>
</div>
<table id="result">
<tr>
<th>no</th>
<th>name</th>
<th>sex</th>
<th>age</th>
<th>dept</th>
</tr>
</table>
</body>
</html>
9、运行结果
这是首页界面,为给出具体访问的网页时,就默认index.html,也就是上面的代码。
输入学号201215122,得到学生信息
10、总结
总结。。没有总结。总之心累,弄了一下午,莫名奇妙的bug有一些,你清空项目后重新编译,发现又好了。。
源码地址:https://download.csdn.net/download/jdbdh/10587199