首先说,出BUG了,不过解决了。
1.找不到dao里面的方法。如图1所示
2.Controller事务不生效,但写测试类时,事务生效(就是通过浏览器访问一个url,url指向Controller,里面调用service保存方法,service里面制造一个异常,但是数据保存到数据库了)。 如图三、四、五
第一个问题出在下面这段代码配置xml路径上,这里的路径,随便点开一个BeanDao看dao包名,看看xml到底实际路径是哪个。
错误:src/example/dao/xml/.xml
正确:example/dao/xml/.xml
<!-- Mybatis的工厂 实现谁 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:mybatisSpring-config.xml"/>
<property name="mapperLocations" value="classpath*:example/dao/xml/*.xml"/>
</bean>
第二个解决有点意思:
我看网上说:
SpringMVC开启扫描后默认对 @Service,@Controller和@Repository 注解的类进行扫描(use-default-filters=true)
但是,在springmvc的配置文件中扫描controller时要忽略service,因为在springmvc的配置文件加载的service事务不起作用。
问题就出在SpringMVC也扫描了@Sercive注解标签,导致事务无效。
处理是:只扫描controller即可
方式一:
<context:component-scan base-package="example.controller"/>
方式二:
<context:component-scan base-package="example" use-default-filters="false">
<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>
图一

【配置SpringMVC】
1.在SpringMVC入门程序的时候,除了配置前端控制器以外,还需要开启注解扫描。
2.看下图工作原理图就知道,需要配置的有:
A:前端控制器
B:映射处理器
C:处理器适配器
D:视图解析器
<!-- 开启注解扫描
<context:component-scan base-package="example.controller"/> -->
<context:component-scan base-package="example" use-default-filters="false">
<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>
<!-- 配置处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- 配置处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<!-- 配置处理器映射器 配置处理器适配器 可以用下面这个标签开启
<mvc:annotation-driven/> -->
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>


【配置Mybatis】
首先,看官方文档怎么说的先:(很重要)
官方一

官方二
@Test
public void userDaoTest() throws IOException{
SqlSession session = null;
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
BeanDao mapper = session.getMapper(BeanDao.class);
} catch (Exception e) {
}finally{
session.close();
}
}

【Spring配置】
1.接上面Mybatis的说。
所以,Mybatis配置文件需要配置
A:数据源
B:bean 的xml文件
但是既然整合了,就交给Spring处理。
A:配置数据源
1.看官方二的测试方法,要生成SqlsessionFactory需要MyBatis的配置文件
2.然后根据SqlsessionFactory获取SqlSession,然后把DAO传入获取到对象。
B:所以,需要配置一个org.mybatis.spring.SqlSessionFactoryBean,传入数据源,传入xml和dao

<!--数据源的配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactoryBean 并给它需要的参数-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatisSpring-config.xml"/>
<property name="mapperLocations" value="classpath*:example/dao/xml/*.xml"/>
</bean>
<!-- 用sqlsession创建对象(代理谁) 这些参数都是必须的 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
<property name="basePackage" value="example.dao"/>
</bean>

图三

图四

图五(测试类)

本文解决MyBatis与SpringMVC集成时出现的两个常见问题:一是无法找到DAO方法,二是Controller中事务不生效。通过调整XML配置路径和优化组件扫描策略,确保事务正确执行。
249

被折叠的 条评论
为什么被折叠?



