一部分是 spring与mybatis间的整合.  一部分凡是 spring与mybatis间的整合.

旷日持久没有写博客了, 今年业务太多了, 也从没了心思. 去深圳出差, 转java,
更主要的成婚的事务, 一蔸交接一茬. 好于近来空余一些,
就想记录有就困扰了我的问题(现在就此spring boot真是无比好了, 很多题材,
都不见面另行是问题).

长期无写博客了, 今年事务太多了, 也尚无了心思. 去深圳出差, 转java,
更要紧之洞房花烛的业务, 一茬接一茬. 好当最近空一些,
就想记录有都困扰了自己之题材(现在于是spring boot真是最有利了, 很多题目,
都非会见又是问题).

SSM(spring + springmvc + mybatis), 是一模一样缓缓非常流行的框架.
所以把当下款框架搭建筑起来, 也是开发的均等项基本功.

SSM(spring + springmvc + mybatis), 是同样款特别流行的框架.
所以把及时款框架搭建筑起来, 也是出的一样项核心功.

SSM的结, 分为少有的:

SSM的结缘, 分为零星局部:

  一部分凡是 spring与springmvc间的重组,

  一部分是 spring与springmvc间的三结合,

  一部分凡是 spring与mybatis间的整合.

  一部分是 spring与mybatis间的整合.

实际上, 也当是这么, 因为 springmvc与mybatis间, 没有其他关系.
都不过是跟spring有关系.

其实, 也应当是这么, 因为 springmvc与mybatis间, 没有外关系.
都止是与spring有关系.

始于的目录结构:

初始的目结构:

图片 1

图片 2

 

 

不论是是为此eclipse还是用idea, 都是采取maven来治本jar包,
且把援在pom.xml文件被, 可以告别繁琐的jar包引入.

任凭是因此eclipse还是用idea, 都是运用maven来管理jar包,
且把援位于pom.xml文件中, 可以告别繁琐的jar包引入.

ssh, ssm 框架的 pom.xml文件, 网上还是力所能及搜获的, 大把好把,
真的如错过多框架的时光, 可以去网上直接下一个不怕推行了. 没必要这么麻烦.

ssh, ssm 框架的 pom.xml文件, 网上都是力所能及搜获的, 大把万分把,
真的比方失去多框架的早晚, 可以错过网上直接下一个就算执行了. 没必要这样麻烦.

一. jar包引入

一. jar包引入

<properties>
        <spring.version>4.3.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!--             spring核心包               -->
        <dependency>
            <groupId>org.springframework</groupId>
            <!--包含Spring 框架基本的核心工具类-->
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--所有应用都用到,包含访问配置文件,创建和管理bean等,是Spring IOC的基础实现-->
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--提供在基础IOC功能上的扩展服务-->
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--Spring提供的对AspectJ框架的整合-->
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--Spring的面向切面编程,提供AOP(面向切面编程)的实现-->
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--     springmvc相关的spring包     -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring数据库相关包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--对象的XML映射,可以让Java与XML之间来回切换-->
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <!--用于在Spring 中集成AspectJ AspectJ LTW织入器-->
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.10</version>
        </dependency>


        <!--     mybatis/spring包     -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
        <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.38</version>
        </dependency>

        <!--   日志包    -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.20</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.20</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.11</version>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.5</version>
        </dependency>

        <!--jackson包是springmvc返回json数据时用到的-->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>


    </dependencies>
    <build>
        <finalName>ssm</finalName>
        <plugins>
            <!--解决idea中java compile 和 language level在更新pom文件后,重置到5的版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
<properties>
        <spring.version>4.3.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!--             spring核心包               -->
        <dependency>
            <groupId>org.springframework</groupId>
            <!--包含Spring 框架基本的核心工具类-->
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--所有应用都用到,包含访问配置文件,创建和管理bean等,是Spring IOC的基础实现-->
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--提供在基础IOC功能上的扩展服务-->
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--Spring提供的对AspectJ框架的整合-->
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--Spring的面向切面编程,提供AOP(面向切面编程)的实现-->
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--     springmvc相关的spring包     -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring数据库相关包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--对象的XML映射,可以让Java与XML之间来回切换-->
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <!--用于在Spring 中集成AspectJ AspectJ LTW织入器-->
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.10</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.10</version>
        </dependency>


        <!--     mybatis/spring包     -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
        <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.38</version>
        </dependency>

        <!--   日志包    -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.20</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.20</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.11</version>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.5</version>
        </dependency>

        <!--jackson包是springmvc返回json数据时用到的-->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>


    </dependencies>
    <build>
        <finalName>ssm</finalName>
        <plugins>
            <!--解决idea中java compile 和 language level在更新pom文件后,重置到5的版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

 

