如何搭建 SSM 框架集成环境?

SsM 框架集成环境搭建方式

jar 包依赖添加

web.xml 文件配置

Springmvc 配置文件 servlet-context.xml 添加

Spring.xml 配置

案例实操

1.jar 包依赖添加(原有基础上继续添加 springmvc 相关依赖 jar 包及对应 jetty 插件) 修改 pom.xml 文件

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"> 

4.0.0 

com.xxx 

spring_mybatis 

war 

0.0.1-SNAPSHOT 

spring_mybatis Maven Webapp 

http://maven.apache.org 

 


 

junit 

junit 

4.12 

test 

 


 

org.springframework 

spring-context 

4.3.2.RELEASE 

 


 

org.springframework 

spring-test 

4.3.2.RELEASE 

 


org.springframework 

spring-jdbc 

4.3.2.RELEASE 

 


 

org.springframework 

spring-tx 

4.3.2.RELEASE 

 


 

org.aspectj 

aspectjweaver 

1.8.9 

 


 

c3p0 

c3p0 

0.9.1.2 

 


 

org.mybatis 

mybatis 

3.4.1 

 


 

org.mybatis 

mybatis-spring 

1.3.0 

 


 

mysql 

mysql-connector-java 

5.1.39 

 


 

org.slf4j 

slf4j-log4j12 

1.7.2 

 

 

org.slf4j 

slf4j-api 

1.7.2 

 


 

com.github.pagehelper 

pagehelper 

4.1.0 

 


 

org.springframework 

spring-web 

4.3.2.RELEASE 

 


 

org.springframework 

spring-webmvc 

4.3.2.RELEASE 

 


 

javax.servlet 

javax.servlet-api 

3.0.1 

 


 

com.fasterxml.jackson.core 

jackson-core 

2.7.0 

 

 

com.fasterxml.jackson.core 

jackson-databind 

2.7.0 

 

 

com.fasterxml.jackson.core 

jackson-annotations 

2.7.0 

 


 

commons-fileupload 

commons-fileupload 

1.3.2 

 

 

 

 ssM 

  

 

 **src/main/resources** 

  

 

 **src/main/java** 

  

***\*/\*.xml** 

***\*/\*.properties** 

***\*/\*.tld** 

  

 **false** 

 

 

 

 

org.mybatis.generator 

mybatis-generator-maven-plugin 

1.3.2 

 

 src/main/resources/generatorConfig.xml 

true 

true 

 

 

 

org.mybatis.generator 

mybatis-generator-core 

1.3.2 

 

 

 


 

org.apache.maven.plugins 

maven-compiler-plugin 

2.3.2 

 

1.7 

1.7 

UTF-8 

 

${project.basedir}/lib/rt.jar 

 

 

 


 

org.mortbay.jetty 

maven-jetty-plugin 

6.1.21 

 

10 

/ssM 

 

 

 

 

2.web.xml 文件配置


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_3_0.xsd" 

id="WebApp_ID" version="3.0"> 

 

contextConfigLocation 

classpath:spring.xml 

 

 

class>org.springframework.web.context.ContextLoaderListener 

 

 

 char encoding filter 

encodingFilter 

class>org.springframework.web.filter.CharacterEncodingFilter 

 

encoding 

UTF-8 

 

 

 

encodingFilter 

/* 

 

 

springMvc 

class>org.springframework.web.servlet.DispatcherServlet 

 

contextConfigLocation 

classpath:servlet-context.xml 

 

1 

 

 

springMvc 

*.do 

 

3.Springmvc 配置文件 servlet-context.xml 添加


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc" 

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

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" 

xsi:schemaLocation="

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

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

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

http://www.springframework.org/schema/aop

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

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 


 


 


class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

value="org.springframework.web.servlet.view.JstlView" /> 

 

 

 

 


class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHand 

lerMapping"> 

 

class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHand 

lerAdapter"> 

  

 

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConver 

ter" /> 

 

 

 


class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

 

104857600 

 

 

4096 

 

 

4.Spring.xml 配置


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" 

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd 

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

http://www.springframework.org/schema/context/spring-context.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-3.0.xsd"> 


 

 

 


 

 


 

 

 

 

 

 


class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 

  

 


 

 

 

 

 

 

 

 

 

 

 

 


 

expression="execution(* com.xxx.service..*.*(..))" /> 

 

 


class="org.mybatis.spring.SqlSessionFactoryBean"> 

 

 

value="classpath:com/xxx/mapper/*.xml" /> 

 


class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 


 

 

 

扩展

Helloworld 编写

搭建好SsM集成框架,Hellocontroller 的编写就变得非常简单

@Controller 

public class HelloController { 

/** 

* 注入 service 层 userService 接口 

*/ 

@Autowired 

private UserService userService; 

@RequestMapping("/hello") 

public ModelAndView hello(){ 

ModelAndView mv=new ModelAndView(); 

/** 

* 调用 service 层查询方法 

*/ 

User user=userService.queryUserById(7); 

System.out.println(user); 

mv.addObject("user", user); 

mv.setViewName("hello"); 

return mv; 

}

视图页面部分代码接收结果显示:

 

欢迎你,${user.userName} 

启动 jetty 服务器,访问地址: http://localhost:8080/ssM/hello.do

你可能感兴趣的:(如何搭建 SSM 框架集成环境?)