飞道的博客

挺详细的spring+springmvc+mybatis配置整合|含源代码

348人阅读  评论(0)

大家好,我是雄雄,今天来带着大家来配置一下SSM(spring+springmvc+

mybatis)框架。

01

新建java web项目

直接在myeclipse中,新建一个web项目即可。

02

导入jar包

将SSM所需的jar包复制到项目的/WebRoot/WEB-INF/lib中,在这里我整理了下,大致需要34个jar文件,复制完之后,选中所有jar包,右击—Build Path-->Add to Build Path。

03

配置web.xml文件。

web.xml文件需要配置三部分信息,spring、springmvc以及解决springmvc传值乱码的过滤器,分别如下:

spring配置信息:


   
  1. <listener>
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3.   </listener>
  4.   <context-param>
  5.     <param-name>contextConfigLocation</param-name>
  6.     <param-value>classpath:applicationContext.xml</param-value>
  7.   </context-param>

springmvc配置信息:


   
  1. <servlet>
  2.     <servlet-name>springmvc</servlet-name>
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4.    <init-param>
  5.      <param-name>contextConfigLocation</param-name>
  6.      <param-value>classpath:springmvc-servlet.xml</param-value>
  7.    </init-param>
  8.    <load-on-startup> 1</load-on-startup>
  9.   </servlet>
  10.   <servlet-mapping>
  11.     <servlet-name>springmvc</servlet-name>
  12.     <url-pattern>/</url-pattern>
  13.   </servlet-mapping>