地方的保, 还蕴藏了mybatis的包, 到组合mybatis的时段,
就未会见重复写一全体pom.xml文件了

地方的管教, 还含了mybatis的保险, 到做mybatis的当儿,
就不见面重写一一体pom.xml文件了

二. web.xml文件配置spring和springmvc入口

二. web.xml文件配置spring和springmvc入口

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/application.xml</param-value>        
    </context-param>

    <!-- 加载log4j的配置文件log4j.properties -->
    <!--<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>
            classpath:conf/log4j.properties
        </param-value>
    </context-param>-->
    <!-- 设定刷新日志配置文件的时间间隔,这里设置为10s -->
   <!-- <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>10000</param-value>
    </context-param>-->

    <!--解决post乱码问题-->
    <filter>
        <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>

    <!--   配置spring启动listener入口 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

   <!-- <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>-->

    <!--springmvc入口servlet-->
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--contextConfigLocation如果不配置, 则会按照默认的方式去找文件, 如果找不到, 则会抛出异常,
        所以这里推荐还是要写一下. 我这里配置的空, 因为在前面application.xml中, 已经import了这里需要的xml文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                <!--classpath:conf/spring-mvc.xml-->
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/application.xml</param-value>        
    </context-param>

    <!-- 加载log4j的配置文件log4j.properties -->
    <!--<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>
            classpath:conf/log4j.properties
        </param-value>
    </context-param>-->
    <!-- 设定刷新日志配置文件的时间间隔,这里设置为10s -->
   <!-- <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>10000</param-value>
    </context-param>-->

    <!--解决post乱码问题-->
    <filter>
        <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>

    <!--   配置spring启动listener入口 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

   <!-- <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>-->

    <!--springmvc入口servlet-->
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--contextConfigLocation如果不配置, 则会按照默认的方式去找文件, 如果找不到, 则会抛出异常,
        所以这里推荐还是要写一下. 我这里配置的空, 因为在前面application.xml中, 已经import了这里需要的xml文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                <!--classpath:conf/spring-mvc.xml-->
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

 

在application.xml文件被, 我引入了spring-mvc.xml文件, 这样 在web.xml
文件被, 配置 DispatcherServlet 的时刻, 就好用xml文件路径配置也空.
否则, 程序会按照一定的平整去摸索xml文件. 如果找不至, 则会抛来异常.

以application.xml文件中, 我引入了spring-mvc.xml文件, 这样 在web.xml
文件中, 配置 DispatcherServlet 的时节, 就得拿xml文件路径配置为空.
否则, 程序会按照一定的平整去搜寻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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
       default-lazy-init="true">

    <!-- 自动搜索bean -->
    <context:annotation-config />

    <!-- 开启注解 -->
    <mvc:annotation-driven/>

    <context:component-scan base-package="org.elvin.ssm" />

    <import resource="classpath:conf/spring-mvc.xml" />

   <!--在进行mybatis的整合的时候, 这句话要注释回去-->
    <!--<import resource="classpath:conf/spring-mybatis.xml"/>-->

</beans>
<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
       default-lazy-init="true">

    <!-- 自动搜索bean -->
    <context:annotation-config />

    <!-- 开启注解 -->
    <mvc:annotation-driven/>

    <context:component-scan base-package="org.elvin.ssm" />

    <import resource="classpath:conf/spring-mvc.xml" />

   <!--在进行mybatis的整合的时候, 这句话要注释回去-->
    <!--<import resource="classpath:conf/spring-mybatis.xml"/>-->

</beans>

 

 

三. springmvc配置文件: spring-mvc.xml

