spring boot版本:2.6.13
(一)添加maven依赖:包括mybatis和oracle数据库的驱动
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version>
</dependency>
<dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.1.0</version>
</dependency>
(二)配置文件中增加数据库相关配置
spring:datasource:driver-class-name: oracle.jdbc.OracleDriverjdbc-url: jdbc:oracle:thin:@localhost:1521/PDBORCLusername: mallpassword: 123
配置url时,如果用Java Config方式,需要改成jdbc-url,否则会报错。
我们以java config的方式配置事务,所以就不在配置文件中配置mybatis开头的配置项了。
(三)以JAVA Config方式配置数据源以及事务等
package com.mall.provider.config;import javax.sql.DataSource;import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
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;/*** @since 2024/12/6 19:23*/
@Configuration
//指定扫描的mapper接口所在的包
@MapperScan(basePackages = "com.mall.provider.dao", sqlSessionFactoryRef = "DBDataSqlSessionFactory")
public class DataSourceConfig {@Bean(name = "DBDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource() {return DataSourceBuilder.create().build();}@Bean(name = "DBDataSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("DBDataSource") DataSource dataSource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));bean.setTypeAliasesPackage("com.mall.provider.entity");org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setLogImpl(Slf4jImpl.class);configuration.setJdbcTypeForNull(JdbcType.NULL);configuration.setMapUnderscoreToCamelCase(true);bean.setConfiguration(configuration);return bean.getObject();}@Bean(name = "DBDataTransactionManager")public DataSourceTransactionManager transactionManager(@Qualifier("DBDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "DBDataSqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplate(@Qualifier("DBDataSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}
上面使用代码在SqlSessionFactory中设置数据库的一些配置,等同于在xml文件中设置如下配置:
1、type-aliases-package:开启包别名,用于mapper.xml简化类型映射,如:配置此项后,type可以只写类名,mybatis会自动去配置包名中找该类
2、map-underscore-to-camel-case:将数据库返回的下划线格式的字段映射到Java对象的驼峰式命名属性,这样可以省去resultMap去定义数据库表字段和实体类的映射关系。
3、jdbc-type-for-null:此项在oracle数据库时需要,不然在插入空值时会报错。此项配置null时指定jdbcType为NULL,不然jdbcType默认为OTHER