设置编码格式的过滤器信息:


   
  1. <!-- 解决springmvc传递值乱码问题 -->
  2.   <filter>
  3.     <filter-name>encodingFilter</filter-name>
  4.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  5.     <init-param>
  6.       <param-name>encoding</param-name>
  7.       <param-value>utf -8</param-value>
  8.     </init-param>
  9.     <init-param>
  10.       <param-name>forceEncoding</param-name>
  11.       <param-value> true</param-value>
  12.     </init-param>
  13.   </filter>
  14.   <filter-mapping>
  15.     <filter-name>encodingFilter</filter-name>
  16.     <url-pattern> /*</url-pattern>
  17.   </filter-mapping>

04

配置Spring配置文件

applicationContext.xml文件,里面需要包含这些信息:用来连接数据库的数据源信息


   
  1. <!-- 配置数据源-->
  2.   <bean id= "dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">
  3.     <property name= "driverClassName" value= "com.mysql.jdbc.Driver"></property>
  4.     <property name= "url" value= "jdbc:mysql://localhost:3306/schooldb"></property>
  5.     <property name= "username" value= "root"></property>
  6.     <property name= "password" value= "root"></property>
  7.   </bean>

加载mybatis-config.xml文件以及sql映射文件的SqlSessionFactory:


   
  1. <!--sqlSessionFactory -->
  2.   <bean id= "sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean">
  3.     <property name= "dataSource" ref= "dataSource"></property>
  4.     <property name= "configLocation" value= "classpath:mybatis-config.xml"></property>
  5.     <property name= "mapperLocations">
  6.       <list>
  7.         <value>classpath:org/dao /*.xml</value>
  8.       </list>
  9.     </property>
  10.   </bean>

Mapper注入映射器的MapperScannerConfigurer:


   
  1. <!-- 配置自动映射器 -->
  2.   <bean class= "org.mybatis.spring.mapper.MapperScannerConfigurer">
  3.     <property name= "basePackage" value= "org.dao"></property>
  4.   </bean>

最后就是扫描注解的配置:


   
  1. <!-- 扫描所有注解信息 -->
  2.   <context:component-scan base- package= "org.dao,org.service,org.web"/>
  3.   <mvc:annotation-driven/>

05

配置springmvc的信息

springmvc-servlet.xml中需要包含最基本的两个部分,扫描注解和springmvc请求的前缀后缀设置:


   
  1. <!-- 扫描注解 -->
  2.   <context:component-scan base- package= "org.web,org.dao,org.service"/>
  3.   <mvc:annotation-driven/>
  4.   
  5.   <!-- 配置前缀和后缀 -->
  6.   <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver">
  7.     <property name= "prefix" value= "/"></property>
  8.     <property name= "suffix" value= ".jsp"></property>
  9.   </bean>

06

配置mybatis配置文件

本文件简单点,就配置个别名和打印sql语句就行,都是可选的:


   
  1. <?xml version= "1.0" encoding= "UTF-8" ?>
  2. <!DOCTYPE configuration
  3.   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4.    "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.   
  7.    <settings>
  8.         <!-- 打印sql语句 -->
  9.         <setting name= "logImpl" value= "STDOUT_LOGGING" />
  10.     </settings>
  11.   <!-- 起别名 -->
  12.   <typeAliases>
  13.     < package name= "org.entity"/>
  14.   </typeAliases>
  15.   
  16. </configuration>

附录:

spring配置文件(applicationContext.xml):


   
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <beans
  3.   xmlns= "http://www.springframework.org/schema/beans"
  4.   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  5.   xmlns:p= "http://www.springframework.org/schema/p"
  6.   xmlns:aop= "http://www.springframework.org/schema/aop"
  7.   xmlns:mvc= "http://www.springframework.org/schema/mvc"
  8.   xmlns:context= "http://www.springframework.org/schema/context"
  9.   xsi:schemaLocation= "
  10.   http://www.springframework.org/schema/beans
  11.   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  12.   http://www.springframework.org/schema/p
  13.   http://www.springframework.org/schema/p/spring-p-3.1.xsd
  14.   http://www.springframework.org/schema/aop
  15.   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  16.   http://www.springframework.org/schema/mvc
  17.   http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  18.   http://www.springframework.org/schema/context
  19.   http://www.springframework.org/schema/context/spring-context-3.1.xsd
  20.   ">
  21.   <!-- 配置数据源-->
  22.   <bean id= "dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">
  23.     <property name= "driverClassName" value= "com.mysql.jdbc.Driver"></property>
  24.     <property name= "url" value= "jdbc:mysql://localhost:3306/schooldb"></property>
  25.     <property name= "username" value= "root"></property>
  26.     <property name= "password" value= "root"></property>
  27.   </bean>
  28.   
  29.   <!--sqlSessionFactory -->
  30.   <bean id= "sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean">
  31.     <property name= "dataSource" ref= "dataSource"></property>
  32.     <property name= "configLocation" value= "classpath:mybatis-config.xml"></property>
  33.     <property name= "mapperLocations">
  34.       <list>
  35.         <value>classpath:org/dao /*.xml</value>
  36.       </list>
  37.     </property>
  38.   </bean>
  39.   
  40.   <!-- 配置自动映射器 -->
  41.   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42.     <property name="basePackage" value="org.dao"></property>
  43.   </bean>
  44.   
  45.   <!-- 扫描所有注解信息 -->
  46.   <context:component-scan base-package="org.dao,org.service,org.web"/>
  47.   <mvc:annotation-driven/>
  48. </beans>

springmvc配置文件(springmvc-servlet.xml):


   
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <beans
  3.   xmlns= "http://www.springframework.org/schema/beans"
  4.   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  5.   xmlns:p= "http://www.springframework.org/schema/p"
  6.   xmlns:aop= "http://www.springframework.org/schema/aop"
  7.   xmlns:mvc= "http://www.springframework.org/schema/mvc"
  8.   xmlns:context= "http://www.springframework.org/schema/context"
  9.   xsi:schemaLocation= "
  10.   http://www.springframework.org/schema/beans
  11.   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  12.   http://www.springframework.org/schema/p
  13.   http://www.springframework.org/schema/p/spring-p-3.1.xsd
  14.   http://www.springframework.org/schema/aop
  15.   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  16.   http://www.springframework.org/schema/mvc
  17.   http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  18.   http://www.springframework.org/schema/context
  19.   http://www.springframework.org/schema/context/spring-context-3.1.xsd
  20.   ">
  21.   
  22.   <!-- 扫描注解 -->
  23.   <context:component-scan base- package= "org.web,org.dao,org.service"/>
  24.   <mvc:annotation-driven/>
  25.   
  26.   <!-- 配置前缀和后缀 -->
  27.   <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver">
  28.     <property name= "prefix" value= "/"></property>
  29.     <property name= "suffix" value= ".jsp"></property>
  30.   </bean>
  31. </beans>

web.xml文件:


   
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <web-app version= "3.0" 
  3.   xmlns= "http://java.sun.com/xml/ns/javaee" 
  4.   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" 
  5.   xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee
  6.   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  7.   <display-name></display-name>  
  8.   <welcome-file-list>
  9.     <welcome-file>index.jsp</welcome-file>
  10.   </welcome-file-list>
  11.   
  12.   <!-- springmvc的配置 -->
  13.   <servlet>
  14.     <servlet-name>springmvc</servlet-name>
  15.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16.    <init-param>
  17.      <param-name>contextConfigLocation</param-name>
  18.      <param-value>classpath:springmvc-servlet.xml</param-value>
  19.    </init-param>
  20.    <load-on-startup> 1</load-on-startup>
  21.   </servlet>
  22.   <servlet-mapping>
  23.     <servlet-name>springmvc</servlet-name>
  24.     <url-pattern>/</url-pattern>
  25.   </servlet-mapping>
  26.   
  27.   <!-- spring -->
  28.   <context-param>
  29.     <param-name>contextConfigLocation</param-name>
  30.     <param-value>classpath:applicationContext.xml</param-value>
  31.   </context-param>
  32.   <!-- 监听配置 -->
  33.   <listener>
  34.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  35.   </listener>
  36.   
  37.   <!-- 解决springmvc传递值乱码问题 -->
  38.   <filter>
  39.     <filter-name>encodingFilter</filter-name>
  40.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  41.     <init-param>
  42.       <param-name>encoding</param-name>
  43.       <param-value>utf -8</param-value>
  44.     </init-param>
  45.     <init-param>
  46.       <param-name>forceEncoding</param-name>
  47.       <param-value> true</param-value>
  48.     </init-param>
  49.   </filter>
  50.   <filter-mapping>
  51.     <filter-name>encodingFilter</filter-name>
  52.     <url-pattern> /*</url-pattern>
  53.   </filter-mapping>
  54.   
  55. </web-app>

配置到此结束,明天带着大家实现一遍SSM的增删改查案例。

独家特制纯手工辣椒酱,小商店现在下单,单件商品立减1.88元,满80元减15元.

往期精彩

投资理财要趁早,基金风险是最小!

2021-01-10

java中的泛型类型擦除

2021-01-12

一百馒头一百僧,大僧三个更无争,小僧三人分一个,大小和尚得几丁?

2021-01-09

你们好好的学,回头教教我~

2021-01-08

辣椒酱中奖说明~

2021-01-07

点分享

点点赞

点在看


转载:https://blog.csdn.net/qq_34137397/article/details/112598399
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场