三. springmvc配置文件: spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="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.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 注解扫描包 -->
    <context:component-scan base-package="org.elvin.ssm.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 开启注解 -->
    <!--<mvc:annotation-driven/>-->

    <!--静态资源访问-->
    <mvc:default-servlet-handler />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 避免IE在ajax请求时,返回json出现下载 -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>


</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="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.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 注解扫描包 -->
    <context:component-scan base-package="org.elvin.ssm.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 开启注解 -->
    <!--<mvc:annotation-driven/>-->

    <!--静态资源访问-->
    <mvc:default-servlet-handler />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 避免IE在ajax请求时,返回json出现下载 -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>


</beans>

 

 

部署文件就少到此了, 接下来看一下java代码

布置文件就临时到这边了, 接下来看一下java代码

四. java代码

四. java代码

以controller文件夹下, 加入一个控制器: HelloController.java

每当controller文件夹下, 加入一个控制器: HelloController.java

package org.elvin.ssm.controller;

import org.elvin.ssm.pojo.*;
import org.elvin.ssm.utils.CollectionUtilsLocal;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * author: Elvin
 * datetime: 2017/11/29 20:06
 * description:
 */
@Controller
@RequestMapping("hello")
public class HelloController {

    @GetMapping("index")
    public String index(ModelMap model){
        List<Book> bookList = getBookList();
        model.put("bookList",bookList);
        return "index";
    }

    @RequestMapping(value = "book", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Book book(@RequestParam("id") Integer id){
        List<Book> bookList = getBookList();
        Book b = CollectionUtilsLocal.findOneBy(bookList, n -> n.getId().equals(id));
        return b;
    }

    //region private method
    private List<Book> getBookList(){
        List<Book> bookList = new ArrayList<>();
        String[] nameStrs = {"吴", "一", "雪", "动", "额", "阿", "前", "里", "排"};
        Random r = new Random();
        String timeStr = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 10 ; i++){
            Book b = new Book();
            b.setId(i+1);
            b.setName(nameStrs[r.nextInt(5)] + nameStrs[r.nextInt(9)]);
            b.setPublishTime(timeStr);
            b.setPrice(r.nextInt(100));
            bookList.add(b);
        }
        return bookList;
    }
    //endregion
}
package org.elvin.ssm.controller;

import org.elvin.ssm.pojo.*;
import org.elvin.ssm.utils.CollectionUtilsLocal;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * author: Elvin
 * datetime: 2017/11/29 20:06
 * description:
 */
@Controller
@RequestMapping("hello")
public class HelloController {

    @GetMapping("index")
    public String index(ModelMap model){
        List<Book> bookList = getBookList();
        model.put("bookList",bookList);
        return "index";
    }

    @RequestMapping(value = "book", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Book book(@RequestParam("id") Integer id){
        List<Book> bookList = getBookList();
        Book b = CollectionUtilsLocal.findOneBy(bookList, n -> n.getId().equals(id));
        return b;
    }

    //region private method
    private List<Book> getBookList(){
        List<Book> bookList = new ArrayList<>();
        String[] nameStrs = {"吴", "一", "雪", "动", "额", "阿", "前", "里", "排"};
        Random r = new Random();
        String timeStr = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 10 ; i++){
            Book b = new Book();
            b.setId(i+1);
            b.setName(nameStrs[r.nextInt(5)] + nameStrs[r.nextInt(9)]);
            b.setPublishTime(timeStr);
            b.setPrice(r.nextInt(100));
            bookList.add(b);
        }
        return bookList;
    }
    //endregion
}

此间有个援类似, 顺手也贴出来, 期望达到的目的就是是, 拷贝下来,
就能够直接走起, 不需调剂

此间有个帮扶类似, 顺手也贴出, 期望达到的目的就是, 拷贝下来,
就会直接飞起, 不待调剂

CollectionUtilsLocal.java

package org.elvin.ssm.utils;

import java.util.*;
import java.util.function.Predicate;

/**
 * author: Elvin
 * Date: 2017/11/30 9:33
 * Description:
 */
