1.注解方式,yml文件配置上以下就可以直接使用
1
2
3
4
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.这一种网上没有,搜过好多资料都没有,我是配置多数据源,所以是在代码中写的config那么yml文件就是失效的,只能一个一个配置,到了打印sql的时候,就怎么都是找不到,后来设置的源码找到灵感,发现可以使用,特此记下,方便其他小伙伴遇到同样的问题使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Bean
`("sqlSessionFactory"
)`
public
SqlSessionFactory sqlSessionFactory()
throws
Exception {
// 导入mybatissqlsession配置
MybatisSqlSessionFactoryBean sessionFactory =
new
MybatisSqlSessionFactoryBean();
// 指明数据源
sessionFactory.setDataSource(multipleDataSource(dataSource0(), dataSource1(), dataSource2()));
// 指明mapper.xml位置(配置文件中指明的xml位置会失效用此方式代替,具体原因未知)
sessionFactory.setMapperLocations(
`new
PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/**Mapper.xml"
));`
// 指明实体扫描(多个package用逗号或者分号分隔)
sessionFactory.setTypeAliasesPackage(
`"gsa.geographic.system.entity"`);
// 导入mybatis配置
MybatisConfiguration configuration =
new
MybatisConfiguration();
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(
`true`);
configuration.setCacheEnabled(
`false`);
// 配置打印sql语句
configuration.setLogImpl(StdOutImpl.
`class`);
sessionFactory.setConfiguration(configuration);
// 添加分页功能
sessionFactory.setPlugins(
`new
Interceptor[]{`
paginationInterceptor()
});
// 导入全局配置
sessionFactory.setGlobalConfig(globalConfiguration());
return
sessionFactory.getObject();
}
主要就是这句
点击setLogImpl看源码,找到Configuration()构造方法,就可以看见了
访问一下看一下控制台
package cc.deepmall.config.datasource;
import cc.deepmall.config.MyBatisPlusModulesDeepMallMetaObjectHandler;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.Data;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.sql.SQLException;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
/**
* @Description: Java类作用描述
* @Author: linjinyu
* @CreateDate: 2021/1/11 下午12:06
* @UpdateUser: linjinyu
* @UpdateDate: 2021/1/11 下午12:06
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "deep-mall.datasource.druid")
@MapperScan(
basePackages = {
// "cc.deepmall.modules.monitor.dao.domain",
// "cc.deepmall.modules.quartz.dao.domain",
// "cc.deepmall.modules.system.dao.domain",
//
// "cc.deepmall.modules.shop.dao.entity",
//// "cc.deepmall.modules.taobaoke.dao.entity",
// "cc.deepmall.modules.activity.dao.entity",
//
// "cc.deepmall.modules.tools.dao.domain",
// "cc.deepmall.modules.gen.dao.domain",
// "cc.deepmall.modules.logging.dao.domain",
// "cc.deepmall.modules.mp.dao.domain",
"cc.deepmall.modules.monitor.dao.mapper",
"cc.deepmall.modules.quartz.dao.mapper",
"cc.deepmall.modules.system.dao.mapper",
"cc.deepmall.modules.shop.dao.mapper",
"cc.deepmall.modules.mallx.dao.mapper",
// "cc.deepmall.modules.taobaoke.dao.mapper",
"cc.deepmall.modules.activity.dao.mapper",
"cc.deepmall.modules.tools.dao.mapper",
"cc.deepmall.modules.gen.dao.mapper",
"cc.deepmall.modules.logging.dao.mapper",
"cc.deepmall.modules.mp.dao.mapper",
},
sqlSessionFactoryRef = "deepMallSqlSessionFactory")
public class DataSourceConfigModulesDeepMall {
private String filters;
private String url;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
private long maxWait;
private long timeBetweenEvictionRunsMillis;
private long minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
@Bean(name = "deepMallDataSource")
@Primary
public DataSource getDateSourceMoguShopMall() throws SQLException {
// return DataSourceBuilder.create().build();
DruidDataSource druid = new DruidDataSource();
// 监控统计拦截的filters
druid.setFilters(filters);
// 配置基本属性
druid.setDriverClassName(driverClassName);
druid.setUsername(username);
druid.setPassword(password);
druid.setUrl(url);
// 初始化时建立物理连接的个数
druid.setInitialSize(initialSize);
// 最大连接池数量
druid.setMaxActive(maxActive);
// 最小连接池数量
druid.setMinIdle(minIdle);
// 获取连接时最大等待时间,单位毫秒。
druid.setMaxWait(maxWait);
// 间隔多久进行一次检测,检测需要关闭的空闲连接
druid.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// 一个连接在池中最小生存的时间
druid.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// 用来检测连接是否有效的sql
druid.setValidationQuery(validationQuery);
// 建议配置为true,不影响性能,并且保证安全性。
druid.setTestWhileIdle(testWhileIdle);
// 申请连接时执行validationQuery检测连接是否有效
druid.setTestOnBorrow(testOnBorrow);
druid.setTestOnReturn(testOnReturn);
// 是否缓存preparedStatement,也就是PSCache,oracle设为true,mysql设为false。分库分表较多推荐设置为false
druid.setPoolPreparedStatements(poolPreparedStatements);
// 打开PSCache时,指定每个连接上PSCache的大小
druid.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
return druid;
}
// 表示这个数据源是默认数据源
@Bean(name = "deepMallSqlSessionFactory")
// @Qualifier表示查找Spring容器中名字为deepMallDataSource的对象
@Primary
public SqlSessionFactory deepMallSqlSessionFactory(@Qualifier("deepMallDataSource") DataSource datasource) throws Exception {
// 注意事项:
// 如果用myBatis, SqlSessionFactory 部分可以使用SqlSessionFactoryBean来生成。
// 但是如果用mybatis plus一定要用MybatisSqlSessionFactoryBean 来生成SqlSessionFactory。否则会报错 ,无法直接通过BaseMapper去调用查询。
// 如果要再不同的包中混合上XML进行调用。需要在SqlSessionFactory的配置中设置factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/ehr/**Mapper.xml"));
// SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// Mybatis plus配置MetaObjectHandler无效
// 解决方案 https://www.cnblogs.com/wangyong5609/p/14771590.html
GlobalConfig globalConfig = GlobalConfigUtils.defaults();
globalConfig.setMetaObjectHandler(new MyBatisPlusModulesDeepMallMetaObjectHandler());
MybatisConfiguration configuration = new MybatisConfiguration();
// configuration.setJdbcTypeForNull(JdbcType.NULL);
// configuration.setMapUnderscoreToCamelCase(true);
// configuration.setCacheEnabled(false);
// 配置打印sql语句
configuration.setLogImpl(StdOutImpl.class);
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
sessionFactoryBean.setDataSource(datasource);
sessionFactoryBean.setGlobalConfig(globalConfig);
sessionFactoryBean.setConfiguration(configuration);
sessionFactoryBean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:" +
"mappers/modules/monitor/**/*.xml," +
"mappers/modules/quartz/**/*.xml," +
"mappers/modules/system/**/*.xml," +
"mappers/modules/shop/**/*.xml," +
"mappers/modules/mallx/**/*.xml," +
// "mappers/modules/taobaoke/**/*.xml," +
"mappers/modules/activity/**/*.xml," +
"mappers/modules/tools/**/*.xml," +
"mappers/modules/mp/**/*.xml"));
return sessionFactoryBean.getObject();
}
// 多数据源配置样例
// @Bean("sqlSessionFactory")
// public SqlSessionFactory sqlSessionFactory() throws Exception {
// // 导入mybatissqlsession配置
// MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
// // 指明数据源
// sessionFactory.setDataSource(multipleDataSource(dataSource0(), dataSource1(), dataSource2()));
// // 指明mapper.xml位置(配置文件中指明的xml位置会失效用此方式代替,具体原因未知)
// sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/**Mapper.xml"));
// // 指明实体扫描(多个package用逗号或者分号分隔)
// sessionFactory.setTypeAliasesPackage("gsa.geographic.system.entity");
// // 导入mybatis配置
// MybatisConfiguration configuration = new MybatisConfiguration();
// configuration.setJdbcTypeForNull(JdbcType.NULL);
// configuration.setMapUnderscoreToCamelCase(true);
// configuration.setCacheEnabled(false);
// // 配置打印sql语句
// configuration.setLogImpl(StdOutImpl.class);
// sessionFactory.setConfiguration(configuration);
// // 添加分页功能
// sessionFactory.setPlugins(new Interceptor[]{
// paginationInterceptor()
// });
// // 导入全局配置
// sessionFactory.setGlobalConfig(globalConfiguration());
// return sessionFactory.getObject();
// }
@Bean(name = "deepMallTransactionManager")
@Primary
public DataSourceTransactionManager deepMallTransactionManager() throws SQLException {
return new DataSourceTransactionManager(getDateSourceMoguShopMall());
}
@Bean("deepMallSqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate deepMallSqlSessionTemplate(@Qualifier("deepMallSqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
到此这篇关于springboot+mybatis-plus 两种方式打印sql语句的方法的文章就介绍到这了,更多相关springboot+mybatis-plus打印sql内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
原网址: 访问
创建于: 2021-08-08 20:43:27
目录: default
标签: 无
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论