spring mvc mybatis集成踩的坑

时间:2023-12-26 18:32:01

开园这么多年了也没写几篇文章,现在想想光看别人的也不行啊,咱也自己写写,就写这天我我在做spring mvc与mybatis的集成时遇到的问题

1 spring与mybatis的集成

这个相信大家都弄过吧,不过我还是要说说.首先咱们得先把项目大体的架子搭起来,本人用的IDEA,废话不多说来上图

spring mvc mybatis集成踩的坑

这是我的spring和mybatis的项目的结构,这我相信大家都能看明白,不用我多说什么.其实最主要的还是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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.kevin.learn.testspringmybatis.service.*" /> <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<!-- 基本属性 url、user、password -->
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${pwd}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
</bean>   <!--spring与mybatis结合的bean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/kevin/learn/testspringmybatis/sqlmapper/*.xml"/>
</bean>
<!--自动扫描dao接口与mapper文件绑定-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kevin.learn.testspringmybatis.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!--下面是事务了-->
<bean id="transactionManage"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManage">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"
isolation="DEFAULT" />
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="transactionAdvice"
pointcut="execution(* com.kevin.springmvc.service.*.*(..))" />
</aop:config> </beans>

这配置很简单,但正是由于spring 与mybatis的结合配的太简单才让我小看了spring mvc与mybatis的集成,再一个是所有的代码都在一个包里面pom文件引用的类库也全一般也不会出什么问题.

2 spring mvc 与 mybatis

先看看项目的大体架子

spring mvc mybatis集成踩的坑

这个分的也很清楚吧

先从web项目说起吧 web.xml在这我们用的是servlet3.1,在这很多人都会出错,错就错在这个schema,因为maven默认的那个web骨架项目的web.xml有点太个性,要动那个骨架文件也太麻烦了,这我就全贴出来了(要用直接考走)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <display-name>Spring Mvc Web Application</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/app*.xml
</param-value>
</context-param>
<!-- 上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- spring 支持 -->
<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-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

在这我的spring servlet我没用默认的在web-info下的那个,我是给spring的DispatcherServlet传入了一个指定的xml  springmvc-servlet.xml 放在了resource下

<?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"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.kevin.springmvc.web.controller" /> <mvc:annotation-driven /><!--这一定要注意了,tomcat不写也可以运行,到别的容器上那不一定--> <!--视图解板器-->
<mvc:view-resolvers>
<mvc:jsp cache-views="false" prefix="/WEB-INF/jsp/" suffix=".jsp" />
</mvc:view-resolvers> <!--json-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<description>注解controller方法适配器</description>
<property name="messageConverters">
<list><!--JSON转换器 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json;charset=UTF-8" />
</bean>
</list>
</property>
</bean>
</beans>

在这个配置文件的时候我遇到一个小小的问题啊,就是我文件里红色标注的,tomcat下没问题 该怎么启动运行的也没事,在我换tomcat之前我用的是tomEE 启动的时候一直报错,我说这怎么回事啊,然后开始一点一点的查发现这个东西不加不行,加完后在tomcat

上试了也没问题 ,那好吧,为了别的容器不出错我看我还是要在验证更加严格的容器上来做吧,还是用我的tomee.

这些完了之后下面就是spring与mybatis的集成了,之前我是直接拿上面我spring与mybatis的配置文件,但那个文件太乱,所有的东西全在一块不利于修改,在这我把他们分开了,由于我只是个简单的集成我分了三个文件

app-context.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:context="http://www.springframework.org/schema/context"
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"> <!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <context:component-scan base-package="com.kevin.springmvc.service.*"/>
</beans>

这个简单只引入了配置文件和自动扫描

app-datasource.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.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<!-- 基本属性 url、user、password -->
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pwd}"/> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="20"/> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
</bean> </beans>

这个也好说,就配了一个连接池别的没用

app-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<description>spring和MyBatis完美整合,不需要mybatis的配置映射文件</description>
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:com/kevin/springmvc/dao/sqlmapper/*.xml" />
<property name="typeAliasesPackage" value="com.kevin.springmvc.beans" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<description>DAO接口所在包名,Spring会自动查找其下的类</description>
<property name="basePackage" value="com.kevin.springmvc.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

这也没什么好说的只是把文件拆分一下而以

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> <settings>
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指定),不会加载关联表的所有字段,以提高性能 -->
<setting name="aggressiveLazyLoading" value="false" />
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true" />
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true" />
<!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->
<setting name="autoMappingBehavior" value="FULL" />
<!-- 对于批量更新操作缓存SQL以提高性能 -->
<setting name="defaultExecutorType" value="BATCH" />
<!-- 数据库超过25000秒仍未响应则超时 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings> </configuration>

这是mybatis的一些全局设置

这些个配置文件都弄了,基本的maven引用也弄完了,貌似可以运行了,我错了,这才是我的噩梦的开始啊,一运行全是错

1数据源的错

  这个我当时无语啊,数据库配好了,maven也引用了,按说不应该啊,然后一顿好找啊,先看错误吧报找不到数据库驱动,哦哦哦哦,知道了parent的maven引用我加了但在web项目下我没加哎加上,这不出错了,但问题又来了

2 mybatis的mapper文件与接口文件绑定不了,报找不到statment

  在这我要说一下了,我的mapper文件并没有放到web的resource文件夹,我放到了dao,和数据访问的接口放到一个模块下了,但是maven在生成jar文件时是不会把 xml文件把包到jar文件里,这就需要我们来设置maven文件了,

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>
src/main/resources
</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>

就是这个resources的节点,加上它就没事了,好了,现在我的所有的东西都可以去行了

我在做这些的时候也在网看上看到过好多的问题,在我看大部分都是maven文件的引用缺少引起的,大家还是把相关的文件好好查一下,然后放到idea下结合tomcat来运行吧,这个报的错还算清析,之前我用的eclipsee 加tomcat报错提示的不太清楚,有些也无从查

起,也许是我用eclipse用的不太好,大家别喷我啊.

好了,以上两个是我在做spring mvc的时候遇到的,在些只做一个总结.

总之还是细心细心再细心