dependency去掉 pagehelper-spring-boot-starter mybatis-spring-boot-starter
添加
版本号
<spring.boot.version>2.0.7.RELEASE</spring.boot.version>
<mybatisplus.version>3.0.7.1</mybatisplus.version>
<mysql.version>8.0.15</mysql.version>
<springframework.version>5.2.7.RELEASE</springframework.version>
mybatis-plus版本低的话,不能使用
LambdaQueryWrapper
依赖
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<!-- mybatis-plus -->
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- pagehelper 依赖 -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.1</version>
</dependency>
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import javax.annotation.Resource;
import javax.sql.DataSource;
/**
* @author me
* @create 2019/11/18 14:41
* @description: <pre>
* MybatisPagenationConfig
* +--------------------------------------------------------------------
* 更改历史
* 更改时间 更改人 目标版本 更改内容
* +--------------------------------------------------------------------
* 2019/11/18 me 1.2.0 更新
*
* </pre>
**/
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"xx.xx.xx"})
@MapperScan("com.xx.xx.mappers")
public class MybatisPagenationConfig implements TransactionManagementConfigurer {
private static final Logger LOGGER = LoggerFactory.getLogger(MybatisPagenationConfig.class);
@Resource
private DataSource dataSource;
// 重点
// @Bean(name = "sqlSessionFactory")
// public SqlSessionFactory sqlSessionFactoryBean() throws IOException {
// SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// bean.setDataSource(dataSource);
// Properties props = new Properties();
// // 分页合理化,true开启,如果分页参数不合理会自动修正。默认false不启用
// props.setProperty("reasonable", "false");
// // 是否支持接口参数来传递分页参数,默认false
// props.setProperty("supportMethodsArguments", "true");
// props.setProperty("returnPageInfo", "check");
// props.setProperty("params", "count=countSql");
// PageInterceptor pageInterceptor = new PageInterceptor();
// pageInterceptor.setProperties(props);
// bean.setPlugins(new Interceptor[]{pageInterceptor});
// PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// bean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
// try {
// return bean.getObject();
// } catch (Exception e) {
// LoggerUtils.error(LOGGER, "方法Exception : {}", e);
// }
// return null;
// }
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.addInterceptor(new com.github.pagehelper.PageInterceptor());
}
};
}
}
以下内容转载 地址https://www.cnblogs.com/huahua035/p/10345382.html
1、mapper xml文件的扫描
如果使用xml配置sql,需要告诉SpringBoot扫描这些xml,常用以下两种配置方法
方法一:配置文件指定扫描路径(推荐)
1 mybatis:
2 mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
3 type-aliases-package: com.winter.model # 注意:对应实体类的路径
方法二:配置 SqlSessionFactory
Mybatis万能的SqlSessionFactory接口(还有一个SqlSession接口,他俩是mybatis的核心),直接在他里面指定xml路径
@Autowired
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,
PageHelper pageHelper) throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
/** 添加mapper 扫描路径 */
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/sql/*.xml";
sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
/** 设置datasource */
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper });
return sqlSessionFactoryBean;
}
2、mapper接口的扫描
mapper接口是真正的java接口,使用动态代理,虽然只是接口定义,却实现了真正的sql执行、响应结果映射封装等,需要告诉SpringBoot扫描这些mapper接口,常用以下两种配置方法
方法一:接口上添加注解(推荐)
@Mapper
public interface PermissionMapper {
... 略 ...
}
方法二:指定扫描包路径
@MapperScan("com.XXX.XXX.services.mapper")
总之,SpringBoot中注意xml和mapper接口的扫描配置。
出现:BindingException: Invalid bound statement (not found) 这种异常,问题排查步骤:
1、先确认如上两个配置是否正常;
2、检查mapper文件,方法是否存在
3、检查xml文件,id=方法名 的sql是否存在,该xml对应的mapper接口是否存在
注意:
如果sql通过annotation注解写在mapper接口上,同时也使用了xml的方式,注意id不能重复,即使参数完全不同,id也必须不同(mybatis的xml里面可没有override的概念)
相同的id只能存在不同的namespace里面
以上内容转载 地址https://www.cnblogs.com/huahua035/p/10345382.html
看下边两个地方,我的是这个情况
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.activelink.iot.entity,com.activelink.iot.model
不能配置如下
//@Bean(name = "sqlSessionFactory")
//public SqlSessionFactory sqlSessionFactoryBean() throws IOException {
//SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//bean.setDataSource(dataSource);
//Properties props = new Properties();
分页合理化,true开启,如果分页参数不合理会自动修正。默认false不启用
//props.setProperty("reasonable", "false");
是否支持接口参数来传递分页参数,默认false
//props.setProperty("supportMethodsArguments", "true");
//props.setProperty("returnPageInfo", "check");
//props.setProperty("params", "count=countSql");
//PageInterceptor pageInterceptor = new PageInterceptor();
//pageInterceptor.setProperties(props);
//bean.setPlugins(new Interceptor[]{pageInterceptor});
//PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
//try {
//return bean.getObject();
//} catch (Exception e) {
//e.printStackTrace();
//}
//return null;
//}