赞
踩
最近写ssm项目升级为mybatis-plus发现一个问题,就是在ssm中使用mybatis-plus, mapper和xml文件可以正常生成,但是mybatis-plus自带的增删改查方法用不了,
报错如下
查找了半天,发现是SqlSessionFactory的问题,Mybatis-plus 和Mybatis 是兼容的。
所以网上的配置 mybatis 的SqlSessionFactory 的方法基本上可行,但是对于mybatis-plus 的一些特性会丢失, 自带的增删改查方法用不了
原配置如下
package com.it.car.config; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.github.pagehelper.PageInterceptor; import com.zaxxer.hikari.HikariDataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.*; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import java.io.IOException; import java.util.Properties; /** * 配置类作用: * 1.开启service层包扫描 * 2.service注入dao对象 * 3.注入事务管理器对象 */ @Configuration @ComponentScan("com.it.car.service") //开启spring AOP 代理 proxyTargetClass 默认false jdk 启用 true cglib @EnableAspectJAutoProxy(proxyTargetClass = true) //引入数据库连接池配置信息 @Import({SpringDao.class,RedisConfig.class}) public class SpringService { @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(HikariDataSource hikariDataSource){ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); //配置: 核心配置文件url地址 sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); //配置:sql映射文件的url地址,如果sql映射文件和接口的文件同名同目录,这行配置可以省略不写 /*sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:com/it/dao/*.xml"));*/ //配置分页插件 PageInterceptor pageInterceptor = new PageInterceptor(); //设置分页插件的属性 Properties properties = new Properties(); properties.setProperty("helperDialect","mysql"); properties.setProperty("reasonable","true"); pageInterceptor.setProperties(properties); sqlSessionFactoryBean.setPlugins(pageInterceptor); // 将资源设置到 sqlSessionFactoryBean 的 mapperLocations try { sqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:/mappers/**/*.xml")); } catch (IOException e) { throw new RuntimeException(e); } //配置数据库连接池 sqlSessionFactoryBean.setDataSource(hikariDataSource); //可选配置,设置别名 sqlSessionFactoryBean.setTypeAliasesPackage("com.it.car.outlet.po"); return sqlSessionFactoryBean; } /** * sql映射文件对应接口时扫描器,通过指定的sqlSessionFactoryBean获取SqlSession对象 * sqlSession.getMapper(接口.class)获取代理对象 * @return */ @Bean public MapperScannerConfigurer configurer(){ MapperScannerConfigurer configurer=new MapperScannerConfigurer(); //关联SqlSessionFactoryBean对象 configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); //扫描器配置接口扫描包范围 configurer.setBasePackage("com.it.car.outlet.dao"); return configurer; } /** * 事务管理器,即利用spring AOP管理事务时的增强类 * @param dataSource * @return */ @Bean public DataSourceTransactionManager dataSourceTransactionManager(HikariDataSource dataSource){ DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
要想使用Mybatis-plus 自带增删改查方法,需要将
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
换成mybatis-plus
修改配置如下
package com.it.car.config; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.github.pagehelper.PageInterceptor; import com.zaxxer.hikari.HikariDataSource; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.*; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import java.io.IOException; import java.util.Properties; /** * 配置类作用: * 1.开启service层包扫描 * 2.service注入dao对象 * 3.注入事务管理器对象 */ @Configuration @ComponentScan("com.it.car.service") //开启spring AOP 代理 proxyTargetClass 默认false jdk 启用 true cglib @EnableAspectJAutoProxy(proxyTargetClass = true) //引入数据库连接池配置信息 @Import({SpringDao.class,RedisConfig.class}) public class SpringService { @Bean public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(HikariDataSource hikariDataSource){ MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); //配置: 核心配置文件url地址 mybatisSqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); //配置:sql映射文件的url地址,如果sql映射文件和接口的文件同名同目录,这行配置可以省略不写 /*sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:com/it/dao/*.xml"));*/ //配置分页插件 PageInterceptor pageInterceptor = new PageInterceptor(); //设置分页插件的属性 Properties properties = new Properties(); properties.setProperty("helperDialect","mysql"); properties.setProperty("reasonable","true"); pageInterceptor.setProperties(properties); mybatisSqlSessionFactoryBean.setPlugins(pageInterceptor); // 将资源设置到 sqlSessionFactoryBean 的 mapperLocations try { mybatisSqlSessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:/mappers/**/*.xml")); } catch (IOException e) { throw new RuntimeException(e); } //配置数据库连接池 mybatisSqlSessionFactoryBean.setDataSource(hikariDataSource); //可选配置,设置别名 mybatisSqlSessionFactoryBean.setTypeAliasesPackage("com.it.car.outlet.po"); return mybatisSqlSessionFactoryBean; } /** * sql映射文件对应接口时扫描器,通过指定的sqlSessionFactoryBean获取SqlSession对象 * sqlSession.getMapper(接口.class)获取代理对象 * @return */ @Bean public MapperScannerConfigurer configurer(){ MapperScannerConfigurer configurer=new MapperScannerConfigurer(); //关联SqlSessionFactoryBean对象 configurer.setSqlSessionFactoryBeanName("mybatisSqlSessionFactoryBean"); //扫描器配置接口扫描包范围 configurer.setBasePackage("com.it.car.outlet.dao"); return configurer; } /** * 事务管理器,即利用spring AOP管理事务时的增强类 * @param dataSource * @return */ @Bean public DataSourceTransactionManager dataSourceTransactionManager(HikariDataSource dataSource){ DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
再次运行,成功不报错
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。