public class CollectionUtilsLocal {
    /**
     * 功能描述:list去重
     **/
    public static void removeDuplicateWithOrder(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element)){
                newList.add(element);
            }
        }
        list.clear();
        list.addAll(newList);
    }

    /**
     * 判断list是否为空,包括对象为null,内容为空
     *
     * @param list
     * @return true 代表不为空,false代表为空*/
    @SuppressWarnings("rawtypes")
    public static boolean isNotNullList(List list) {
        if (null != list && (!list.isEmpty())) {
            return true;
        }
        return false;
    }

    /**
     * 判断集合是否是null或者空集合
     * @param list
     * @return
     */
    public static boolean isNullOrEmptyList(List list){
        if(list == null || list.isEmpty()){
            return true;
        }
        return false;
    }

    /**
     * @param map
     * @return true 代表不为空,false代表为空
     * @Title: isNotNullMap
     * @Description: 校验map是否为空
     * @Since: 2017年6月12日下午1:15:31
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNotNullMap(Map map) {
        if (null != map && !map.isEmpty()) {
            return true;
        }
        return false;
    }


    /**
     * 根据条件删除集合中的元素
     * @param list 待删除元素的集合
     * @param predicate 条件
     * @param <T>
     * @return
     */
    public static <T> List<T> removeAll(List<T> list, Predicate<T> predicate){
        if (!isNotNullList(list)) {
            return null;
        }
        List<T> resList = new ArrayList<>();
        for (T item : list) {
            if (!predicate.test(item)) {
                resList.add(item);
            }
        }
        return resList;
    }

    /**
     * 根据查询条件查找满足条件的数据,并返回
     *
     * @param list      要查找的集合
     * @param predicate 查找的条件
     * @param <T>
     * @return
     */
    public static <T> T findOneBy(List<T> list, Predicate<T> predicate) {
        if (!isNotNullList(list)) {
            return null;
        }
        for (T item : list) {
            if (predicate.test(item)) {
                return item;
            }
        }
        return null;
    }

    /**
     * 根据条件查找集合中所有的数据
     * @param list 待查找集合
     * @param predicate 条件
     * @param <T>
     * @return
     */
    public static <T> List<T> findAllBy(List<T> list, Predicate<T> predicate) {
        if (!isNotNullList(list)) {
            return null;
        }
        List<T> resList = new ArrayList<>();
        for (T item : list) {
            if (predicate.test(item)) {
                resList.add(item);
            }
        }
        return resList;
    }
}
CollectionUtilsLocal.java

package org.elvin.ssm.utils;

import java.util.*;
import java.util.function.Predicate;

/**
 * author: Elvin
 * Date: 2017/11/30 9:33
 * Description:
 */
public class CollectionUtilsLocal {
    /**
     * 功能描述:list去重
     **/
    public static void removeDuplicateWithOrder(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element)){
                newList.add(element);
            }
        }
        list.clear();
        list.addAll(newList);
    }

    /**
     * 判断list是否为空,包括对象为null,内容为空
     *
     * @param list
     * @return true 代表不为空,false代表为空*/
    @SuppressWarnings("rawtypes")
    public static boolean isNotNullList(List list) {
        if (null != list && (!list.isEmpty())) {
            return true;
        }
        return false;
    }

    /**
     * 判断集合是否是null或者空集合
     * @param list
     * @return
     */
    public static boolean isNullOrEmptyList(List list){
        if(list == null || list.isEmpty()){
            return true;
        }
        return false;
    }

    /**
     * @param map
     * @return true 代表不为空,false代表为空
     * @Title: isNotNullMap
     * @Description: 校验map是否为空
     * @Since: 2017年6月12日下午1:15:31
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNotNullMap(Map map) {
        if (null != map && !map.isEmpty()) {
            return true;
        }
        return false;
    }


    /**
     * 根据条件删除集合中的元素
     * @param list 待删除元素的集合
     * @param predicate 条件
     * @param <T>
     * @return
     */
    public static <T> List<T> removeAll(List<T> list, Predicate<T> predicate){
        if (!isNotNullList(list)) {
            return null;
        }
        List<T> resList = new ArrayList<>();
        for (T item : list) {
            if (!predicate.test(item)) {
                resList.add(item);
            }
        }
        return resList;
    }

    /**
     * 根据查询条件查找满足条件的数据,并返回
     *
     * @param list      要查找的集合
     * @param predicate 查找的条件
     * @param <T>
     * @return
     */
    public static <T> T findOneBy(List<T> list, Predicate<T> predicate) {
        if (!isNotNullList(list)) {
            return null;
        }
        for (T item : list) {
            if (predicate.test(item)) {
                return item;
            }
        }
        return null;
    }

    /**
     * 根据条件查找集合中所有的数据
     * @param list 待查找集合
     * @param predicate 条件
     * @param <T>
     * @return
     */
    public static <T> List<T> findAllBy(List<T> list, Predicate<T> predicate) {
        if (!isNotNullList(list)) {
            return null;
        }
        List<T> resList = new ArrayList<>();
        for (T item : list) {
            if (predicate.test(item)) {
                resList.add(item);
            }
        }
        return resList;
    }
}

 

 

