欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSM框架整合

程序员文章站 2022-03-27 10:21:34
...

所需要的配置文件及相关操作流程

 

1、导入所需jar包

<properties>
      <spring.version>4.1.6.RELEASE</spring.version>
      <mysql.version>5.1.6</mysql.version>
      <druid.version>1.0.29</druid.version>
      <mybatis.version>3.4.1</mybatis.version>
      <mybatis-spring.version>1.3.1</mybatis-spring.version>
      <lombok.version>1.16.18</lombok.version>
      <junit.version>4.12</junit.version>
      <jstl.version>1.2</jstl.version>
      <common.version>4.4.0</common.version>
      <json.version>1.2.47</json.version>
  </properties>
  <dependencies>
 
  <!-- spring框架 -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
      </dependency>
      
      <!-- springMVC框架 -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
      </dependency>
      
      <!-- 将mybatis和spring建立对象关系映射 -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${spring.version}</version>
      </dependency>
      
      <!-- 使用spring test -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
      </dependency>
      
      <!-- mysql -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>${mysql.version}</version>
      </dependency>
      
      <!-- 数据库连接池 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>${druid.version}</version>
      </dependency>
      
      <!-- mybatis框架 -->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${mybatis.version}</version>
      </dependency>
      
      <!-- 将mybatis集成到spring -->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>${mybatis-spring.version}</version>
      </dependency>
      
      <!-- 自动生成getter和setter方法 -->
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>${lombok.version}</version>
          <scope>provided</scope>
      </dependency>
      
      <!-- junit测试 -->
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
      </dependency>
      
      <!-- jsp页面的jstl -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>${jstl.version}</version>
      </dependency>
      
      <!-- 在控制台查看sql语句 -->
      <dependency>
          <groupId>org.duracloud</groupId>
          <artifactId>common</artifactId>
          <version>${common.version}</version>
      </dependency>
      <!-- 用于前后分离 -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${json.version}</version>
       </dependency>
  </dependencies>

2、配置spring框架

在src/main/resources新建applicationContext.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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:util="http://www.springframework.org/schema/util"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
      <!-- 扫描service包 -->
      <context:component-scan base-package="com.nut.service"></context:component-scan>
      
      <!-- 数据库连接池 -->
      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/db1?useSSL=false"></property>
            <property name="username" value="root"></property>
            <property name="password" value="clhjg"></property>
      </bean>
      
      <!-- mybatis session工厂 -->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="typeAliasesPackage" value="com.nut.pojo"></property>
      </bean>
      
      <!-- 扫描mapper所生成的代理对象 -->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.nut.dao"></property>
      </bean>
</beans>

3、配置mybatis

在src/main/java/com/nut/dao文件夹下,实现pojo的dao层接口类。

    在src/main/resources/com/nut/dao文件夹下,实现对应的mybatis的配置文件

 

注意:

  1. src/mian/java和src/mian/resources在编译时属于一个文件夹,是在同一个根目录下

  2. 接口类和对用的xml文件,是需要在同一个目录下。即在src/mian/java和src/mian/resources两个文件夹下需要建立相同的目录com/nut/dao

  3. 接口类和xml文件,除了后缀名不一样外,其余都一样

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace为接口的完全限定名 -->
<mapper namespace="com.nut.dao.GoodDao">
      <!-- 结果集的映射,如果数据库表的字段名和类的属性名不同,则可采用下面的方法 -->
      <!-- type 是要映射的类,id自己起 -->
      <!-- property为类的名,注意和pojo中的类的属性名一致, column为数据库的名 -->
      <resultMap type="Good" id="GoodMap">
            <result property="id" column="id" />
            <result property="name" column="name" />
            <result property="color" column="color" />
            <result property="price" column="price" />
      </resultMap>
      <!-- 标签的id必须为方法名 -->
      <!-- 如果在上面使用了结果的映射,则使用 resultMap来进行 resultMap="GoodMap" -->
      <!-- 如果不使用映射,则使用 resultType 来进行resultType="Good" -->
      <!-- #{} 去掉get,第一个字母变小写 -->
      <!-- ${} 不预编译,会被 sql注入。 使用时需要加 单引号 -->
      <select id="selectAll" resultType="Good">
            select
            id,name,color,price from goods
            <if test="isShow==1">
                  limit #{start},#{pageSize}
            </if>
      </select>
</mapper>

4、配置springMVC

在src/main/webapp/WEB-INF文件夹下, 新建 web.xml

注意:此处不仅配置了springMVC,同时还有request的编码过滤器,启动spring上下文,ajax跨域。

<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0">
      <!-- 服务器启动的时候加载spring上下文 -->
      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 解决request中文乱码(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>
            <init-param>
                  <param-name>forceEncoding</param-name>
                  <param-value>true</param-value>
            </init-param>
      </filter>
      <filter-mapping>
            <filter-name>encodingfilter</filter-name>
            <url-pattern>*</url-pattern>
      </filter-mapping>
      <!-- 解决ajax跨域 -->
      <filter>
            <filter-name>CorsFilter</filter-name>
            <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
      </filter>
      <filter-mapping>
            <filter-name>CorsFilter</filter-name>
            <url-pattern>/*</url-pattern>
      </filter-mapping>
      <!-- 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/springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
      </servlet-mapping>
</web-app>

接着在src/main/sources/springMVC 文件夹下新建 springmvc.xml文件

注意:springmvc.xml文件的文件名需要和上面web.xml中<servlet> <init-param> <param-value>标签值对应

<?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
      <!--开启注解启动 -->
      <mvc:annotation-driven></mvc:annotation-driven>
      <!-- 扫描controller所在包 -->
      <context:component-scan base-package="com.nut.controller"></context:component-scan>
      <!-- 配置视图解析器 -->
      <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/WEB-INF/pages/"></property>
            <property name="suffix" value=".jsp"></property>
      </bean>
</beans>

相关配置文件参数讲解

1、pom.xml

SSM框架整合

2、applicationContext.xml

SSM框架整合

3、mybatis的xml文件

SSM框架整合

SSM框架整合

SSM框架整合

 

函数传值的三种方式

1、如果所传值均为基本数据类型,可直接传递,在xml文件使用 #{0}  #{1}...  依次类推的形式调用,调用顺序即为在参数列表中的顺序

示例:

SSM框架整合

SSM框架整合

2、直接传递一个封装好的对象,无论是Map,还是普通的Object,或者是自己写的对象。

示例:

SSM框架整合

SSM框架整合

 在传一个自己写的对象的时候,#{} 里面的值时类的属性值

SSM框架整合

3、使用注解传值

SSM框架整合

SSM框架整合

 

注意:在使用第二种和第三种时,在if的判断条件中

test="#{变量名 判断条件}"

上面两个都要写在括号内

 

4、web.xml

SSM框架整合SSM框架整合

5、 springmvc.xml

SSM框架整合

 

相关标签: SSM