Spring + SpringMVC + MyBatis整合

ssm环境搭建初体验

初次体验框架的整合,做个总结吧。很多的错误缺点,很多的问题,一个一个的整理下。

当我们在做一个不管是什么框架的环境搭建时,切记,很多时候都是自己打败了自己,细心一点体验其中有意思的点滴。一个环境搭建的最重要的就是细心了,废话不多说,快看一下怎么整合,破不急待了。

首先我们要说的就是jar没错,这一块的话,建议使用maven这样可以省很多事情,但是这一块我真的不想多说,自己去百度一个最全的ssm整合jar依赖吧。

那么下面就应该是spring的基础环境搭建,

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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
    <!-- 注册数据源文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
     
     <!-- 配置数据源 -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     	<property name="driverClassName" value="${jdbc.driver}"></property>
     	<property name="url" value="${jdbc.url}"></property>
     	<property name="username" value="${jdbc.username}"></property>
     	<property name="password" value="${jdbc.password}"></property>
     </bean>
     
     <!-- 创建 sqlsessionFactory  -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描 mapping.xml 文件 -->
		<!-- <property name="mapperLocations" value="classpath*:cn/shaojie/dao/*.xml"></property> -->
		<!-- 引入 mybatis-config.xml 配置文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	</bean>
     
     <!-- 创建 mybatis 的  MapperScannerConfig 映射接口 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="cn.shaojie.dao"></property>
     	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>
     
    <!-- 配置 spring 的注解扫描 -->
	<context:component-scan base-package="cn.shaojie.service"/>

	<!-- 配置事物管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>  

    <!-- 配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
     
</beans>

这里应该是比较纠结的吧,这个基础的spring的配置文件中有很多的写法,比如配置数据源,有的人喜欢用c3p0当然,每个人的喜好不同,代码的风格肯定也不一样,没有什么推荐,都可以,你用的来就好,仅此而已。

当我们在创建 sqlsessionFactory 时,一般来说,最简配置就是引入mybatis-config.xml 配置文件 (自我感觉)当然,这样的环境搭建也有个小缺点,算是不成文的规定吧,这里的*mapper.xml要求与*dao的开头一致。然后扫描的功能什么的就交给mybatis去实现吧。

mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 取别名 -->
	<typeAliases>
		<package name="cn.shaojie.model"/>
	</typeAliases>
    <!-- 扫描映射文件 -->
	<mappers>
		<package name="cn.shaojie.dao"/>
	</mappers>
</configuration>

配置 spring 的注解扫描就交给spring去做吧,尤其是service层,单独分开,因为这里是不能和springmvc整合在一块,让自己专注做自己擅长的事情,让springmvc专注于控制controller层。

重点:springspringmvc的扫描不能整合在一起,需要单独分开
spring-mvc.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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!-- 引入Spring文件 
	              让web容器加载SpringMVC文件时,也加载Spring文件
	-->
	<import resource="applicationContext.xml"/>
	
	<!-- 使用注解方式 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 扫描器 -->
    <context:component-scan base-package="cn.shaojie.controller"/>
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/static/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
    <!-- 静态资源文件   
                         整合到一个大的文件夹下,统一成一个路径
         location 相对的路径,本地静态资源文件所在的目录
         mapping  相对的位置,将静态资源映射到指定的路径下
    -->
    <mvc:default-servlet-handler/>
   	<!-- 
    <mvc:resources location="/resources/**" mapping="/resources/"/>
     -->
     
     <!-- 处理请求返回json字符串的中文乱码问题 -->
     <!-- 
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
     -->
</beans>	

springmvc其实不需要做太多,只需要引入spring的配置文件就好,然后扫描一下controller层就没了,刚开始搭建时,我建议一切从简,能省掉的全都不要了,让项目能跑起来再说,就这么简单。

视图解析器可有可无,无非就是不配置视图解析器,代码写的比较多点,前期只是搭建环境测试不会影响。

springmvc最简配置
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	
	<!-- 引入Spring文件 
	              让web容器加载SpringMVC文件时,也加载Spring文件
	-->
	<import resource="applicationContext.xml"/>
	
	<!-- 使用注解方式 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 扫描器 -->
    <context:component-scan base-package="cn.shaojie.controller"/>
	
	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/static/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
    
</beans>	

整体都配置好了,就是web.xml配置了,这里就没有什么说的了,一般来说,我们需要配置五项:

spring的监听spring的配置文件springmvc的配置文件springmvc的中央调度器以及springmvc关于字符编码的配置,这里我们不需要配置那么多了,直接配置springmvc的中央调度器 但是这里读取的是springmvc的配置文件,因为spring的配置文件我们已经在springmvc的配置文件中引入过了。

web.xml
<?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" 
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		 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>ssm</display-name>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置 spring mvc 中央调度器 --> 
  <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:spring-mvc.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>
  
  <!-- 配置数据传输的编码 -->
  <filter>
  	<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>  	
  </filter-mapping>
   
</web-app>

关于这个编码,如果你想页面传输这个中文,那么这个肯定是要配置的,毫无疑问。是有作用的,有的人说,这个其实无伤大雅,因为springmvc其实在它的底层也对这个字符编码的问题处理过,不过是需要人为去调用的。其实就是看每个人看问题的角度,写上也不会影响,为什么不避免这个问题呢?

以上ssm的环境搭建到此结束。

更为详细说明,请关注个人博客:https://www.lzmvlog.top/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值