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

1 spring与mybatis的集成

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



这是我的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

先看看项目的大体架子

这个分的也很清楚吧

先从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的时候遇到的,在些只做一个总结.

总之还是细心细心再细心

spring mvc mybatis集成踩的坑的更多相关文章

  1. Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  2. Spring+Spring MVC+MyBatis

    Spring+Spring MVC+MyBatis 目录 一.新建一个基于Maven的Web项目 二.创建数据库与表 三.添加依赖包 四.新建POJO实体层 五.新建MyBatis SQL映射层 六. ...

  3. maven/eclipse搭建ssm(spring+spring mvc+mybatis)

    maven/eclipse搭建ssm(spring+spring mvc+mybatis) 前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实 ...

  4. java企业架构 spring mvc +mybatis + KafKa+Flume+Zookeeper

    声明:该框架面向企业,是大型互联网分布式企业架构,后期会介绍linux上部署高可用集群项目. 项目基础功能截图(自提供了最小部分)      平台简介        Jeesz是一个分布式的框架,提供 ...

  5. Spring MVC + Mybatis项目搭建

    1.参考<Java Spring MVC项目搭建(一)——Spring MVC框架集成>配置spring mvc需要的jar包及eclipse配置(主要是针对servlet-api.jar ...

  6. 基于Spring+Spring MVC+Mybatis的B2C购物网站

    代码地址如下:http://www.demodashi.com/demo/12935.html 准备工作 当前项目运行的系统环境是MacOS,已经测试可以正常运行,并且之前开发的时候也在Windows ...

  7. spring Mvc + Mybatis 中使用junit

    在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/ja ...

  8. 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建

    一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...

  9. spring + spring mvc + mybatis + react + reflux + webpack Web工程例子

    前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...

随机推荐

  1. YUM 安装及清理

    Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理 ...

  2. chrome浏览器美化插件:让你的浏览器页面冒水泡, 游小鱼儿

    下载插件和效果图 这是一个让你的浏览器冒泡泡的插件, 浏览网页的时候仿佛置身于海底世界: 插件下载地址:http://files.cnblogs.com/files/diligenceday/chro ...

  3. GPU渲染管线概述

    1.顶点着色器 顶点着色器是流水线的第一个阶段,它的输入来自于CPU.顶点着色器的处理单位是顶点,也就是说输入进来的每个顶点都会调用一次顶点着色器. 顶点着色器需要完成的工作主要有:坐标变换和逐顶点光 ...

  4. 小白能学好UI设计吗

    许多童鞋在接触UI培训前会有很多疑问,我是干快递的,我能学好UI设计吗,UI培训要学些什么,电脑操作我好像什么都不会,除了打游戏,我适合学UI设计吗--有这些想法呢是人之常情,但是我们反过来想一想,有 ...

  5. jquery考试纠错笔记.

    1. 获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one +div& ...

  6. shell笔记整理1---vim编译器基础应用(参考鸟哥)

    1.linux中的配置文件都已是以ASCII的纯文本的形式存在 2.vim文本编译器. 一般模式:用vi打开的一个文件直接进入的就是一般模式,这个模式可以移动光标和删除字符,复制粘贴等,但是不能比那几 ...

  7. 通讯录--(适配iOS7/8/9)

    导入库#import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> #pragma m ...

  8. swift -- as / 扩展

    一.使用 可选链式 调用代替强制展开 //当声明一个属性时,将属性类型设置为可选类型: 好处: 当可选类型的属性被赋予初始值时,系统调用初始值;当可选类型属性没有赋予初始值时,系统只会调用失败;如果属 ...

  9. Solr commit 策略测试

    已知Solr 的Commit策略: 服务器端: 1)AutoCommit 2)AutoSoftCommit 客户端 Commit 本次我测试了客户端关闭Commit的情况下,服务器端Commit策略的 ...

  10. TCP协议设计原理

    TCP协议设计原理 最近去了解TCP协议,发现这是一个特别值得深思的协议.在本篇博客中,不会长篇大论的给大家介绍TCP协议特点.包头格式以及TCP的连接和断开等基本原理,而是会带大家深入理解为什么要这 ...