为了账号安全,请及时绑定邮箱和手机立即绑定

MyBatis整合Spring插入变慢的问题

MyBatis整合Spring插入变慢的问题

慕娘5188226 2018-07-04 15:44:14
先上代码:@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秒。这是为什么呢?请大佬们告知!
查看完整描述

目前暂无任何回答

  • 0 回答
  • 2 关注
  • 1493 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号