springboot的mybatis

1、添加数据库驱动

<dependency>	
    <groupId>mysql</groupId>	
    <artifactId>mysql-connector-java</artifactId>	
</dependency>

添加MySQL驱动,这里如果不指定版本默认是8.0的版本,当然你也可以指定版本,8.0的版本这里有一问比较坑的问题:

  1. MySQL数据库默认使用的是美国的时区,而我们连接的时候用的是中国的北京时间,然后比美国晚上8个小时,所以当我们在连接数据库的时候要设置一下时区为东八区ServerTimezone=UTC
  2. 驱动类
  • MySQL5.x的版本使用的驱动类是com.mysql.jdbc.Driver
  • MySQL8.x的版本使用的驱动类是com.mysql.cj.jdbc.Driver

2、添加数据库连接池-阿里开源的druid

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid-spring-boot-starter</artifactId>
  <version>1.1.9</version>
</dependency>

 Druid首先是一个数据库连接池。Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBossDataSource。Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。Druid是阿里巴巴开发的号称为监控而生的数据库连接池!

3、依赖

<dependency>	           	 
  <groupId>org.mybatis.spring.boot</groupId>	
  <artifactId>mybatis-spring-boot-starter</artifactId>	
  <version>2.0.1</version>	
</dependency>

通常我们在集成一些SpringBoot提供支持的技术的时候,所添加的依赖都是以spring-boot-starter开头,格式:spring-boot-starter-xxx;但是刚才我们添加的Mybatis的依赖却是mybatis-spring-boot-starter,是以mybatis开头的,这个能其实是SpringBoot默认是不支持mybatis的,它默认支持的是它自己生态内的持久层框架JPA,由于SpringBoot是大势所趋,所以mybatis就主动去迎合SpringBoot生态,自己开发了mybatis的stater。以后大家凡是看到xxx-spring-boot-starter的依赖,都是SpringBoot没有主动提供支持的技术。

4、yml中配置数据源和mybatis配置

5、spring.factories

org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

实例化以下bean

SqlSessionFactory、SqlSessionTemplate、

MapperScannerRegistrarNotFoundConfiguration

如果没有使用@MapperScan这个注解,会去类路径下自动搜索@Mapper注解的类生成MappeFactoryBean

6、比较原始的mybatis配置,MapperFactoryBean,只能针对一个接口

 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
   <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
   <property name="sqlSessionFactory" ref="sqlSessionFactory" />
 </bean>
  @Override
  public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
  }

7、注解@Mapper对应的配置

指定的basePackage来自动装配MapperFactoryBean,但如果指定了多个DataSource,则必须指定 sqlSessionFactoryBeanName属性

7.1、xml配置

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>

7.2、注解@MapperScan配置

8、springboot整合多数据源

 两个数据源,对应两个SqlSessionFactory,@MapperScan扫描不同包下的接口

spring:
  datasource:
    test1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
      username: root
      password: 1234

    test2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
      username: root
      password: 1234
@Configuration
@MapperScan(basePackages = "com.example.mapper.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {


    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    @Primary
    public DataSource test1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "test1TransactionManager")
    @Primary
    public DataSourceTransactionManager test1TransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration
@MapperScan(basePackages = "com.example.mapper.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {


    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource test2DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "test2TransactionManager")
    public DataSourceTransactionManager test2TransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate test2SqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

9、多数据源动态切换

10、如果要写配置文件时有相应的提示,需要加一个依赖

  <!-- 自定义配置的提示 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

编译,target/classes/META-INF下会生成一个spring-configuration-metadata.json文件,在配置文件中写相关配置时就有提示啦(如果已存在该配置项,则不会提示)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot集成Mybatis是一种流行的开发框架组合,可以帮助开发者更方便地使用SpringBootMybatis进行项目开发。首先,你需要在application.properties文件中配置数据源和mapper接口的位置。通过设置spring.datasource.url、spring.datasource.username、spring.datasource.password等参数,来指定数据库连接和认证信息。同时,使用mybatis.mapper-locations来指定mapper接口文件的位置。 接下来,你需要在启动类中添加注解@MapperScan指定mapper接口的包路径。这个注解会自动扫描指定路径下的mapper接口,并与Mybatis进行关联。同时,添加@SpringBootApplication注解来启动SpringBoot应用程序。最后,通过main方法启动应用程序。 这样,你就成功地搭建了一个SpringBoot集成Mybatis的项目。你可以根据自己的需求,编写mapper接口和对应的SQL语句,以及相应的业务逻辑代码,来实现你的项目功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot集成Mybatis保姆级教程(完整版)](https://blog.csdn.net/xqnode/article/details/113079010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值