<1.手动>(该手动版本为第一次试验获取,凭记忆粘贴,可能有遗漏,因为此版本在添加 spring cloud eureka 依赖,报mapper找不到错误,所以更改为使用mybaties
官网 给予整合包,搭建)
一 、整合数据源
1.添加jdbc配置文件 hmq-demo-server\src\main\resources\jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true jdbc.username=root jdbc.password=1234562.配置数据库驱动 com.heimeiqiu.config.DataSourceConfig
package com.heimeiqiu.config; import javax.sql.DataSource; import com.jolbox.bonecp.BoneCPDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** * Created by wxl on 2017/12/13. */ @Configuration @PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true) public class DataSourceConfig { @Value("${jdbc.url}") private String jdbcUrl; @Value("${jdbc.driverClassName}") private String jdbcDriverClassName; @Value("${jdbc.username}") private String jdbcUsername; @Value("${jdbc.password}") private String jdbcPassword; @Bean(destroyMethod = "close") public DataSource dataSource() { BoneCPDataSource boneCPDataSource = new BoneCPDataSource(); // 数据库驱动 boneCPDataSource.setDriverClass(jdbcDriverClassName); // 相应驱动的jdbcUrl boneCPDataSource.setJdbcUrl(jdbcUrl); // 数据库的用户名 boneCPDataSource.setUsername(jdbcUsername); // 数据库的密码 boneCPDataSource.setPassword(jdbcUsername); // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60); // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 boneCPDataSource.setIdleMaxAgeInMinutes(30); // 每个分区最大的连接数 boneCPDataSource.setMaxConnectionsPerPartition(100); // 每个分区最小的连接数 boneCPDataSource.setMinConnectionsPerPartition(5); return boneCPDataSource; } }2.添加banner hmq-demo-server\src\main\resources\banner.txt
生成网址
http://patorjk.com/software/taag/#p=display&f=Fire%20Font-k&t=heimeiqiu%0A
3.修改数据源类 com.heimeiqiu.config.DataSourceConfig
package com.heimeiqiu.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; /** * Created by wxl on 2017/12/13. * 数据源配置 */ @Configuration //开启配置 @PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true) //引入文件 读取配置 @SpringBootApplication public class DataSourceConfig extends SpringBootServletInitializer { @Value("${jdbc.url}") private String jdbcUrl; @Value("${jdbc.driverClassName}") private String jdbcDriverClassName; @Value("${jdbc.username}") private String jdbcUsername; @Value("${jdbc.password}") private String jdbcPassword; @Bean(destroyMethod = "close") public DataSource dataSource() { BoneCPDataSource boneCPDataSource = new BoneCPDataSource(); // 数据库驱动 boneCPDataSource.setDriverClass(jdbcDriverClassName); // 相应驱动的jdbcUrl boneCPDataSource.setJdbcUrl(jdbcUrl); // 数据库的用户名 boneCPDataSource.setUsername(jdbcUsername); // 数据库的密码 boneCPDataSource.setPassword(jdbcUsername); // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60); // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 boneCPDataSource.setIdleMaxAgeInMinutes(30); // 每个分区最大的连接数 boneCPDataSource.setMaxConnectionsPerPartition(100); // 每个分区最小的连接数 boneCPDataSource.setMinConnectionsPerPartition(5); return boneCPDataSource; } protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DataSourceConfig.class); } @Bean @ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象 public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // 设置数据源 sqlSessionFactoryBean.setDataSource(dataSource); // 设置mybatis的主配置文件 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource mybatisConfigXml = resolver.getResource("classpath:mapper/mybatis-config.xml"); sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml); // 设置别名包 sqlSessionFactoryBean.setTypeAliasesPackage("com.heimeiqiu.entity"); return sqlSessionFactoryBean; } }4。开启mapper扫描,数据源加载后
com.heimeiqiu.config.DataSourceConfig
package com.heimeiqiu.config; import com.heimeiqiu.HmqDemoServerApplication; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @AutoConfigureAfter(DataSourceConfig.class) //保证在MyBatisConfig实例化之后再实例化该类 public class MapperScannerConfig { // mapper接口的扫描器 @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.heimeiqiu.dao.mapper"); return mapperScannerConfigurer; } }
5.启动入口修改
//启动应用入口 public static void main(String[] args) { SpringApplication.run(HmqDemoServerApplication.class, args); } 并在启动类上添加 注解
@Configuration @SpringBootApplication //开启自动化注解 @ComponentScan(basePackages = "com.heimeiqiu") @MapperScan("com.heimeiqiu.dao.mapper")
<2mybaties 官网配置方案>
1.将mybaties 依赖换为
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.0.0version>
dependency>
2.修改数据库配置
#数据库设置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/hmq spring.datasource.username=root spring.datasource.password=123456 #-------------------------- # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 #spring.datasource.useGlobalDataSourceStat=true mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.heimeiqiu.entity