小白是一名.net程式員,之前小白介紹了過了自己的博客系統http://www.ttblog.site/,用.net寫厭了,所以想學下java嘗嘗鮮,於是小白準備用spring boot來實現一個博客內容管理系統。 因為管理系統要有自己的數據源,但是又要從博客系統獲取博客內容,所以第一反應是要弄一個 ...
小白是一名.net程式員,之前小白介紹了過了自己的博客系統http://www.ttblog.site/,用.net寫厭了,所以想學下java嘗嘗鮮,於是小白準備用spring boot來實現一個博客內容管理系統。
因為管理系統要有自己的數據源,但是又要從博客系統獲取博客內容,所以第一反應是要弄一個多數據源,因為沒有java開發實戰基礎,所以都是從網上百度的,這裡只是把自己的過程分享出來。
配置數據源:
spring.datasource.data-blog.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.data-blog.username= spring.datasource.data-blog.password= spring.datasource.data-blog.jdbc-url=jdbc:mysql://127.0.0.1:3306/Blog?characterEncoding=utf8&useSSL=false&serverTimezone=UTC spring.datasource.data-cms.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.data-cms.username= spring.datasource.data-cms.password= spring.datasource.data-cms.jdbc-url=jdbc:mysql://127.0.0.1:3306/CMS?characterEncoding=utf8&useSSL=false&serverTimezon
註意:之前我寫的是 spring.datasource.data-cms.url,但是出現“jdbcUrl is required with driverClassName”錯誤,於是百度得到結果為替換成jdbc-url:https://blog.csdn.net/qq_40437152/article/details/90905336
創建數據配置:
package com.blog.cms.properties; import org.apache.ibatis.session.SqlSessionFactory; 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.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.blog.cms.dao.b",sqlSessionFactoryRef = "blogSqlsessionFactory") public class DataSourceBLOG { /** * 返回數據源 * @return */ @Bean(name = "blogDataSoruce") @ConfigurationProperties(prefix="spring.datasource.data-blog") public DataSource dataSource(){ return DataSourceBuilder.create().build(); } /** * 返回資料庫會話工廠 * @return */ @Bean(name = "blogSqlsessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("blogDataSoruce")DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean=new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/b/**/*Mapper.xml")); return bean.getObject(); } /** * 返回資料庫會話模板 * @return */ @Bean(name = "blogSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("blogSqlsessionFactory")SqlSessionFactory factory){ return new SqlSessionTemplate(factory); } /** * 返回資料庫的事務 * @param dataSource * @return */ @Bean(name = "blogTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("blogDataSoruce") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } /** * 返回jdbc * @param dataSource * @return */ @Bean public JdbcTemplate jdbcTemplate(@Qualifier("blogDataSoruce") DataSource dataSource){ return new JdbcTemplate(dataSource);
package com.blog.cms.properties; import org.apache.ibatis.session.SqlSessionFactory; 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.context.properties.EnableConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Component; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.blog.cms.dao.c",sqlSessionFactoryRef = "cmsSqlsessionFactory") public class DataSourceCMS { /** * 返回數據源 * @return */ @Bean(name = "cmsDataSoruce") @ConfigurationProperties(prefix="spring.datasource.data-cms") public DataSource dataSource(){ return DataSourceBuilder.create().build(); } /** * 返回資料庫會話工廠 * @return */ @Bean(name = "cmsSqlsessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("cmsDataSoruce")DataSource dataSource) throws Exception{ SqlSessionFactoryBean bean=new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/c/**/*Mapper.xml")); return bean.getObject(); } /** * 返回資料庫會話模板 * @return */ @Bean(name = "cmsSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("cmsSqlsessionFactory")SqlSessionFactory factory){ return new SqlSessionTemplate(factory); } /** * 返回資料庫的事務 * @param ds * @return */ @Bean(name = "cmsTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("cmsDataSoruce") DataSource ds){ return new DataSourceTransactionManager(ds); }
@Autowired @Qualifier("jdbcTemplate") private JdbcTemplate jdbcTemplate; @Override public List<String> selectTables(String dataBaseName) { String sql="select table_name from information_schema.tables where table_schema=?"; try { List<String> list= jdbcTemplate.queryForList(sql,String.class,dataBaseName); return list; } catch (Exception ex) { return null; } }
註意:如果你不需要使用原生jdbc,則不需要最後一個方法,我這裡是需要。並且要加上“bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/b/**/*Mapper.xml"));”,我網上查的是有的人沒加,但是我不加的話一直提示找不到select節點的錯誤。
啟動類:
@SpringBootApplication(scanBasePackages = {"com.blog.cms", "com.blog.cms.web"}) @EnableScheduling @ServletComponentScan @EnableTransactionManagement(proxyTargetClass = true) public class AdminWebApplication { public static void main(String[] args) { SpringApplication.run(AdminWebApplication.class, args); } }
我的代碼結構如圖,每個數據源所對應的mapper和xml都要分開:
目前到現在,我已是能夠正常的運行項目了,但是目前還不知道配置數據源的地方那幾個註解的作用,準備去把它搞懂。總的感覺會.net再學java覺得很輕鬆,大部分都是類似的。