《springboot篇》数据源
连接oracle
参考链接:https://blog.csdn.net/weixin_44384605/article/details/118449107
新建项目
参考《java接力》springboot篇——新建项目
项目结构
引入依赖
POM文件,注意添加注释为“后面引入”的依赖。
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>oracle_java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>oracle_java</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 默认导入oracle连接包 因测试时会报错因此为采用-->
<!-- <dependency>-->
<!-- <groupId>com.oracle.database.jdbc</groupId>-->
<!-- <artifactId>ojdbc8</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!-- 引入ojdk6 用来替换ojdk8-->
<!--后面引入 Oracle相关-->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!--后面引入 Oracle-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--后面引入 Mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--后面引入 Mybatis-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!--后面引入 maven相关-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<!--后面引入 maven相关-->
</plugins>
<resources>
<!--静态资源,将Java代码下的xml编译到class下面去-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>
</project>
配置文件
application.yml中配置
server:
port: 8080
spring:
datasource:
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: system
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zhg.demo.mybatis.entity
configuration:
map-underscore-to-camel-case: true
代码
TextConterller.java文件
点击查看代码
package com.text.conterller;
import com.text.mapper.TextMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
@RequestMapping("/text")
public class TextConterller {
@Resource
TextMapper textMapper;
@RequestMapping(value = "/name",method = RequestMethod.GET)
@ResponseBody
public String getName(String code){
String name=textMapper.getName(code);
return name;
}
}
TextMapper.java文件
点击查看代码
package com.text.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TextMapper {
String getName(String code);
}
TextMapper.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.text.mapper.TextMapper">
<select id = "getName" resultType="String">
select name from v_emp where barcode=#{code} and rownum = 1
</select>
</mapper>
dao报错
参考链接:https://www.jb51.net/article/238921.htm
这里有个细节问题:这个dao可能会报错,但是不影响,如果看不习惯就在这里设置下:
测试
启动项目后,测试连接
http://localhost:8080/text/name?code=输入的内容
多数据源配置
以oralce为例
参考链接:https://www.jianshu.com/p/851f07e4928b
引入依赖
POM引入mybatis和oralce的依赖
<!--oracle 依赖-->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<!--oracle 依赖-->
<!--mybatis 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--mybatis 依赖-->
配置单数据源
修改application.yml
spring:
datasource:
url: jdbc:oracle:thin:@//*.*.*.28:1521/orcl
driver-class-name: oracle.jdbc.driver.OracleDriver
username: your_user_name
password: your_user_password
配置多数据源
修改application.yml
spring:
datasource:
fxdb:
jdbc-url: jdbc:oracle:thin:@//*.*.*.28:1521/orcl
username: your_user_name
password: your_user_password
driverClassName: oracle.jdbc.OracleDriver
创建一个配置类;
如果需要多数据源,创建多个DataSourceConfig,然后修改配置内的约定即可;
最后在yml文件中配置数据源(例如上面的 spring.datasource.fxdb);
点击查看代码
package com.sawl.fxapi.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Author ds
* @Date 2022-05-06
*/
@Configuration
@MapperScan(basePackages = "com.sawl.fxapi.api.*.dao", sqlSessionTemplateRef = "fxJdbcTemplate")
public class FXDataSourceConfig {
@Bean(name = "fxDataSource")
@ConfigurationProperties(prefix="spring.datasource.fxdb")
public DataSource fxDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "fxJdbcFactory")
public SqlSessionFactory fxJdbcFactory(@Qualifier(value ="fxDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/fx/*.xml"));
return bean.getObject();
}
@Bean(name = "fxJdbcManager")
public DataSourceTransactionManager fxJdbcManager(@Qualifier(value = "fxDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "fxJdbcTemplate")
public SqlSessionTemplate fxJdbcTemplate(@Qualifier(value = "fxJdbcFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}