|
@@ -0,0 +1,68 @@
|
|
|
|
|
+package space.anyi.config;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
|
|
+import org.mybatis.spring.SqlSessionFactoryBean;
|
|
|
|
|
+import org.mybatis.spring.mapper.MapperScannerConfigurer;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.context.annotation.*;
|
|
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
|
+import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
+
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @fileName: SpringConfig
|
|
|
|
|
+ * @projectName: SSM_template
|
|
|
|
|
+ * @package: space.anyi.config
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @date:2026/4/24 13:57
|
|
|
|
|
+ * @description: Spring配置类替代spring-context.xml
|
|
|
|
|
+ */
|
|
|
|
|
+@Configuration // 标识为配置类
|
|
|
|
|
+@ComponentScan(basePackages = {"space.anyi"},excludeFilters = {@ComponentScan.Filter(value = {Controller.class}, type = FilterType.ANNOTATION)}) // 扫描Service层
|
|
|
|
|
+@PropertySource("classpath:jdbc.properties") // 加载属性文件
|
|
|
|
|
+@EnableTransactionManagement // 开启事务管理
|
|
|
|
|
+public class SpringConfig {
|
|
|
|
|
+ // 配置数据源
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public DataSource dataSource(@Value("${jdbc.url}") String url,
|
|
|
|
|
+ @Value("${jdbc.username}") String username,
|
|
|
|
|
+ @Value("${jdbc.password}") String password,
|
|
|
|
|
+ @Value("${jdbc.driver}") String driver) {
|
|
|
|
|
+ DruidDataSource dataSource = new DruidDataSource();
|
|
|
|
|
+ dataSource.setUrl(url);
|
|
|
|
|
+ dataSource.setUsername(username);
|
|
|
|
|
+ dataSource.setPassword(password);
|
|
|
|
|
+ dataSource.setDriverClassName(driver);
|
|
|
|
|
+ return dataSource;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 配置SqlSessionFactory
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
|
|
|
|
|
+ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
|
|
|
|
+ factoryBean.setDataSource(dataSource);
|
|
|
|
|
+ factoryBean.setTypeAliasesPackage("space.anyi.entity");
|
|
|
|
|
+ // 配置MyBatis设置
|
|
|
|
|
+ org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
|
|
|
|
|
+ configuration.setMapUnderscoreToCamelCase(true); // 驼峰命名
|
|
|
|
|
+ factoryBean.setConfiguration(configuration);
|
|
|
|
|
+ return factoryBean;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 配置Mapper扫描
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public MapperScannerConfigurer mapperScannerConfigurer() {
|
|
|
|
|
+ MapperScannerConfigurer configurer = new MapperScannerConfigurer();
|
|
|
|
|
+ configurer.setBasePackage("space.anyi");
|
|
|
|
|
+ return configurer;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 配置事务管理器
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public PlatformTransactionManager transactionManager(DataSource dataSource) {
|
|
|
|
|
+ return new DataSourceTransactionManager(dataSource);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|