MyBatis整合Spring插入变慢的问题

先上代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml"})
public class TestMapper {

    @Autowired
    EmployeeMapper employeeMapper;

    @Test
    public void test() throws IOException {

        // System.out.println(departmentMapper.selectByPrimaryKey(1));

        SqlSessionFactory sqlSessionFactory = Main.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);


        long begin = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {
            mapper.insertSelective(new Employee("MrXieXie" + i, "MrXieXie" + i + "@qq.com", "1", null));
        }

        long end = System.currentTimeMillis();

        System.out.println("所用时间 : " + (end - begin));//所用时间 : 1252

        begin = System.currentTimeMillis();

        for (int i = 0; i < 1000; i++) {
            employeeMapper.insertSelective(new Employee("MrXieXie" + i, "MrXieXie" + i + "@qq.com", "1", null));
        }

        end = System.currentTimeMillis();

        System.out.println("所用时间 : " + (end - begin));//所用时间 : 7513
    }
}

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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    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 http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">


    <context:component-scan base-package="pers.mrxiexie.ssm">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

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


   <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
   </bean>

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

   <mybatis:scan base-package="pers.mrxiexie.ssm.mapper"/>
  
</beans>

Spring整合MyBatis的mybatis-config.xml

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

MyBatis单独使用的配置文件

<configuration>

    <!-- 类属性驼峰式,对应数据库下划线 -->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
        <setting name="jdbcTypeForNull" value="NULL" />
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>    
        <setting name="cacheEnabled" value="true"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="pers.mrxiexie.ssm.test.datasource.C3p0DataSource">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql:///ssm" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>

   <mappers>
        <mapper resource="mapper/EmployeeMapper.xml" />
   </mappers>
</configuration>

可以看出直接使用直接使用Mybatis插入只需要1秒。使用Spring整合MyBatis要用到7.5秒。这是为什么呢?请大佬们告知!

慕娘5188226
浏览 1494回答 0
0回答

慕设计2395807

OADQEZSRHOHLBQAVGILBYHBENWGCFZCSVTPSCYHDGPYOMHKURFUXNKMADGWLILHDNDKKADSCEADUJNJFOYNYOQGPHKZCKADTWTVUWYPEIRNXYADGOFOENPFOXDZVEHXBRAKZHDMPLHDMPSTJMWFUEAQSVLCEAXTWSVSOXBQLIROEBXUWFBCZVSBQTQSCLCFBRHQTCFCSIRUETQTIFQXUKTCYILOXTCSIKGRGKUDHQXILBXMCFVIKZPZQGJFJGLHWTJSVLUKMNPFORAKAJZIHJZILUJZJRNXMWTJMKMCZVSICYPLILHDGCFHKTJSCROKNWLOLVKFOKACYAWZJMVYBEHKZPFODZJSIHDTPLBWFOYUDNJTISORUQAZUQZOKUQSPFCRBEAEHCLBEUWLJQNWYIEOGHQAPSCSPYUXMBLAXGVSBYKMPLHQMPSTPYHJTWZUXNJMXADGQMWZPYVQSALHXAWZIYCSUJFHKHDZCYTWTCFVXLBLKTKNHOMCYFLTOLOROLOROXKMCZBZVENQGCEUROENJRAJHKTWZILHXYVVRTDFVSBQAWDLHEGJFVKNONWLUKHKFISIMVRIKNXNWGVIXHRAJMPXNQQAILNEVYBKTHVRUJGCEHENRNIFVYVFVLHRNPZBLAJMVLCMHRNQFVWTWLBPFBYNWSIEGPRZPRBENKGWSUQAKAWTCLFOJURROKNQGVETDGXGPFVWHDSBYBRHXNJKMHEVRHIYIFKYOYILHXSPRGWRBRNQZIFHWMBMVEVKNQMGOKAJGJSBYUNVLVQNPYILUDYURNEAQTJFXGVGILBXTWSLUWMISIZCLOPFNKNDFVLHWVOREYYZCUKHJMBRNXTPFVLYHQNJSBYBRTYMIYHLOXUXG
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java