SpringBoot--数据访问
- 数据库连接的步骤:
-
druid数据源starter整合:
-
第一中因为druid的依赖:我们需要自己配置druid的配置文件
-
package com.model.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; 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 org.springframework.context.annotation.Configuration; import javax.servlet.Filter; import javax.sql.DataSource; import java.util.HashMap; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/8/3 20:43 */ @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource(){ return new DruidDataSource(); } // 后台监控,web.xml,ServletRegistrationBean // 因为springboot内置了servlet容器,所以没有web.xml,我们想使用web.xml可以替代类:ServletRegistrationBean @Bean public ServletRegistrationBean servletRegistrationBean(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*"); // 后台需要有人登录,帐号密码设置 HashMap<String, String> initParameters = new HashMap<>(); // 增加配置 initParameters.put("loginUsername", "admin");//登录key是固定的,loginUsername,loginPassword initParameters.put("loginPassword", "123456"); // 允许谁访问 initParameters.put("allow",""); // 禁止谁访问 initParameters.put("zhangzihan","192.168.56.1") bean.setInitParameters(initParameters); return bean; } // 配置拦截器 @Bean public FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(); bean.setFilter(new WebStatFilter()); // 设置可以过滤那些,可以通过哪些 HashMap<Object, Object> initParameters = new HashMap<>(); initParameters.put("exclusions", "*.js,*.css,/druid/*");//不进行统计 return bean; } }
-
-
我们可以引入druid的场景启动器:(单纯的依赖我们需要自己写配置配置,如果有场景启动器start说明他整合了druid,对于 springboot-xxx-start:场景启动器,官方为我们配置好了配置类,且都有默认值)
-
Mybatis的整合:
-
springboot整合mybatis-plus:
-
-
-
删除时,重定向也可以代参数
-