背景
笔者最近改造一个老项目,原来项目是Hibernate的,由于项目维护的人不在这个项目了,现在需要添加Mybatis开发支持,正确配置如下
application.properties
#mysql database setting jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://172.16.3.50:3306/xxxx?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=exdfld #connection pool settings jdbc.pool.maxIdle=5 jdbc.pool.maxActive=40 #hibernate settings hibernate.show_sql=false hibernate.format_sql=false hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.search.default.indexBase=indexes #cache settings hibernate.ehcache.configFile=cache/ehcache-hibernate-local.xml ehcache.configFile=cache/ehcache-local.xml captcha.validate=true
applicationContext.xml
<?xml version="1.1" 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" 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 http://www.springframework.org/schema/tx/spring-tx.xsd" default-lazy-init="true"> <!-- 读取配置文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" /> <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 --> <context:component-scan base-package="com.xxx"> <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> <!-- <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/> <bean id="lobHandler" class="org.springframework.orm.hibernate3.support.BlobByteArrayType" /> --> <!-- 定义Hibernate Session工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="namingStrategy"> <bean class="org.hibernate.cfg.ImprovedNamingStrategy" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="use_sql_comments">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> </props> </property> <property name="packagesToScan" value="com.xxx" /><!-- 如果多个,用“,”分隔 --> </bean> <!-- SqlSessionFactoryBean的ID不要取sqlSessionFactory,不然MapperScannerConfigurer配置会出错 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- 定义事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 配置 JSR303 Bean Validator 定义 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> <!-- 数据源配置, 使用druid连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}" /> <property name="filters" value="mergeStat"/> <!-- 密码解密 --> <!-- <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=true" /> --> <!-- 申请连接的时候检测 --> <property name="testWhileIdle" value="true"></property> <!-- 检测连接 --> <property name="validationQuery" value="select 'x'"></property> <!--maxActive: 最大连接数量 --> <property name="maxActive" value="${jdbc.pool.maxActive}"/> <!--initialSize: 初始化连接 --> <property name="initialSize" value="${jdbc.pool.maxIdle}"/> </bean> <!-- 扫描mapper接口及xml --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xxx.**"></property> </bean> </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="lazyLoadingEnabled" value="false" /> <setting name="defaultStatementTimeout" value="25000" /> <setting name="defaultExecutorType" value="REUSE" /> <setting name="logImpl" value="LOG4J" /> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- for jdk1.8 new date/time api <typeHandlers> <typeHandler handler="org.apache.ibatis.type.InstantTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.ZonedDateTimeTypeHandler" /> </typeHandlers>--> </configuration>
pom.xml
<properties> <!-- 主要依赖库的版本定义 --> <spring.version>4.1.3.RELEASE</spring.version> <hibernate.version>4.1.8.Final</hibernate.version> <hibernate-validator.version>4.3.2.Final</hibernate-validator.version> <shiro.version>1.2.2</shiro.version> <jackson.version>2.4.4</jackson.version> <slf4j.version>1.7.10</slf4j.version> <log4j.version>1.2.17</log4j.version> <logback.version>1.1.2</logback.version> <commons-lang3.version>3.3.2</commons-lang3.version> <mybatis.version>3.4.6</mybatis.version> <mybatis-spring.version>1.3.2</mybatis-spring.version> <mybatis-ehcache.version>1.0.0</mybatis-ehcache.version> <mybatis-typehandlers-jsr310.version>1.0.2</mybatis-typehandlers-jsr310.version> <guava.version>18.0</guava.version> <junit.version>4.12</junit.version> <annotation-version>1.2</annotation-version> <!-- jdbc driver --> <jdbc.driver.groupId>mysql</jdbc.driver.groupId> <jdbc.driver.artifactId>mysql-connector-java</jdbc.driver.artifactId> <jdbc.driver.version>5.1.30</jdbc.driver.version> <!-- other --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.7</jdk.version> </properties> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>${mybatis-ehcache.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-typehandlers-jsr310</artifactId> <version>${mybatis-typehandlers-jsr310.version}</version> </dependency>
ProjectMapper.java
import com.h2.project.entity.Project; import org.apache.ibatis.annotations.Param; import java.util.Date; public interface ProjectMapper { Project getById(Integer id); int updateRecordTime(@Param("id") Integer projectId, @Param("recordTime")Date recordTime); }
ProjectMapper.xml(和ProjectMapper.java放在同一个目录中)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.xxx.project.dao.ProjectMapper" > <update id="updateRecordTime"> update project set record_time = #{recordTime} where id = #{id} </update> <select id="getById" resultType="com.xxx.project.entity.Project"> select * from project where id = #{id} </select> </mapper>
出错信息
报错信息如下:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.h2.project.dao.ProjectMapper.getById at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227) ~[MapperMethod$SqlCommand.class:3.4.6] at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49) ~[MapperMethod.class:3.4.6] at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[MapperProxy.class:3.4.6] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[MapperProxy.class:3.4.6] at com.sun.proxy.$Proxy51.getById(Unknown Source) ~[na:na] at com.h2.project.service.ProjectService.getById(ProjectService.java:173) ~[ProjectService.class:na] at com.h2.project.service.ProjectService$$FastClassBySpringCGLIB$$326c18c1.invoke(<generated>) ~[ReflectUtils.class:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[MethodProxy.class:4.1.3.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) ~[CglibAopProxy$CglibMethodInvocation.class:4.1.3.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[ReflectiveMethodInvocation.class:4.1.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[TransactionInterceptor$1.class:4.1.3.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267) ~[TransactionAspectSupport.class:4.1.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[TransactionInterceptor.class:4.1.3.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[ReflectiveMethodInvocation.class:4.1.3.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) ~[CglibAopProxy$DynamicAdvisedInterceptor.class:4.1.3.RELEASE] at com.h2.project.service.ProjectService$$EnhancerBySpringCGLIB$$d7751fe2.getById(<generated>) ~[ReflectUtils.class:na] at com.h2.project.service.ProjectService$$FastClassBySpringCGLIB$$326c18c1.invoke(<generated>) ~[ReflectUtils.class:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[MethodProxy.class:4.1.3.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) ~[CglibAopProxy$CglibMethodInvocation.class:4.1.3.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[ReflectiveMethodInvocation.class:4.1.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[TransactionInterceptor$1.class:4.1.3.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267) ~[TransactionAspectSupport.class:4.1.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[TransactionInterceptor.class:4.1.3.RELEASE]
本地配置
后来到线上下载一个最近可运行版本,发现xml文件根本没有部署成功。(右边是可运行版本)
解决方法
把ProjectMapper.xml部署到对应XXXMapper.class同级目录,不过虽然找到原因,笔者不知道IDEA(本地版本2018.3.4)为什么出现这个问题。
部署成功后如下: