首页 > 编程知识 正文

整合MyBatis出现异常property 'sqlSessionFactory' or 'sqlSessionTemplate' are - 解决方法

时间:2023-11-21 03:35:16 阅读:289598 作者:VEIN

本篇文章旨在帮助初学者解决整合MyBatis时可能遇到的问题(property 'sqlSessionFactory' or 'sqlSessionTemplate' are)

一、检查配置文件

首先,我们需要检查我们的配置文件是否正确。

1、检查mybatis-config.xml文件是否正确

在整合MyBatis时,我们需要在Spring配置文件中指定mybatis-config.xml的位置,例如:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

我们需要检查mybatis-config.xml文件是否正确,例如是否缺少必要的配置,例如是否有以下配置:

<configuration>
    <settings>
        <setting name="cacheEnabled" value="true" />
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="aggressiveLazyLoading" value="false" />
    </settings>
</configuration>

2、检查Mapper.xml文件是否正确

在整合MyBatis时,我们需要指定Mapper.xml文件的位置,例如:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath:mapper/*.xml" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>

我们需要检查Mapper.xml文件是否正确,例如是否缺少必要的配置,例如是否有以下配置:

<mapper namespace="com.example.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.model.User">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
    </resultMap>
 
    <select id="getUserById" resultMap="BaseResultMap">
        SELECT * FROM users WHERE id = #{id}
    </select>
</mapper>

二、检查依赖包

在整合MyBatis时,我们需要添加相应的依赖包,例如:

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
</dependency>
 
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
</dependency>

我们需要检查是否缺少必要的依赖包,例如是否缺少以下依赖包:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
 
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
</dependency>

三、检查Spring配置文件

在整合MyBatis时,我们需要在Spring配置文件中指定数据源和事务管理器,例如:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis" />
    <property name="user" value="root" />
    <property name="password" value="123456" />
</bean>
 
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

我们需要检查Spring配置文件是否正确,例如是否缺少必要的配置,例如是否有以下配置:

<tx:annotation-driven transaction-manager="transactionManager" />

四、样例代码

以下是一个简单的MyBatis整合Spring的样例代码:

package com.example.demo;
 
import com.example.mapper.UserMapper;
import com.example.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean(UserMapper.class);
        User user = userMapper.getUserById(1);
        System.out.println(user.getName());
    }
}

完整的配置文件(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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       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/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/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis" />
        <property name="user" value="root" />
        <property name="password" value="123456" />
    </bean>

    <!-- MyBatis SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <!-- MyBatis MapperScannerConfigurer -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.example.mapper" />
    </bean>

    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 开启事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。