学习springboot中的一些配置

1.通过springboot访问页面

在prom.xml 文件中配置
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

2.ssm框架用springboot来整合

思路:创建一个springboot项目加入mysql,mybatis,jdbc,springweb-starter的依赖。
遇到的问题:
1.mapper找不到:在主程序中添加注解@MapperScan("mapper的路径")
2.时区有问题:在springboot的配置文件中添加serverTimezone=GMT%2B8&
3.可能会警告jdbc的版本低,使用:com.mysql.cj.jdbc.Driver
**

3.加入事务

1.在主程序上面加上@EnableTransactionManagement//事务的开关
2.在serviceImpl的方法或类(在类上面加这个这个注解,使类里面的方法都受到这个注解的影响)上面加上@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollbackFor=RuntimeException.class)

4.DRUID的使用与监控

1.在prom.xml中配置druid的依赖

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.18</version>
		</dependency>

2.在配置文件中加入

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/atcrowdfunding?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource//加这个

3.监控druid,写一个这个类

import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@SpringBootConfiguration
public class DruidDataSourceConfig {
	//将数据库连接信息直接封装到数据源对象中
	@ConfigurationProperties(prefix = "spring.datasource") 
	@Bean
	public DataSource dataSource() throws SQLException {
	DruidDataSource dataSource = new DruidDataSource();
	//配置监控统计拦截的filters
	dataSource.setFilters("stat");
	return dataSource;
	}
	
	// 1、配置一个管理后台的Servlet
	@Bean
	public ServletRegistrationBean statViewServlet() {
		ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		Map<String, String> initParams = new HashMap<>();
		initParams.put("loginUsername", "admin");
		initParams.put("loginPassword", "123");
		initParams.put("allow", "");// 默认就是允许所有访问
		initParams.put("deny", "192.168.15.21");// 拒绝哪个ip访问
		bean.setInitParameters(initParams);
		return bean;
	}
	//2、配置一个web监控的filter
	@Bean
	public FilterRegistrationBean webStatFilter() {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		bean.setFilter(new WebStatFilter());
		Map<String, String> initParams = new HashMap<>();
		initParams.put("exclusions", "*.js,*.css,/druid/*");//排除过滤
		bean.setInitParameters(initParams);
		bean.setUrlPatterns(Arrays.asList("/*"));
		return bean;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值