目录
(一)创建父类项目:新建maven project,项目名:maven-ssm-base,packageing:pom
1.创建maven module,项目名:maven-ssm-mapper,packaging:jar
2.创建maven module,项目名:maven-ssm-bean,packaging:jar
2.创建maven module,项目名:maven-ssm-service,packaging:jar
3.创建maven module,项目名:maven-ssm-controller,packaging:war
(一)创建父类项目:新建maven project,项目名:maven-ssm-base,packageing:pom
新建maven project,项目名:maven-ssm-base,packageing:pom
(二)创建子类项目
1.创建maven module,项目名:maven-ssm-mapper,packaging:jar
2.创建maven module,项目名:maven-ssm-bean,packaging:jar
2.创建maven module,项目名:maven-ssm-service,packaging:jar
3.创建maven module,项目名:maven-ssm-controller,packaging:war
(三)完善各个模块代码
1.将bean模块打成jar包,并在mapper模块中引用
(1)右键maven-ssm-bean项目-【run as】-【maven install】,发现报了如下错误:
解决这个错误很简单,右键maven-ssm-bean项目-【build path】-【configure build path】
然后再次右键maven-ssm-bean项目-【run as】-【maven install】,发现build成功!
然后我们去maven-ssm-mapper的pom.xml的dependencies中添加maven-ssm-bean包
创建测试,在mapper模块右键new-other-Junit test case
service模块中引入mapper包
在service模块中新建测试代码
ItemMapperTest.java:
package com.sikiedu.mapper;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sikiedu.bean.ItemInfo;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_mapper.xml")
public class ItemMapperTest {
@Autowired
private ItemMapper itemMapper;
@Test
public void test() {
System.out.println("this is ItemMapperTest");
List<ItemInfo> allList = itemMapper.getAllList();
for (ItemInfo itemInfo : allList) {
System.out.println("itemInfo="+itemInfo);
}
}
}
ItemServicelmplTest.java:
package com.sikiedu.service;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sikiedu.bean.ItemInfo;
//@ContextConfiguration(locations={"classpath:applicationContext_service.xml",
// "classpath:applicationContext_transaction.xml"})
@ContextConfiguration("classpath*:applicationContext_*.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ItemServicelmplTest {
@Autowired
private ItemService itemService;
@Test
public void test() {
System.out.println("this is ItemServicelmplTest");
List<ItemInfo> allList = itemService.getAllList();
for (ItemInfo itemInfo : allList) {
System.out.println("itemInfo="+itemInfo);
}
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Maven-ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.ttf</url-pattern>
<url-pattern>*.woff</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext_*.xml</param-value>
</context-param>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
运行结果:
剩下的代码参考:https://blog.csdn.net/qq_40323256/article/details/90168191