springboot-data

导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


yaml配置文件
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource

# 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙防御sql注入,stat监控统计,logback日志
filters: stat,wall
# Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
#aop-patterns: com.springboot.servie.*
# lowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500


controller层
@RestController
public class JDBCController {

@Autowired
JdbcTemplate jdbcTemplate;

//查数据库的所有信息
//没有实体类,数据库中的字段如何获取 万能的Map
@GetMapping("/stuList")
public List<Map<String,Object>> stuList(){
String sql = "select * from stu";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}

@GetMapping("/stuList/add")
public String addStu(){
String sql = "insert into stu(id,name,gender,age,birthday,score) values(4,'马王八','男',18,'2021-11-11',80)";
jdbcTemplate.update(sql);
return "update-ok";
}

@GetMapping("/stuList/{id}")
public String deletStu(@PathVariable("id") int id){
String sql = "update stu set id=?,name=?,gender=?,age=?,birthday=?,score=? where id="+id;
jdbcTemplate.update(sql);
return "update-ok";
}
@GetMapping("/deleteList/{id}")
public String deleteStu(@PathVariable("id") int id){
String sql = "delete from stu where id=?";
jdbcTemplate.update(sql,id);
return "delete-ok";
}




config层
@Configuration
public class DruidConfig {

@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}

@Bean
public ServletRegistrationBean StatViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

HashMap<String, String> initParameters = new HashMap<>();
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","123456");

initParameters.put("allow","");

bean.setInitParameters(initParameters);
return bean;
}
@Bean
public FilterRegistrationBean webStaFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();

bean.setFilter(new WebStatFilter());


HashMap<String, String> initParameters = new HashMap<>();
initParameters.put("exclusions","*.js,*.css");

bean.setInitParameters(initParameters);
return bean;
}
}
上一篇:SpringBoot常用配置(持续更新)


下一篇:SpringBoot+Druid后台监控配置