国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫配置過程

瀏覽:5日期:2023-02-20 11:54:30

Druid來自于阿里的一個(gè)開源連接池能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能,Spring Boot默認(rèn)不支持Druid和jpa,需要引入依賴。

1、引入依賴包

<!--druid--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.22</version> </dependency> <!--jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

2、配置application.properties

#druid配置-MYSQLspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=truespring.datasource.username=rootspring.datasource.password=123456 # 初始化大小,最小,最大spring.datasource.initialSize=5spring.datasource.maxActive=20spring.datasource.minIdle=5# 配置獲取連接等待超時(shí)的時(shí)間spring.datasource.max-wait=60000# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒spring.datasource.time-between-eviction-runs-millis=60000# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒spring.datasource.min-evictable-idle-time-millis=300000#檢測連接是否有效的sql,要求是一個(gè)查詢語句,常用select ’x’.如果validationQuery為null,testOnBorrow,testOnBorrow,testOnReturn,testWhileIdle都不會(huì)起作用。這個(gè)可以不配置spring.datasource.validation-query=SELECT ’x’#檢測連接是否有效的超時(shí)時(shí)間。spring.datasource.validation-query-timeout=60000spring.datasource.test-while-idle=truespring.datasource.test-on-borrow=falsespring.datasource.test-on-return=false# 打開PSCache,并且指定每個(gè)連接上PSCache的大小spring.datasource.pool-prepared-statements=truespring.datasource.max-pool-prepared-statement-per-connection-size=20# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),’wall’用于防火墻,#別名方式,擴(kuò)展插件,監(jiān)控統(tǒng)計(jì)用的filter:stat,日志用的filter:log4j,防御sql注入的filter:wallspring.datasource.filters=stat,wall,slf4j

3、Druid配置信息定制

@Configurationpublic class DruidConfig { @Autowired private DruidDataSourceProperties properties; @Bean(name = 'druidDataSource', initMethod = 'init', destroyMethod = 'close') @Qualifier('druidDataSource') public DataSource dataSource() throws Exception {DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setUrl(properties.getUrl());druidDataSource.setUsername(properties.getUsername());druidDataSource.setPassword(properties.getPassword());druidDataSource.setDriverClassName(properties.getDriverClassName());druidDataSource.setInitialSize(properties.getInitialSize());druidDataSource.setMaxActive(properties.getMaxActive());druidDataSource.setMinIdle(properties.getMinIdle());druidDataSource.setMaxWait(properties.getMaxWait());druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());druidDataSource.setValidationQuery(properties.getValidationQuery());druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());druidDataSource.setTestOnReturn(properties.isTestOnReturn());druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());druidDataSource.setFilters(properties.getFilters()); try { if (null != druidDataSource) {druidDataSource.setFilters('wall,stat');druidDataSource.setUseGlobalDataSourceStat(true);//Properties properties = new Properties();//properties.setProperty('decrypt', 'true');//druidDataSource.setConnectProperties(properties);druidDataSource.init(); }} catch (Exception e) { throw new RuntimeException( 'load datasource error, dbProperties is :', e);}return druidDataSource; }}

3、獲取Properties中配置信息

@Configuration@ConfigurationProperties(prefix = 'spring.datasource')public class DruidDataSourceProperties {private String url; private String username; private String password; private String driverClassName; private int initialSize; private int maxActive; private int minIdle; private int maxWait; private long timeBetweenEvictionRunsMillis; private long minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private boolean poolPreparedStatements; private int maxPoolPreparedStatementPerConnectionSize; private String filters; public String getUrl() {return url;} public void setUrl(String url) {this.url = url;} public String getUsername() {return username;} public void setUsername(String username) {this.username = username;} public String getPassword() {return password;} public void setPassword(String password) {this.password = password;} public String getDriverClassName() {return driverClassName;} public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;} public int getInitialSize() {return initialSize;} public void setInitialSize(int initialSize) {this.initialSize = initialSize;} public int getMaxActive() {return maxActive;} public void setMaxActive(int maxActive) {this.maxActive = maxActive;} public int getMinIdle() {return minIdle;} public void setMinIdle(int minIdle) {this.minIdle = minIdle;} public int getMaxWait() {return maxWait;} public void setMaxWait(int maxWait) {this.maxWait = maxWait;} public long getTimeBetweenEvictionRunsMillis() {return timeBetweenEvictionRunsMillis;} public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;} public long getMinEvictableIdleTimeMillis() {return minEvictableIdleTimeMillis;} public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;} public String getValidationQuery() {return validationQuery;} public void setValidationQuery(String validationQuery) {this.validationQuery = validationQuery;} public boolean isTestWhileIdle() {return testWhileIdle;} public void setTestWhileIdle(boolean testWhileIdle) {this.testWhileIdle = testWhileIdle;} public boolean isTestOnBorrow() {return testOnBorrow;} public void setTestOnBorrow(boolean testOnBorrow) {this.testOnBorrow = testOnBorrow;} public boolean isTestOnReturn() {return testOnReturn;} public void setTestOnReturn(boolean testOnReturn) {this.testOnReturn = testOnReturn;} public boolean isPoolPreparedStatements() {return poolPreparedStatements;} public void setPoolPreparedStatements(boolean poolPreparedStatements) {this.poolPreparedStatements = poolPreparedStatements;} public int getMaxPoolPreparedStatementPerConnectionSize() {return maxPoolPreparedStatementPerConnectionSize;} public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;} public String getFilters() {return filters;} public void setFilters(String filters) {this.filters = filters;} public DruidDataSourceProperties() {// TODO Auto-generated constructor stub} public DruidDataSourceProperties(String url, String username, String password, String driverClassName, int initialSize, int maxActive, int minIdle, int maxWait, long timeBetweenEvictionRunsMillis, long minEvictableIdleTimeMillis, String validationQuery, boolean testWhileIdle, boolean testOnBorrow, boolean testOnReturn, boolean poolPreparedStatements, int maxPoolPreparedStatementPerConnectionSize, String filters) {this.url = url;this.username = username;this.password = password;this.driverClassName = driverClassName;this.initialSize = initialSize;this.maxActive = maxActive;this.minIdle = minIdle;this.maxWait = maxWait;this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;this.validationQuery = validationQuery;this.testWhileIdle = testWhileIdle;this.testOnBorrow = testOnBorrow;this.testOnReturn = testOnReturn;this.poolPreparedStatements = poolPreparedStatements;this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;this.filters = filters;}}

如果需要Druid的監(jiān)控統(tǒng)計(jì)功能在配置代碼中加入以下代碼:

@Beanpublic ServletRegistrationBean druidServlet() {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), '/druid/*');// IP白名單 (沒有配置或者為空,則允許所有訪問)servletRegistrationBean.addInitParameter('allow', '127.0.0.1');// IP黑名單(共同存在時(shí),deny優(yōu)先于allow)//servletRegistrationBean.addInitParameter('deny', '');//控制臺(tái)管理用戶servletRegistrationBean.addInitParameter('loginUsername', 'admin');servletRegistrationBean.addInitParameter('loginPassword', 'admin');//是否能夠重置數(shù)據(jù) 禁用HTML頁面上的“Reset All”功能servletRegistrationBean.addInitParameter('resetEnable', 'false');return servletRegistrationBean;} @Beanpublic FilterRegistrationBean filterRegistrationBean() {FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());filterRegistrationBean.addUrlPatterns('/*');filterRegistrationBean.addInitParameter('exclusions', '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*');return filterRegistrationBean;}

