spring整合mybatis

img

img

spring配置文件



<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">


    
    <context:component-scan base-package="mapper"/>

    
    <context:property-placeholder location="jdbc.properties"/>

    
    <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}"/>
    bean>

    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="configLocation" value="Sqlconfig.xml"/>
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="typeAliasesPackage" value="pojo"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mapper"/>
    bean>

    
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>

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

mybaits核心配置文件



configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    settings>
configuration>
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Mytest {

    @Autowired
    private MyMapper m;
    @Test
    public void test01(){
        Studnet selectbysno = m.selectbysno("0001");
        System.out.println(selectbysno);
    }
}
报错信息

img

分析一:这是一个Spring整合MyBatis的测试类,在运行时报错:"org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.nybatis.spring.SglSessionfactoryBean#g': Lookup method." 可能是因为在加载Spring配置文件时发生了错误,请检查Spring配置文件的正确性和依赖的组件是否都已安装。

分析二:这是一个Spring框架应用的配置文件。配置文件主要包括以下内容:

通过context:component-scan元素指定扫描的包
通过context:property-placeholder元素加载jdbc.properties配置文件
通过元素配置数据源(使用阿里巴巴的Druid数据源)
通过元素配置MyBatis SqlSessionFactory
通过元素配置MyBatis映射器扫描
通过元素配置事务管理器
通过tx:annotation-driven元素启用事务支持。

分析三:这是一段Mybatis的映射文件代码,是定义在mapper包中的MyMapper.xml中的,这个文件定义了名为"selectbysno"的查询方法,它会从数据库的"students"表中查询编号为参数"sno"的行,并返回一个包含编号,姓名,年龄,籍贯和年级的学生对象。

根据报错信息,问题出在创建bean的过程中。在名为org.nybatis.spring.SglSessionfactoryBean#g的bean的创建过程中,遇到了错误。可能的原因是该类不存在,也有可能是配置文件中有打字错误。建议检查并修改配置文件中的错误。