以pojo文件夹下, 加入一个实体文件: Book.java

在pojo文件夹下, 加入一个实体文件: Book.java

package org.elvin.ssm.pojo;

/**
 * author: Elvin
 * datetime: 2017/11/29 20:46
 * description:
 */
public class Book {

    private Integer id;

    private String name;

    private Integer price;

    private String publishTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(String publishTime) {
        this.publishTime = publishTime;
    }
}
package org.elvin.ssm.pojo;

/**
 * author: Elvin
 * datetime: 2017/11/29 20:46
 * description:
 */
public class Book {

    private Integer id;

    private String name;

    private Integer price;

    private String publishTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(String publishTime) {
        this.publishTime = publishTime;
    }
}

 五. 前台代码

 五. 前台代码

当jsp文件夹下入一个index.jsp文件

每当jsp文件夹下参加一个index.jsp文件

<%--
  Created by IntelliJ IDEA.
  User: Elvin
  Date: 2017/11/30
  Time: 9:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>ssm</h1>
<table>
    <thead>
        <tr>
            <td>id</td>
            <td>name</td>
            <td>price</td>
            <td>publishTime</td>
        </tr>
    </thead>
    <tbody>
        <c:if test="${bookList!=null && bookList.size()>0}">
            <c:forEach var="item" items="${bookList}">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.price}</td>
                    <td>${item.publishTime}</td>
                </tr>
            </c:forEach>
        </c:if>
    </tbody>
</table>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Elvin
  Date: 2017/11/30
  Time: 9:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>ssm</h1>
<table>
    <thead>
        <tr>
            <td>id</td>
            <td>name</td>
            <td>price</td>
            <td>publishTime</td>
        </tr>
    </thead>
    <tbody>
        <c:if test="${bookList!=null && bookList.size()>0}">
            <c:forEach var="item" items="${bookList}">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.price}</td>
                    <td>${item.publishTime}</td>
                </tr>
            </c:forEach>
        </c:if>
    </tbody>
</table>
</body>
</html>

六. 结果显示

六. 结果显示

  1. 访问页面:
  1. 走访页面:

图片 3

图片 4

 

 

  1. 走访数
  1. 做客数

图片 5

图片 6

使想叫springmvc直接返回数据, 要小心几碰

一经想为springmvc直接回数据, 要专注几接触

  1. 要引入 jackson 的jar包, springmvc没有提object to json 的功能

  2. 每当springmvc的布局文件被, 要丰富  <mvc:annotation-driven/> 

  3. 于controller控制器的action方法方面长  @ResponseBody 注解

  1. 要引入 jackson 的jar包, springmvc没有提object to json 的功能

  2. 以springmvc的安排文件中, 要添加  <mvc:annotation-driven/> 

  3. 于controller控制器的action方法方面长  @ResponseBody 注解

 

 

  1. 做客静态资源
  1. 访静态资源

图片 7

图片 8

想念只要访问静态资源, 需要丰富  <mvc:default-servlet-handler /> ,
或者自己配置有资源, 否则是匪可知顾到的.

思如果访问静态资源, 需要添加  <mvc:default-servlet-handler /> ,
或者好布置部分资源, 否则是免克看到之.

 

 

相关文章