引入shardingsphere-jdbc-core-spring-boot-starter依赖后启动报错 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
时间: 2023-11-19 21:57:20 浏览: 311
在引入shardingsphere-jdbc-core-spring-boot-starter依赖后启动报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required,这是因为ShardingSphere需要一个SqlSessionFactory或SqlSessionTemplate来创建SqlSession,但是在你的配置中没有配置它们。你可以通过以下两种方式来解决这个问题:
1.配置SqlSessionFactory或SqlSessionTemplate
你可以在你的配置文件中配置SqlSessionFactory或SqlSessionTemplate,例如:
```yaml
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:mapper/*.xml
sqlSessionFactory:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: root
```
2.使用ShardingSphere提供的自动配置
你可以使用ShardingSphere提供的自动配置来自动配置SqlSessionFactory或SqlSessionTemplate,例如:
```yaml
spring:
shardingsphere:
datasource:
names: ds0, ds1
ds0:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds0
username: root
password: root
ds1:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds1
username: root
password: root
sharding:
tables:
user:
actual-data-nodes: ds$->{0..1}.user_$->{0..1}
table-strategy:
inline:
sharding-column: id
algorithm-expression: user_$->{id % 2}
```
阅读全文