SSM Integration Framework (with source)

Brief

SSM integrated main frame of the Spring and MyBatis integration, SpringMVC natural and Spring integration.

1. Create a new Maven project IDEA

Create a maven project

Figure 1. Create a web-app

1.2 does create a folder structure as follows
Directory Structure

Figure 2. The folder structure

2. Add relevant dependent

2.1 Configuration pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <!--自定义一个版本变量,方便更新版本-->
    <spring.version>5.1.5.RELEASE</spring.version>
    <mybatis.version>3.4.6</mybatis.version>
    <c3p0.version>0.9.5.4</c3p0.version>
</properties>
<dependencies>
    <!-- 默认的idea创建的依赖版本是4.11,后续集成测试时,提示Junit的版本过低,至少需要4.12 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!--Spring核心包-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <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>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--Spring的依赖添加结束,如有其他需求请自行添加-->
    <!--MyBatis核心包-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!--MyBatis依赖包到此结束-->
    <!--Spring-Mybatis集成的依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!--Spring集成Mybatis的依赖结束-->
    <!--用于连接数据库的依赖,这里来连接MySQL,其他数据库自行更换包-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
    <!--数据连接池选择c3p0-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3p0.version}</version>
    </dependency>
    <!--数据库相关到此结束-->
    <!--JavaWeb需要的-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!--简化代码的依赖-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
        <scope>provided</scope>
    </dependency>
    <!--Spring的测试包-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Figure 3.pom.xml

2.2 Creating a database

CREATE DATABASE `test`;
USE `test`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
Figure 4.test create sql database

3.Spring integration with Mybatis

3.1 Creating a applicationContext.xml in resources / spring and configured as follows

<?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"
       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">
    <!--配置自动扫描-->
    <!--
        context:component-scan 会自动扫描当前包及其子包,
        并且每一个context:componment-scan都会生成一个bean,
        当在后续的SpringMVC中需要配置控制器,所以不排除Controller和RestController注解,
        就会扫描到两个同类型的bean,导致Spring不知道要装配那个bean。
    -->
    <context:component-scan base-package="cn.edu.nit">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
    </context:component-scan>
    <!--用于管理整个Spring上下文-->
    <import resource="classpath:spring/spring-*.xml"/>
</beans>
图5. applicationContext.xml

3.2 Creating a jdbc.properties in resources and configuration is as follows (user and password database according to their availability)

jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT%2B8
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.user=developer
jdbc.password=123456
Figure 6.jdbc.properties

3.3 Create in a spring-mybatis.xml resources / spring and configured as follows

<?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"
       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">
    <!--配置数据源-->
    <!--1. 先加载数据库的配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--2.配置DataSource,采用c3p0的-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置Spring事务用于管理Mybatis的DataSource-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置MyBatis-->
    <!--1. 自动扫描mapper.xml文件和会话工厂,配置mapper是操作那个数据源-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--注意:这里是Mapperlocations,不是configLocation-->
        <property name="mapperLocations" value="classpath:cn/edu/nit/mapper/*.xml"/>
    </bean>
    <!--2.自动扫描Mapper层的接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="cn.edu.nit.mapper"/>
    </bean>
</beans>
FIG 7.spring-mybatis.xml

3.4 test the Spring and MyBatis integrated environment

package cn.edu.nit.test;

import cn.edu.nit.pojo.User;
import cn.edu.nit.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringMyBatisTest {
    @Autowired
    private UserService userService;
    @Test
    public void t1(){
        User user = new User();
        user.setName("developer");
        user.setPassword("123456");
        userService.insertUser(user);
    }
}
Figure 8. Spring testing and integration class MyBatis
  • Document all changes, test results shown in Figure 9.

Figure 9.SpringMybatis integration test results chart

4. Configuration and Tomcat SpringMVC

4.1 Configuration SpringMVC

  • Creating springmvc.xml in the resources folder
<?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"
       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">
    <!--1.扫描控制层的控制器-->
    <context:component-scan base-package="cn.edu.nit">
        <!--include-filter是只扫描有这个注解的-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
    </context:component-scan>
    <!--2.配置内部视图解析器-->
    <!--转发的时候会经过,重定向不会-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
FIG 10. springmvc.xml
  • Adding to the resources / spring / applicationContext.xml some configuration
<import resource="classpath:springmvc.xml"/>
11. FIG added to resources / spring / applicationContext.xml Code
  • Springmvc arranged in WEB-INF / web.xml in
<?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">
    <display-name>Archetype Created Web Application</display-name>
    <!--配置上下文配置文件地址-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
    <!--配置Spring上下文加载监听器,-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--配置springmvc拦截器-->
    <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:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--以下的是一些优化的不是必要的的,视情况而定-->
    <!--编码过滤器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <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>
    <!--配置Spring内存溢出监听器-->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
FIG 12.WEB-INF / web.xml

4.2. Environment (container) runtime configuration
Configuring Tomcat

Figure 13. Configuration Tomcat

5. Test run results

5.1 Test Results

Figure 14./user/query viewName = "show" results


FIG 15./user/query viewName = "redirect: /jsp/show.jsp" Results

Note: 14 is forwarding the request, the address bar of your browser is http: // localhost: 8080 / user / query, and FIG. 15 is a redirection, the address bar is http: // localhost: 8080 / jsp / show .jsp, and the data is lost during the second initiated the request.

6. Related Exception & Error Tips

  • 6.1 . org.springframework.beans.factory.BeanCreationException
    提示 1: Each component-scan will scan it again Spring context, and create a Bean, if there are multiple component-scan to scan, then these component-scan scanned the intersection more of the same type will appear in Bean, not leading to Spring know how to inject, it throws an exception.
    Tip 2: the web.xml in applicationContext.xml did not scan all the notes, would eventually be thrown BeanCreationException , springmvc.xml configured @Controller and @RestController take effect. All can also be configured in a spring file, as long as the problem on the line, love how on how equipped with .
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
  • 6.2 jstl related But with no problem
    Tip: Check uri <% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>

7. complete code

8. Reference documents and other

9. Other

  • Reprinted show in reference to the document.
  • Related issues can be reacted in the source address.

Guess you like

Origin www.cnblogs.com/liguiyangcn/p/11518255.html