springboot整合mybatis配置jndi数据源
时间: 2023-08-12 08:04:57 浏览: 335
在Spring Boot中整合Mybatis并配置JNDI数据源的方法如下:
1. 在application.properties或application.yml文件中,配置JNDI数据源的相关信息。例如,可以使用以下配置:
```
spring.datasource.jndi-name=jndi/oscardb
```
2. 在Mybatis的配置文件中,指定Mapper文件的位置。例如,可以使用以下配置:
```
mybatis.mapper-locations=classpath:mapper/*.xml
```
3. 在Spring Boot的启动类中,添加`@MapperScan`注解来扫描Mapper接口。例如,可以使用以下配置:
```
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String\[\] args) {
SpringApplication.run(Application.class, args);
}
}
```
4. 在Spring Boot的配置类中,使用`@Bean`注解配置JNDI数据源。例如,可以使用以下配置:
```
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() throws NamingException {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("java:comp/env/jdbc/数据库名");
jndiObjectFactoryBean.afterPropertiesSet();
return (DataSource) jndiObjectFactoryBean.getObject();
}
}
```
通过以上步骤,你可以在Spring Boot中成功配置JNDI数据源并整合Mybatis。
#### 引用[.reference_title]
- *1* [springboot + mybatis + jndi](https://blog.csdn.net/jiandong06/article/details/108659673)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [SpringBoot整合MybatisPlus多数据源](https://blog.csdn.net/qq_37284798/article/details/129279732)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Spring整合mybatis配置JNDI数据源](https://blog.csdn.net/mwx523037520036/article/details/127700054)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文