Spring 、Mybatis、Spring MVC整合

xiaoxiao2021-02-28  6

整合思路:

1.Dao层 使用Mybatis框架。创建SqlMapConfig.xml 创建一个applicationContext-dao.xml 1)配置数据源 2)需要让spring容器管理sqlSessionFactory,单例存在 3)把mapper代理对象放到spring容器中。使用扫描包方式加载mapper代理对象 2.Service层 1)事务管理 2)需要把service实现类对象放到spring容器中进行管理 3.表现层层 1)配置注解驱动 2)配置视图解析器 3)需要扫描Controller 4.Web.xml 1)spring容器配置 2)Spring MVC前端控制器的配置 3)Post乱码过滤器

1.pom.xml

<!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!--spring AOP--> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</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>1.2.2</version> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>0.2.15</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end -->

2.jdbc.properties

<!--oracle--> jdbc_driverClassName=oracle.jdbc.driver.OracleDriver jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl jdbc_username=用户名 jdbc_password=密码 <!--mysql--> jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://172.18.7.250:3306/testlinksaptac?useUnicode=yes&characterEncoding=UTF8 jdbc_username=用户名 jdbc_password=密码

3.web.xml配置

<!-- 应用名称 --> <display-name></display-name> <!-- 应用描述 --> <description></description> <!-- 应用范围内的初始化参数 --> <!-- 加载spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param> <!-- ContextLoaderListener的作用就是启动Web容器时,自动装配application.xml的配置信息。详情见【3】 --> <listener> <description>spring监听器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 自定义编码拦截器 --> <filter> <filter-name>setcharacter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- request 编码 --> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <!-- response 编码 --> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>setcharacter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring MVC前端控制器 --> <servlet> <description>spring mvc servlet</description> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过contextConfigLocation来配置springmvc加载的配置文件(处理器映射器、处理器适配器等等) --> <!-- 如果不配置 contextConfigLocation 默认加载的是/WEB-INF/servler名称-servlet.xml(springmvc-servlet.xml)--> <init-param> <description>spring mvc 配置文件</description> <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> <!-- 第一种 *.action:访问以.action结尾的由DispatcherServlet进行解析 --> <!-- 第二种/ :所有由DispatcherServlet进行解析 需要配置静态文件不被解析 使用此种可以实现restful分割的url --> <!-- 第三种/* :这样配置不对,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址 ,不能根据jsp页面找到handler 会报错--> <url-pattern>/</url-pattern> </servlet-mapping> <!--指定显示的默认文件--> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!--请求返回码为400 返回的jsp页面--> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <!--请求返回码为500 返回的jsp页面--> <error-page> <error-code>500</error-code> <location>/xxx/xxx</location> </error-page> <!-- 日志记录 --> <context-param> <!-- 日志配置文件路径 --> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <!-- 日志页面的刷新间隔 --> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>

4.加载spring的配置文件application.xml(web容器启动时,自动装配application中的配置信息)

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 使用@Autowired注解 --> <!-- <context:annotation-config/> --> <context:component-scan base-package="com.person"></context:component-scan> <!-- 导入要加载的spring配置--> <!-- 配置数据源 【5】 --> <import resource="classpath:spring-dataSource.xml"/> <!--mybatis 配置--> <import resource="classpath:spring-mybatis.xml"/> <!-- 事务管理器 --> <import resource="classpath:spring-transaction.xml"/> </beans>

5.数据源配置spring-dataSource.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <!-- 加载配置 --> <!-- 加载jdbc.properties文件中的内容 中的key要有一定的规则 --> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/> <!-- 创建数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="${pool.maxPoolSize}"/> <!-- 超过时间限制是否回收 --> <property name="removeAbandoned" value="true"/> <!-- 超时时间;单位为秒。180秒=3分钟 --> <property name="removeAbandonedTimeout" value="${pool.removeAbandonedTimeout}"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${pool.maxWait}"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${pool.timeBetweenEvictionRunsMillis}"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${pool.minEvictableIdleTimeMillis}"/> <property name="validationQuery" value="${pool.validationQuery} "/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> </bean> </beans>

6.mybatis配置信息:spring-mybatis.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis全局配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property> <!--加载映射文件--> <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"></property> </bean> <!-- 配置mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.person.dao"/> </bean> <!-- 配置事务管理器 --> <!-- 对mybatis操作数据库事务控制 spring使用jdbc的事务控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- dataSource在 spring-dataSource.xml中配置了--> <property name="dataSource" ref="dataSource"/> </bean> </beans>

7.事务管理 spring-transaction.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" default-autowire="byName" default-lazy-init="true" > <!-- Spring的声明式事务管理 --> <!-- 通知 --> <!-- transaction-manager 表示把这通知给谁 transactionManager就是上面的spring-mybatis.xml事务管理器的id--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 与事务属性关联的方法名 propagation:D 事务传播行为 rollback-for:Exception异常时被触发回滚--> <!-- 传播行为 --> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="set*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="change*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="cancel*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="confirm*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 2.配置事物切入点,以及把事物切入点和事物属性关联起来 --> <!-- aop 通知是有aop来调用 --> <aop:config> <!-- pointcut 表示aop的切点 --> <!-- execution(* execution(* com.person.service.*.*(..)) 表示要切com.person.service.impl这个包下面的所有类的所有方法 --> <aop:pointcut id="txPointCut" expression="execution(* com.person.service.*.*(..))"/> <aop:advisor pointcut-ref="txPointCut" advice-ref="txAdvice"/> </aop:config> </beans>

8.整合spring MVC:spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 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"> <!--Spring组件扫描器注解的处理器可以单个配置,但是我们可以使用spring框架的组件扫描的方式来配置base-package:需要扫描哪个包下面的处理器--> <!--开启注解,解析Controller--> <mvc:annotation-driven /> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.person.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:annotation-driven> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 注册一个适配器 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- JSON转换器 --> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans>

9.mybatis全局配置文件:sqlMapConfig.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> <settings> <!-- 全局范围内启用或禁用缓存配置任何 --> <setting name="cacheEnabled" value="true"/> <!-- 是否开启延迟加载 --> <setting name="lazyLoadingEnabled" value="false"/> <!-- 允许或不允许一个单独的语句返回多个结果集 --> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="defaultExecutorType" value="REUSE"/> <setting name="defaultStatementTimeout" value="25000"/> </settings> </configuration>
转载请注明原文地址: https://www.6miu.com/read-1950273.html

最新回复(0)