Spring+Spring MVC+myBatis整合详细步骤

来源:互联网 发布:东方财富炒股软件 编辑:程序博客网 时间:2024/04/30 18:13

第一步:加入jar包

第二步:编写数据库的外部properties文件

jdbc_url=jdbc:mysql://localhost:3306/supermarket

jdbc_username=root

jdbc_driver=com.mysql.jdbc.Driver

jdbc_password=1234

第三步:编写spring的配置文件

1).包扫描的配置

注意:SpringMVCSpring配置文件中都需要有包扫描的配置,在实际开发中,可以会按照功能模块来划分包,所以这里最好使用扫描某些注解的方式,当然也可以根据实际情况指定要扫描的包。如果采用扫描注解的方式,那么只需要指定SpringMVC扫描@Controller@ControllerAdvice注解,Spring扫描除了@Controller@ControllerAdvice以外的注解即可

2).数据库外部配置文件的路径配置

3).配置数据源

4).配置bean,bean的class=org.mybatis.spring.SqlSessionFactoryBean,该bean的属性包括:dataSource(数据源)typeAliasesPackage(需要起别名的包的包名,一般是entity所在的包名)configLocation(mybatis配置文件所在的路径)mapperLocations(mapper.xml所在的路径,是一个数组)

5).mapper文件的批量扫描配置,需要配置一个bean,class=org.mybatis.spring.mapper.MapperScannerConfigurer,bean属性包括basePackage(XXXmapper.xml所在的包的名称,如果扫描多个包,每个包名之间使用半角逗号分隔)sqlSessionFactoryBeanName注意sqlSessionFactoryBeanNamevalue值即是上面配置的bean的SqlSessionFactoryBean的id值

6).配置Spring的事务管理

7).启用Spring的事务注解

 

 

 

<?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:tx="http://www.springframework.org/schema/tx"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<!--包扫描的方式:spring扫描除了@Controller@ControllerAdvice以外的注解  -->

<context:component-scan base-package="com.shxt.supermarket">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>

</context:component-scan>

<!-- 数据库外部配置文件的路径配置 -->

<context:property-placeholder location="classpath:properties//db.properties"/>

<!-- 配置数据源 -->

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

<property name="username" value="${jdbc_username}"></property>

<property name="password" value="${jdbc_password}"></property>

<property name="url" value="${jdbc_url}"></property>

<property name="driverClassName" value="${jdbc_driver}"></property>

</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="typeAliasesPackage" value="com.shxt.supermarket.entity"></property>

<!-- 加载mybatis的配置文件 ,一般情况下不再需要-->

<!-- <property name="configLocation" value="classpath:mybatis-config.xml"></property> -->

<!-- 通过下面的方式也可以配置mybatismapper文件所在的路径 ,一般情况下mapper.java接口和mapper.xml不在同一个目录下,

可以在mapperLocations属性中配置mapper.xml所在的路径,可以使用通配符;不过一般建议将mapper.java接口类名和mapper.xml映射文件名称保持一致,且在同一个目录中-->

<!-- <property name="mapperLocations">

<array>

<value>路径一</value>

<value>路径二</value>

<value>路径三</value>

</array>

</property> -->

</bean>

<!-- mapper的批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册

需要遵循的规范:将mapper.java接口类名和mapper.xml映射文件名称保持一致,且在同一个目录中

自动扫描出来的mapper的bean的id为mapper类名(首字母小写)

-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 指定扫描的包名  如果扫描多个包,每个包名之间使用半角逗号分隔-->

<property name="basePackage" value="com.shxt.supermarket.mapper"></property>

<!-- 需要注意的是:sqlSessionFactoryBeanName的value值即是上面配置的bean的SqlSessionFactoryBean的id值 -->

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

</bean>

<!--配置Spring的事务管理 -->

<bean id="transactionManager" 

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 启用事务注解 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

 

 

第四步:编写SpringMVC的配置文件

1).包扫描的配置,实际上SpringMVC只需要扫描@Controller@ControllerAdvice注解即可

2).配置视图解析器的bean,class=org.springframework.web.servlet.view.InternalResourceViewResolver,bean包含的属性包括:prefix(指定要返回的视图的前缀)suffix(指定要返回的视图的后缀)

4).配置<mvc:default-servlet-handler/>

  <mvc:annotation-driven></mvc:annotation-driven>

 

具体配置如下:

 

 

 

<?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.xsd">

     <!-- 使用默认的handler -->

<mvc:default-servlet-handler/>

<!-- 在实际开发中通常都配上mvc:annotation-driven标签 -->

<mvc:annotation-driven></mvc:annotation-driven>

<!-- 包扫描的配置 -->

<context:component-scan base-package="com.shxt.supermarket" use-default-filters="false">

<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>        

<!-- 配置视图解析器,把handler方法的返回值解析为实际的物理视图 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<!-- 文件上传相关:配置 MultipartResolver -->

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="defaultEncoding" value="UTF-8"></property>

<!-- <property name="maxUploadSize" value="1024000"></property> -->

</bean>

<mvc:interceptors>

<!-- 配置自定义的登录拦截器 -->

<bean class="com.shxt.supermarket.interceptors.LoginInterceptor">

<property name="urls">

<array>

<value>/css</value>

<value>/js</value>

<value>/images</value>

<value>/login</value>

<value>/fonts</value>

</array>

</property>

</bean>

</mvc:interceptors>

<!-- 配置异常处理器 -->

<bean class="org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver">

</bean>

<!-- 配置直接转发的页面,这样就可以直接响应转发的页面而无需再经过handler的方法 -->

<mvc:view-controller path="/checkOut" view-name="checkOut"/>

</beans>

 

第五步:配置web.xml

1).配置启动Spring IOC容器的Listener

2).配置SpringMVCDispatcherServlet

3).配置SpringMVCHiddenHttpMethodFilter,SpringMVCPOST请求转换为PUT或者DELETE请求(这一步视具体需要而配置)

4).配置字符过滤器

 

具体配置如下:

 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

metadata-complete="true" version="3.1">

<!-- 配置启动 Spring IOC容器的 Listener -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--配置DispatcherServlet  -->

<servlet>

<servlet-name>dispatcherServlet</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>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>

<url-pattern>/</url-pattern><!-- / 表示可以应答所有请求 -->

</servlet-mapping>

<!-- 配置HiddenHttpMethodFilterpost请求转换为put或者delete请求-->

<filter>

<filter-name>HiddenHttpMethodFilter</filter-name>

<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>HiddenHttpMethodFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 配置字符过滤器,将所有请求的中文字符用UTF-8字符集表示-->

<filter>

<filter-name>characterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<!-- 配置字符过滤器类CharacterEncodingFilter的初始化参数-->

<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>

<!-- 配置欢迎文件 -->

<welcome-file-list>

<welcome-file>login.jsp</welcome-file>

</welcome-file-list>

</web-app>

 

注:实际上按照上面的配置,mybatis的配置文件就不再需要了,如果想使用mybatis的配置文件,可以在mybatis的配置文件中配置别名、mapper、二级缓存相关的内容,然后在spring的配置文件中的将mybatis的配置文件路径配置一下就可以了,具体如何配置参见spring的配置文件的说明。

0 0
原创粉丝点击