搭建SSM框架

Posted lilihexiaogou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建SSM框架相关的知识,希望对你有一定的参考价值。

SSM框架整合

  • pom中添加依赖
  • 添加编辑Spring配置文件
  • 添加编辑SpringMVC配置文件
  • 添加编辑Mybatis配置文件
  • 配置web.xml

1、pom中添加依赖

<dependencies>
    <!--junit单元测试依赖-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <!--mysql驱动包依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.45</version>
    </dependency>
    <!--mybatis依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
    </dependency>
    <!--mybatis-generator插件依赖-->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>

    <!-- spring框架依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>$spring.version</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$spring.version</version>
    </dependency>

    <!--mybatis集成spring的依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.0</version>
    </dependency>
    <!--c3p0-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>$spring.version</version>
    </dependency>

    <!--日志-->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.3</version>
    </dependency>

    <!--servlet依赖-->
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!--jstl-->
    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>

    <!--json解析依赖-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.4</version>
    </dependency>
</dependencies>

2、添加编辑Spring配置文件

<context:component-scan base-package="com.lili">
    <!--spring中排出controller-->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>

<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- c3p0带有连接池的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="$db.driver"></property>
    <property name="jdbcUrl" value="$db.url"></property>
    <property name="user" value="$db.username"></property>
    <property name="password" value="$db.password"></property>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <property name="mapperLocations" value="classpath:com/lili/mapper/*Mapper.xml" ></property>
    <property name="dataSource"  ref="dataSource"></property>

</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="sqlSessionFactory" index="0"></constructor-arg>
</bean>
<!-- 配置mybatis mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.lili.dao"/>
    <property name="sqlSessionTemplateBeanName" value="sqlSession"/>
</bean>

3、添加编辑SpringMVC配置文件

<context:component-scan base-package="com.lili.controller" annotation-config="true">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--支持Json的Bean,配置ViewResolver。 可以用多个ViewResolver。 使用order属性排序。 InternalResourceViewResolver放在最后。 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="defaultViews">
        <list>
            <!-- JSON View -->
            <bean
                  class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
            </bean>
        </list>
    </property>
</bean>

<!--配置json格式另外方式:
@ResponseBody
利用SpringMVC的Jackson插件将返回值序列化为Json.-->
<mvc:annotation-driven>

    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

4、添加配置Mybatis配置文件

<configuration>
    <!-- autoMappingBehavior should be set in each test case -->
    <!-- 配置参数 -->
    <settings>
        <!-- 使用LOG4J日志 -->
        <setting  name="logImpl"  value="LOG4J" />
    </settings>
</configuration>

5、配置Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <!--step1:加载spring配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <!--step2;加载监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--step3:加载DispacherServlet-->
    <servlet>
        <servlet-name>dispacherservlet</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>dispacherservlet</servlet-name>
        <!-- 默认路径 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

6、注意点

  • sql映射文件的命名空间namespace要与dao接口的名称保持一致
  • DAO接口中方法方法名与sql映射文件中id一致
  • dao接口中方法的形参加注解@Param

以上是关于搭建SSM框架的主要内容,如果未能解决你的问题,请参考以下文章

使用IDEA搭建SSM框架

SSM框架超详细的使用eclipse工具搭建SSM框架,一看就会

SSM框架超详细的使用eclipse工具搭建SSM框架,一看就会

使用maven搭建ssm框架的javaweb项目

SSM框架整合 环境搭建

idea ssm框架搭建