mybatis-spring 中配置mapperLocations 的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis-spring 中配置mapperLocations 的问题相关的知识,希望对你有一定的参考价值。
<!--
myBatis文件
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:code/sy/mapping/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="code.sy.dao,code.dy.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
在配置<property name="mapperLocations" value="classpath:code/sy/mapping/*.xml" /> 中如何指定多个包 比如配置到 classpath:code/dy包/mapping/*.xml,求大神搭救
我现在<property name="configLocation" value="classpath:spring-mapper.xml" /> 指定到配置文件进行配置。
配置文件如下:
<mappers>
<mapper resource="code/dy/mapping/UserMapper.xml" />
<mapper resource="code/sy/mapping/VipMapper.xml" />
</mappers>
如果就一条.xml文件还是可以执行的。问题二条就报错了
你好!
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<!--property name="mapperLocations" value="classpath:code/sy/mapping/*.xml" /-->
<!-- 改为下面的样子 -->
<property name="mapperLocations" >
<list>
<value>classpath*:code/dy/mapping/*.xml</value>
<value>classpath*:code/sy/mapping/*.xml</value>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="code.sy.dao,code.dy.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
classpath后面要带个'*',如果mapper.xml文件目录结构都在code目录下的话,
还可以通过'code/*/mapping/*.xml'通配符进行指定。
希望对你有帮助!
参考技术A <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:code/**/mapping/*.xml" />
</bean> 参考技术B <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:code/**/mapping/*.xml" />
</bean> 参考技术C 那你换package标签行吗…… 参考技术D 有帮助,谢谢分享
MyBatis-Spring 事务配置
官方链接:http://mybatis.org/spring/zh/transactions.html#configuration
1、依赖
tx和aop相关配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> <!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid 这里使用的Spring提供的JDBC:org.springframeword.jdbc.datasource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--绑定MyBatis配置文件--> <property name="configLocation" value="classpath:mybatisConfig.xml"></property> <!--配置mapper--> <property name="mapperLocations" value="classpath:com/doubleh/mapper/*.xml"></property> </bean> <!--SqlSessionTemplate:就是我们用的sqlSession--> <!--<tx:jta-transaction-manager />--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <!--注入DataSourceTransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean> <!--结合AOP实现事务织入--> <!--配置事务通知--> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <!--给哪些方法配置事务--> <!-- 配置事务传播特性 propagation="REQUIRED"--> <tx:method name="select*"></tx:method> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--配置事务切入--> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.doubleh.mapper.*.*(..))"></aop:pointcut> <aop:advisor advice-ref="tx" pointcut-ref="txPointcut"></aop:advisor> </aop:config> </beans>
2、声明式事务
<!--结合AOP实现事务织入--> <!--配置事务通知--> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <!--给哪些方法配置事务--> <!-- 配置事务传播特性 propagation="REQUIRED"--> <tx:method name="select*"></tx:method> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--配置事务切入--> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.doubleh.mapper.*.*(..))"></aop:pointcut> <aop:advisor advice-ref="tx" pointcut-ref="txPointcut"></aop:advisor> </aop:config>
3、编程式事务(略)
以上是关于mybatis-spring 中配置mapperLocations 的问题的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis 与 Spring 是如何结合在一起工作的——mybatis-spring(version:1.2.2)
MyBatis-Spring--Mapped Statements collection does not contain value for...问题解决