SpringBoot 配置 Zebra
优质
小牛编辑
140浏览
2023-12-01
<dependencyManagement>
<dependencies>
<dependency>
<!--Import dependency management from SpringBoot-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.dianping.zebra</groupId>
<artifactId>zebra-client</artifactId>
</dependency>
<dependency>
<groupId>com.dianping.zebra</groupId>
<artifactId>zebra-cat-client</artifactId>
</dependency>
<dependency>
<groupId>com.dianping.zebra</groupId>
<artifactId>zebra-dao</artifactId>
</dependency>
</dependencies>
2.通过注解配置Zebra
DataSourceConfig.java
@Configuration
public class DataSourceConfig {
private String jdbcref="${jdbcref}";
//初始化数据源
@Bean(name="zebraDataSource")
public DataSource zebraDataSource() {
GroupDataSource ds = new GroupDataSource(jdbcref);
// 此处省略ds参数列表
ds.init();
return ds;
}
//配置sqlSessionFactory
@Bean(name="zebraSqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory(@Qualifier(value = "zebraDataSource")DataSource dataSource) throws IOException {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setDataSource(dataSource);
//如果你是将sql写在映射xml文件中,那么需要通过以下方式指定xml文件路径;本案例是直接通过注解来编写sql,因此不需要使用以下配置。
//PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//ssfb.setMapperLocations(resolver.getResources("classpath*:config/sqlmap/**/*.xml"));
return ssfb;
}
//配置zebra的Scanner
@Bean
public ZebraMapperScannerConfigurer mapperScannerConfigurer() throws IOException {
ZebraMapperScannerConfigurer configurer = new ZebraMapperScannerConfigurer();
configurer.setSqlSessionFactoryBeanName("zebraSqlSessionFactory");
//mapper接口所在包名
configurer.setBasePackage("com.dianping.zebra.springboot.mapper");
return configurer;
}
}
Application.java
//exclude掉mybatis自动装配的类 并且不要使用@MappperScnner注解
@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
至此配置完成 可以开始愉快的使用Zebra了~