美文网首页
SSM 框架集成环境搭建方式

SSM 框架集成环境搭建方式

作者: 天上的小仙女呀 | 来源:发表于2020-11-24 08:46 被阅读0次
    • jar 包依赖添加
    • web.xml 文件配置
    • Springmvc 配置文件 servlet-context.xml 添加
    • Spring.xml 配置

    案例实操

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

    <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.xxx</groupId> 
    
        <artifactId>spring_mybatis</artifactId> 
    
        <packaging>war</packaging> 
    
        <version>0.0.1-SNAPSHOT</version> 
    
        <name>spring_mybatis Maven Webapp</name> 
    
        <url>http://maven.apache.org</url> 
    
        <dependencies> 
    
            <!-- junit 测试 --> 
    
            <dependency> 
    
                <groupId>junit</groupId> 
    
                <artifactId>junit</artifactId> 
    
                <version>4.12</version> 
    
                <scope>test</scope> 
    
            </dependency> 
    
            <!-- spring 核心 jar --> 
    
            <dependency> 
    
                <groupId>org.springframework</groupId> 
    
                <artifactId>spring-context</artifactId> 
    
                <version>4.3.2.RELEASE</version> 
    
            </dependency> 
    
            <!-- spring 测试 jar --> 
    
            <dependency> 
    
                <groupId>org.springframework</groupId> 
    
                <artifactId>spring-test</artifactId> 
    
                <version>4.3.2.RELEASE</version> 
    
            </dependency> 
    
            <!-- spring jdbc --> 
    
            <dependency>
    
                <groupId>org.springframework</groupId> 
    
                <artifactId>spring-jdbc</artifactId> 
    
                <version>4.3.2.RELEASE</version> 
    
            </dependency> 
    
            <!-- spring 事物 --> 
    
            <dependency> 
    
                <groupId>org.springframework</groupId> 
    
                <artifactId>spring-tx</artifactId> 
    
                <version>4.3.2.RELEASE</version> 
    
            </dependency> 
    
            <!-- aspectj 切面编程的 jar --> 
    
            <dependency> 
    
                <groupId>org.aspectj</groupId> 
    
                <artifactId>aspectjweaver</artifactId> 
    
                <version>1.8.9</version> 
    
            </dependency> 
    
            <!-- c3p0 连接池 --> 
    
            <dependency> 
    
                <groupId>c3p0</groupId> 
    
                <artifactId>c3p0</artifactId> 
    
                <version>0.9.1.2</version> 
    
            </dependency> 
    
            <!-- mybatis --> 
    
            <dependency> 
    
                <groupId>org.mybatis</groupId> 
    
                <artifactId>mybatis</artifactId> 
    
                <version>3.4.1</version> 
    
            </dependency> 
    
        <!-- 添加 mybatis 与 Spring 整合的核心包 --> 
    
            <dependency> 
    
                <groupId>org.mybatis</groupId> 
    
                <artifactId>mybatis-spring</artifactId> 
    
                <version>1.3.0</version> 
    
            </dependency> 
    
            <!-- mysql 驱动包 --> 
    
            <dependency> 
    
                <groupId>mysql</groupId> 
    
                <artifactId>mysql-connector-java</artifactId> 
    
                <version>5.1.39</version> 
    
            </dependency> 
    
            <!-- 日志打印相关的 jar --> 
    
            <dependency> 
    
                <groupId>org.slf4j</groupId> 
    
                <artifactId>slf4j-log4j12</artifactId> 
    
                <version>1.7.2</version> 
    
            </dependency> 
    
            <dependency> 
    
                <groupId>org.slf4j</groupId> 
    
                <artifactId>slf4j-api</artifactId> 
    
                <version>1.7.2</version> 
    
            </dependency> 
    
            <!-- 分页插件配置 --> 
    
            <dependency> 
    
                <groupId>com.github.pagehelper</groupId> 
    
                <artifactId>pagehelper</artifactId> 
    
                <version>4.1.0</version> 
    
            </dependency> 
    
            <!-- spring web --> 
    
            <dependency> 
    
                <groupId>org.springframework</groupId> 
    
                <artifactId>spring-web</artifactId> 
    
                <version>4.3.2.RELEASE</version> 
    
            </dependency> 
    
            <!-- spring mvc --> 
    
            <dependency> 
    
                <groupId>org.springframework</groupId> 
    
                <artifactId>spring-webmvc</artifactId> 
    
                <version>4.3.2.RELEASE</version> 
    
            </dependency> 
    
            <!-- web servlet --> 
    
            <dependency> 
    
                <groupId>javax.servlet</groupId> 
    
                <artifactId>javax.servlet-api</artifactId> 
    
                <version>3.0.1</version> 
    
            </dependency> 
    
            <!-- 添加 json 依赖 jar 包 --> 
    
            <dependency> 
    
                <groupId>com.fasterxml.jackson.core</groupId> 
    
                <artifactId>jackson-core</artifactId> 
    
                <version>2.7.0</version> 
    
            </dependency> 
    
            <dependency> 
    
                <groupId>com.fasterxml.jackson.core</groupId> 
    
                <artifactId>jackson-databind</artifactId> 
    
                <version>2.7.0</version> 
    
            </dependency> 
    
            <dependency> 
    
                <groupId>com.fasterxml.jackson.core</groupId> 
    
                <artifactId>jackson-annotations</artifactId> 
    
                <version>2.7.0</version> 
    
            </dependency> 
    
            <!-- 文件上传包依赖 --> 
    
                <dependency> 
    
                <groupId>commons-fileupload</groupId> 
    
                <artifactId>commons-fileupload</artifactId> 
    
                <version>1.3.2</version> 
    
            </dependency> 
    
        </dependencies> 
    
        <build> 
    
            <finalName>ssm</finalName> 
    
            <resources> 
    
                <resource> 
    
                    <directory>**src/main/resources**</directory> 
    
                </resource> 
    
                <resource> 
    
                    <directory>**src/main/java**</directory> 
    
                    <includes> 
    
                        <include>***\*/\*.xml**</include> 
    
                        <include>***\*/\*.properties**</include> 
    
                        <include>***\*/\*.tld**</include> 
    
                    </includes> 
    
                    <filtering>**false**</filtering> 
    
            </resource> 
    
        </resources> 
    
        <plugins> 
    
                <plugin> 
    
                    <groupId>org.mybatis.generator</groupId> 
    
                    <artifactId>mybatis-generator-maven-plugin</artifactId> 
    
                    <version>1.3.2</version> 
    
                    <configuration> 
    
                     <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 
    
                        <verbose>true</verbose> 
    
                        <overwrite>true</overwrite> 
    
                    </configuration> 
    
            <dependencies> 
    
                <dependency> 
    
                    <groupId>org.mybatis.generator</groupId> 
    
                    <artifactId>mybatis-generator-core</artifactId> 
    
                    <version>1.3.2</version> 
    
                </dependency> 
    
            </dependencies> 
    
            </plugin> 
    
            <!-- 编译环境插件 --> 
    
            <plugin> 
    
                <groupId>org.apache.maven.plugins</groupId> 
    
                <artifactId>maven-compiler-plugin</artifactId> 
    
                <version>2.3.2</version> 
    
                <configuration> 
    
                    <source>1.7</source> 
    
                    <target>1.7</target> 
    
                    <encoding>UTF-8</encoding> 
    
                    <compilerArguments> 
    
                        <bootclasspath>${project.basedir}/lib/rt.jar</bootclasspath> 
    
                    </compilerArguments> 
    
                </configuration> 
    
            </plugin> 
    
            <!-- 配置 jetty 插件 --> 
    
            <plugin> 
    
                <groupId>org.mortbay.jetty</groupId> 
    
                <artifactId>maven-jetty-plugin</artifactId> 
    
                <version>6.1.21</version> 
    
                <configuration> 
    
                    <scanIntervalSeconds>10</scanIntervalSeconds> 
    
                    <contextPath>/ssm</contextPath> 
    
                </configuration> 
    
            </plugin> 
    
        </plugins> 
    
        </build> 
    
    </project> 
    

    2.web.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_3_0.xsd" 
    
        id="WebApp_ID" version="3.0"> 
    
        <context-param> 
    
            <param-name>contextConfigLocation</param-name> 
    
            <param-value>classpath:spring.xml</param-value> 
    
        </context-param> 
    
        <listener> 
    
        <listener-
    
            class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    
        </listener> 
    
        <filter> 
    
            <description>char encoding filter</description> 
    
        <filter-name>encodingFilter</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>encodingFilter</filter-name> 
    
            <url-pattern>/*</url-pattern> 
    
        </filter-mapping> 
    
        <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:servlet-context.xml</param-value> 
    
            </init-param> 
    
            <load-on-startup>1</load-on-startup> 
    
        </servlet> 
    
        <servlet-mapping> 
    
            <servlet-name>springMvc</servlet-name> 
    
            <url-pattern>*.do</url-pattern> 
    
        </servlet-mapping> 
    
    </web-app> 
    

    3.Springmvc 配置文件 servlet-context.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: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"> 
    
        <!-- 扫描 com.xxx.controller 下包 --> 
    
        <context:component-scan base-package="com.xxx.controller" /> 
    
        <!-- mvc 请求映射处理器与适配器 --> 
    
        <mvc:annotation-driven /> 
    
        <!--配置视图解析器 默认的视图解析器- --> 
    
        <bean id="defaultViewResolver" 
    
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    
            <property name="viewClass" 
    
            value="org.springframework.web.servlet.view.JstlView" /> 
    
            <property name="contentType" value="text/html" /> 
    
            <property name="prefix" value="/WEB-INF/jsp/" /> 
    
            <property name="suffix" value=".jsp" /> 
    
        </bean> 
    
        <!-- json 支持 --> 
    
        <bean 
    
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHand 
    
        lerMapping"> 
    
        </bean> 
    
        <bean 
    
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHand 
    
        lerAdapter"> 
    
            <property name="messageConverters"> 
    
        <list> 
    
            <bean 
    
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConver 
    
            ter" /> 
    
        </list> 
    
        </property> 
    
        </bean> 
    
        <!-- 文件上传配置 --> 
    
        <bean id="multipartResolver" 
    
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    
            <property name="maxUploadSize"> 
    
            <value>104857600</value> 
    
            </property> 
    
            <property name="maxInMemorySize"> 
    
            <value>4096</value> 
    
            </property> 
    
        </bean> 
    
    </beans> 
    

    4.Spring.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: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"> 
    
        <!-- 扫描基本包 过滤 controller 层 --> 
    
        <context:component-scan base-package="com.xxx" > 
    
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
    
        </context:component-scan> 
    
        <!-- 加载 properties 配置文件 --> 
    
        <context:property-placeholder location="classpath:db.properties" /> 
    
        <aop:aspectj-autoproxy /><!-- aop --> 
    
    
        <!-- 配置 c3p0 数据源 --> 
    
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    
            <property name="driverClass" value="${driver}"></property> 
    
            <property name="jdbcUrl" value="${url}"></property> 
    
            <property name="user" value="${user}"></property> 
    
            <property name="password" value="${password}"></property> 
    
        </bean> 
    
        <!-- 配置事务管理器 --> 
    
        <bean id="txManager" 
    
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    
            <property name="dataSource" ref="dataSource"></property> 
    
        </bean> 
    
        <!-- 设置事物增强 --> 
    
        <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 切面配置 --> 
    
        <aop:config> 
    
        <aop:pointcut id="servicePointcut" 
    
        expression="execution(* com.xxx.service..*.*(..))" /> 
    
             <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" /> 
    
        </aop:config> 
    
        <!-- 配置 sqlSessionFactory --> 
    
        <bean id="sqlSessionFactory"  
    
        class="org.mybatis.spring.SqlSessionFactoryBean"> 
    
            <property name="dataSource" ref="dataSource"></property> 
    
            <property name="configLocation" value="classpath:mybatis.xml" /> 
    
            <property name="mapperLocations"  
    
            value="classpath:com/xxx/mapper/*.xml" /> 
    
        </bean> 
    
        <!-- 配置扫描器 --> 
    
        <bean id="mapperScanner"  
    
        class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    
        <!-- 扫描 com.xxx.dao 这个包以及它的子包下的所有映射接口类 --> 
    
            <property name="basePackage" value="com.xxx.dao" /> 
    
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
    
        </bean> 
    
    </beans> 
    

    扩展

    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; 
    
        } 
    
    } 
    

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

    <body> 
     
        欢迎你,${user.userName} 
    
    </body> 
    

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

    相关文章

      网友评论

          本文标题:SSM 框架集成环境搭建方式

          本文链接:https://www.haomeiwen.com/subject/kmabiktx.html