springboot整合mybatis+h2+oracle进行多库多数据源

一、引入mybatis以及h2依赖

          <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!--orcale数据库依赖 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>10.2.0.4.0</version>
        </dependency>

        <!-- h2内存数据库 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>

二、application.properties文件进行基本属性配置

################################h2数据源配置############################
#url有以下几种形式:
# jdbc:h2:E:*/database 会持久化到磁盘文件,但是是单连接
# jdbc:h2:tcp://IP/database 通过远程连接的方式
# jdbc:h2:mem:database 直接在内存中,程序只要重启就会消失
spring.datasource.url=jdbc:h2:mem:h2test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=zbook
spring.datasource.password=zbook
#程序启动后会初始化这些脚本文件
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql
spring.h2.console.settings.web-allow-others=true
spring.h2.console.path=/h2-console/h2test
spring.h2.console.enabled=true

#如果使用mybatis+druid多数据源,则需要下面master和slave+mybatis配置
####################################master数据源##############################
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#连接池初始化连接数量
spring.datasource.initialSize=5
#连接池最大活跃连接数
spring.datasource.maxActive=100
#最小空闲数
spring.datasource.min-idle=5
#最大等待时间
spring.datasource.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis: 300000
#连接是否有效的查询语句
spring.datasource.validationQuery: SELECT 1 FROM DUAL
spring.datasource.testWhileIdle: true
spring.datasource.testOnBorrow: false
spring.datasource.testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements: true
spring.datasource.maxPoolPreparedStatementPerConnectionSize: 50
spring.datasource.removeAbandoned: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters: stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
spring.datasource.useGlobalDataSourceStat:true
# SQL监控后台登录用户名
spring.datasources.druidLoginName=admin
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用MyBatis框架来实现与关系数据库的关联映射。下面是一个简单的示例: 1. 首先,确保你的项目中已经引入了Spring Boot和MyBatis的依赖。 2. 创建一个实体类,表示数据库中的一张表。例如,创建一个名为User的实体类,包含id、name和age属性。 3. 创建一个Mapper接口,用于定义数据库操作的方法。例如,创建一个名为UserMapper的接口,包含查询用户信息的方法。 4. 在Mapper接口中使用注解@Select、@Insert、@Update等来定义SQL语句。 5. 创建一个Mapper.xml文件,用于编写SQL语句。在该文件中,使用<select>、<insert>、<update>等标签来定义SQL语句。 6. 在Spring Boot的配置文件中,配置MyBatis的相关信息,如数据库连接信息、Mapper接口的扫描路径等。 7. 在Service层中调用Mapper接口的方法,实现业务逻辑。 8. 在Controller层中处理请求,并调用Service层的方法。 下面是一个示例代码: ```java // User.java public class User { private Integer id; private String name; private Integer age; // 省略getter和setter方法 } // UserMapper.java @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(Integer id); } // UserMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> // application.properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 mybatis.mapper-locations=classpath:mapper/*.xml // UserService.java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Integer id) { return userMapper.getUserById(id); } } // UserController.java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/{id}") public User getUserById(@PathVariable Integer id) { return userService.getUserById(id); } } ``` 以上示例演示了如何在Spring Boot中整合MyBatis,并实现了关联映射。你可以根据自己的需求,编写更多的Mapper接口和SQL语句来实现其他数据库操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值