搭建 Spring+SpringMVC+MyBatis 框架

liziHexiaogou發表於2019-09-10

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
本作品採用《CC 協議》,轉載必須註明作者和本文連結
小狗和莉莉

相關文章