訪問地址:http://127.0.0.1:8080/druid, 使用配置的賬號(hào)密碼登錄即可查看數(shù)據(jù)源及SQL統(tǒng)計(jì)等監(jiān)控信息。

4、jpa配置

@Configuration@EnableTransactionManagement@EnableJpaRepositories(entityManagerFactoryRef = 'entityManagerFactory',transactionManagerRef = 'transactionManager',basePackages = {'*.dao'})//指定需要掃描的dao所在包public class RepositoryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier('druidDataSource') private DataSource druidDataSource; @Bean(name = 'entityManager') @Primary public EntityManager entityManager(EntityManagerFactoryBuilder builder) {return entityManagerFactory(builder).getObject().createEntityManager(); } /** * 指定需要掃描的實(shí)體包實(shí)現(xiàn)與數(shù)據(jù)庫關(guān)聯(lián) * @param builder * @return */ @Bean(name = 'entityManagerFactory') @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {return builder.dataSource(druidDataSource).properties(getVendorProperties(druidDataSource)).packages('*.entity')//指定需要掃描的entity所在包.build(); } /** * 通過jpaProperties指定hibernate數(shù)據(jù)庫方言以及在控制臺(tái)打印sql語句 * @param dataSource * @return */ private Map<String, String> getVendorProperties(DataSource dataSource) {Map<String, String> map = jpaProperties.getProperties();map.put('hibernate.dialect', 'org.hibernate.dialect.MySQL8Dialect');map.put('hibernate.show_sql', 'true');return map; } /** * 創(chuàng)建事務(wù)管理 * @param builder * @return */ @Bean(name = 'transactionManager') @Primary PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {return new JpaTransactionManager(entityManagerFactory(builder).getObject()); } }

到此這篇關(guān)于springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫配置的文章就介紹到這了,更多相關(guān)springboot druid+jpa+MYSQL配置內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧洲一级视频 | 久久黄色毛片 | 亚洲综合射 | 美女张开腿给男生桶下面视频 | 三级黄色在线 | 久热香蕉精品视频在线播放 | 国产精品高清免费网站 | 欧美另类视频videosbest18 | 免费99视频有精品视频高清 | 精品欧美一区二区三区四区 | 国产夜色| 亚洲天堂色网站 | 男女猛烈无遮掩免费视频 | 亚洲美女在线观看亚洲美女 | 国产成人综合日韩精品无 | 成人午夜在线观看 | 中文字幕在线观看一区二区三区 | 91精品国产综合久久久久久 | 男女视频免费在线观看 | a级成人高清毛片 | 亚洲视频国产精品 | 久久九九色 | 美女被强行扒开双腿激情视频 | 99视频在线免费 | 久久久黄色片 | 中文国产成人精品少久久 | 久草8| 一级久久| 欧美区在线 | 曰本人做爰大片免费观看一 | 91欧美激情一区二区三区成人 | 国产亚洲精品一区二区久久 | 久久99精品久久久久久久野外 | 99精品久久精品一区二区 | 久久久国产精品免费看 | 99久久精品毛片免费播放 | 怡红院免费全部视频在线视频 | 日韩一中文字幕 | 国产一级做a爰片久久毛片99 | 美女午夜影院 | 韩国a级毛片 |