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

148 阅读1分钟

SsM 框架集成环境搭建方式

  • jar 包依赖添加

  • web.xml 文件配置

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

  • Spring.xml 配置

案例实操

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

<project xmlns="maven.apache.org/POM/4.0.0"

xmlns:xsi="www.w3.org/2001/XMLSch…"

xsi:schemaLocation="maven.apache.org/POM/4.0.0

maven.apache.org/maven-v4\_0…

4.0.0

com.xxx

spring_mybatis

war

0.0.1-SNAPSHOT

spring_mybatis Maven Webapp

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 文件配置

<web-app xmlns:xsi="www.w3.org/2001/XMLSch…"

xmlns="java.sun.com/xml/ns/java…"

xsi:schemaLocation="java.sun.com/xml/ns/java…

java.sun.com/xml/ns/java…"

id="WebApp_ID" version="3.0">

contextConfigLocation

classpath:spring.xml

<listener-

class>org.springframework.web.context.ContextLoaderListener

char encoding filter

encodingFilter

<filter

class>org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

springMvc

<servlet

class>org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:servlet-context.xml

1

springMvc

*.do

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

<beans xmlns="www.springframework.org/schema/bean…"

xmlns:xsi="www.w3.org/2001/XMLSch…"

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

xmlns:context="www.springframework.org/schema/cont…"

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

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

xsi:schemaLocation="

www.springframework.org/schema/mvc

www.springframework.org/schema/mvc/…

www.springframework.org/schema/bean…

www.springframework.org/schema/bean…

www.springframework.org/schema/cont…

www.springframework.org/schema/cont…

www.springframework.org/schema/aop

www.springframework.org/schema/aop/…

www.springframework.org/schema/tx

www.springframework.org/schema/tx/s…

<context:component-scan base-package="com.xxx.controller" />

<mvc:annotation-driven />

<bean id="defaultViewResolver"

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

<property name="viewClass"

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

<bean

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

lerMapping">

<bean

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

lerAdapter">

<bean

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

ter" />

<bean id="multipartResolver"

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

104857600

4096

4.Spring.xml 配置

<beans xmlns="www.springframework.org/schema/bean…"

xmlns:xsi="www.w3.org/2001/XMLSch…"

xmlns:context="www.springframework.org/schema/cont…"

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

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

xsi:schemaLocation="www.springframework.org/schema/bean…

www.springframework.org/schema/bean…

www.springframework.org/schema/cont…

www.springframework.org/schema/cont…

www.springframework.org/schema/aop

www.springframework.org/schema/aop/…

www.springframework.org/schema/tx

www.springframework.org/schema/tx/s…

<context:component-scan base-package="com.xxx" >

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />

</context:component-scan>

<context:property-placeholder location="classpath:db.properties" />

<aop:aspectj-autoproxy />

<bean id="txManager"

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

<tx:advice id="txAdvice" transaction-manager="txManager">

tx:attributes

<tx:method name="get*" read-only="true" />

<tx:method name="find*" read-only="true" />

<tx:method name="query*" read-only="true" />

<tx:method name="load*" read-only="true" />

<tx:method name="add*" propagation="REQUIRED" />

<tx:method name="insert*" propagation="REQUIRED" />

<tx:method name="update*" propagation="REQUIRED" />

<tx:method name="delete*" propagation="REQUIRED" />

</tx:attributes>

</tx:advice>

aop:config

<aop:pointcut id="servicePointcut"

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

<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />

</aop:config>

<bean id="sqlSessionFactory"

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

<property name="mapperLocations"

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

<bean id="mapperScanner"

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