创建一个新的maven项目
新建Properties类(相当于调用的项目传入start的参数)
其中需要@ConfigurationProperties注解
prefix = "login" 的意思是调用的spring boot项目 application.properties中配置的参数
调用时传入的为
login.className = com.mysql.jdbc.Driver
login.url=jdbc:mysql://localhost:3306/humanresource?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
login.user=root
login.password=root
@ConfigurationProperties(prefix = "login")
public class LoginCheckProperties {private String className;private String url;private String user;private String password;public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
Config类
其中@EnableConfigurationProperties(LoginCheckProperties.class) 注解为固定写法 意义是实现自动配置 ,类为上文中的Properties类
@Configuration
@EnableConfigurationProperties(LoginCheckProperties.class)
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {// 精确到 master 目录,以便跟其他数据源隔离static final String PACKAGE = "com.airboot.bootdemo.dao.master";static final String MAPPER_LOCATION = "classpath*:mapper/master/*.xml";private String className;private String url;private String user;private String password;public MasterDataSourceConfig(LoginCheckProperties loginCheckProperties) {this.className = loginCheckProperties.getClassName();this.url = loginCheckProperties.getUrl();this.user = loginCheckProperties.getUser();this.password = loginCheckProperties.getPassword();}@Bean(name = "masterDataSource")public DataSource masterDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName(className);dataSource.setUrl(url);dataSource.setUsername(user);dataSource.setPassword(password);//dataSource.setDriverClassName("com.mysql.jdbc.Driver");//dataSource.setUrl("jdbc:mysql://localhost:3306/humanresource?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");//dataSource.setUsername("root");//dataSource.setPassword("root");return dataSource;}@Bean(name = "masterTransactionManager")public DataSourceTransactionManager masterTransactionManager() {return new DataSourceTransactionManager(masterDataSource());}@Bean(name = "masterSqlSessionFactory")public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(masterDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}}
META-INF/spring.factories
最后在resources中新建文件夹META-INF,在其中建立文件spring.factories.路径为上文Config中的路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=logincheckspringbootstarter.config.ds.MasterDataSourceConfig
如果要暴露多个使用以下代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.*.*.*.*Util,\
com.*.*.*.*Util,\
com.*.*.*.*Util,\
com.*.*.*.*Util,\
com.*.*.*.*Util
如果想要在jar中加入注释 则需要在pom文件中修改以下代码 这样会生成一个 *.jar 和一个*:source.jar 需要将这两个jar包都加入项目中。
org.apache.maven.pluginsmaven-source-plugin2.4trueattach-javadocsjar-Xdoclint:none
打包
使用命令mvn install
引入jar包
其中groupId 公司名称 我这里是cn.baocl
artifactId为项目名
这些在start项目的pom文件中配置
上一下pom文件
4.0.0cn.baoclcheck-spring-boot-starter1.1-SNAPSHOTorg.springframework.bootspring-boot-starter-web2.2.1.RELEASEorg.mybatis.spring.bootmybatis-spring-boot-starter1.3.2com.alibabadruid1.1.17org.springframework.bootspring-boot-devtoolsruntime2.2.1.RELEASEmysqlmysql-connector-javaruntime8.0.16org.springframework.bootspring-boot-configuration-processortrue2.2.1.RELEASE
cn.baoclcheck-spring-boot-starter1.1-SNAPSHOT
使用
然后就可以使用了 本文实现的为主项目定义一个从数据源。所以在主项目中
com.airboot.bootdemo.dao.master,classpath*:mapper/master/*.xml
这两个路径文件使用的就是start中的配置的数据源
阿里多数据源配置的博客请参考:
项目结构