Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3592272
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Java

2021-07-26 17:27:20

pom.xml

        

        

            com.github.pagehelper

            pagehelper

            5.2.0

        

        

            mysql

            mysql-connector-java

            8.0.21

        

 

        

            org.mybatis.spring.boot

            mybatis-spring-boot-starter

            1.3.2

        

         

            com.alibaba

            druid

        

yml

spring:

  application:

    name: spring-cloud-dynamic

  datasource:

    #类型

    type: com.alibaba.druid.pool.DruidDataSource

    driver-class-name: com.mysql.cj.jdbc.Driver

    url: jdbc:mysql://localhost:3306/f2f?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

    username: root

    password:

    initial-size: 2

    max-idle: 10

    min-idle: 1

    max-wait: 60000

    max-active: 20 #最大空闲连接数

    #多久进行一次检测,检测需要关闭的空闲连接

    time-between-eviction-tuns-millis: 60000

MybatisConfig

/**

 * @author lanys

 * @Description:

 * @date 23/7/2021 下午8:38

 */

@Configuration

@EnableTransactionManagement

@PropertySource(value = "classpath:application.yml", ignoreResourceNotFound = true)

public class MybatisConfig implements TransactionManagementConfigurer {

    @Value("${mybatis.mapper-locations}")

    private String mapper;

    @Value("${mybatis.type-aliases-package}")

    private String aliases;

    @Autowired

    private DataSource dataSource;

    @Bean(name = "sqlSessionFactory")

    public SqlSessionFactory sqlSessionFactory() throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

        // 设置数据源

        bean.setDataSource(dataSource);

        // 设置xml

        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper));

        // 设置别名

        bean.setTypeAliasesPackage(aliases);

        // 添加分页插件

        bean.setPlugins(new Interceptor[]{pageInterceptor()});

        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

        return bean.getObject();

    }

    /**

     * 分页拦截器

     * @return

     */

    private PageInterceptor pageInterceptor() {

        PageInterceptor pageInterceptor = new PageInterceptor();

        // 详见 com.github.pagehelper.page.PageParams

        Properties p = new Properties();

        // RowBounds是否进行count查询 - 默认不查询

        p.setProperty("rowBoundsWithCount", "true");

        // 当设置为true的时候,如果page size设置为0(或RowBoundslimit=0),就不执行分页,返回全部结果

        p.setProperty("pageSizeZero", "true");

        // 分页合理化

        p.setProperty("reasonable", "false");

        // 是否支持接口参数来传递分页参数,默认false

        p.setProperty("supportMethodsArguments", "true");

        // 设置数据库方言 , 也可以不设置,会动态获取

        p.setProperty("helperDialect", "mysql");

        pageInterceptor.setProperties(p);

        return pageInterceptor;

    }

    @Override

    public PlatformTransactionManager annotationDrivenTransactionManager() {

        return new DataSourceTransactionManager(dataSource);

    }

}

阅读(1510) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~