(2.3)、 配置类SpringBoot时代
package com.jsxs.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.sql.DataSource; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; /** * @Author Jsxs * @Date 2023/7/9 14:28 * @PackageName:com.jsxs.config * @ClassName: MyDuridConfig * @Description: TODO 德鲁伊数据源配置 * @Version 1.0 */ @Configuration public class MyDruidConfig { /** * * @return 主要用途链接数据库和配置数据库信息 */ // @ConditionalOnMissingBean(DataSource.class) ->当数据中没有的话,才会使用默认的数据源 @Bean // 这里有数据源了所以使用我们配置的数据源 // @ConfigurationProperties("spring.datasource") // 因为德鲁伊的链接名和Hik的连接名一致,我们可以使用这个 public DataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); // 1.链接数据库 druidDataSource.setUrl("jdbc:mysql://localhost:3306/demo1"); druidDataSource.setUsername("root"); druidDataSource.setPassword("121788"); druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); // 2.打开SQL监控和防火墙 druidDataSource.setFilters("stat,wall"); System.out.println(druidDataSource.getClass()); return druidDataSource; } /** * * @return : 主要提供监控视图 和 JSON API的操作 */ @Bean public ServletRegistrationBean StatViewServlet(){ StatViewServlet statViewServlet = new StatViewServlet(); ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*"); // 1.添加监控的密码和账户 registrationBean.addInitParameter("loginUsername","1"); registrationBean.addInitParameter("loginPassword","1"); return registrationBean; } /** * * @return WebStatFilter用于采集web-URL关联监控的数据。 */ @Bean public FilterRegistrationBean WebStatFilter(){ WebStatFilter webStatFilter = new WebStatFilter(); FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>(webStatFilter); registrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); //排除这些路径 registrationBean.setUrlPatterns(Arrays.asList("/*")); // 拦截这个路径 return registrationBean; } }
(3).使用官方starter方式
(3.1)、引入druid-starter
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.17</version> </dependency>
@Deprecated // 标注已经过时,
(3.2)、分析自动配置
我们发现德鲁伊的包中自己带了自动配置的包
并且按需导入的方式进行导入 (即屏蔽掉以前SpringBoot默认的数据源)
- 原来的项:
spring.datasource
依然存在 - 扩展配置项
spring.datasource.druid
DruidSpringAopConfiguration.class
, 监控SpringBean的配置;配置项:spring.datasource.druid.aop-patterns
DruidStatViewServletConfiguration.class
, 监控页的配置:spring.datasource.druid.stat-view-servlet
;默认开启DruidWebStatFilterConfiguration.class
, web监控配置;spring.datasource.druid.web-stat-filter
;默认开启DruidFilterConfiguration.class
所有Druid自己filter的配置
private static final String FILTER_STAT_PREFIX = "spring.datasource.druid.filter.stat"; private static final String FILTER_CONFIG_PREFIX = "spring.datasource.druid.filter.config"; private static final String FILTER_ENCODING_PREFIX = "spring.datasource.druid.filter.encoding"; private static final String FILTER_SLF4J_PREFIX = "spring.datasource.druid.filter.slf4j"; private static final String FILTER_LOG4J_PREFIX = "spring.datasource.druid.filter.log4j"; private static final String FILTER_LOG4J2_PREFIX = "spring.datasource.druid.filter.log4j2"; private static final String FILTER_COMMONS_LOG_PREFIX = "spring.datasource.druid.filter.commons-log"; private static final String FILTER_WALL_PREFIX = "spring.datasource.druid.filter.wall";
(3.3)、示列
spring: datasource: url: jdbc:mysql://localhost:3306/db_account username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver druid: aop-patterns: com.atguigu.admin.* #监控SpringBean filters: stat,wall # 底层开启功能,stat(sql监控),wall(防火墙) stat-view-servlet: # 配置监控页功能 enabled: true login-username: admin login-password: admin resetEnable: false web-stat-filter: # 监控web enabled: true urlPattern: /* exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' filter: stat: # 对上面filters里面的stat的详细配置 slow-sql-millis: 1000 logSlowSql: true enabled: true wall: enabled: true config: drop-table-allow: false
https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
3.Mybatis 操作数据库
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
(1).原始(Spring) - 配置模式
- 全局配置文件
- SqlSessionFactory: 自动配置好了
- SqlSession:自动配置了 SqlSessionTemplate 组合了SqlSession
- @Import(AutoConfiguredMapperScannerRegistrar.class);
- Mapper: 只要我们写的操作MyBatis的接口标准了
@Mapper
就会被自动扫描进来
@EnableConfigurationProperties(MybatisProperties.class) : MyBatis配置项绑定类。 @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class }) public class MybatisAutoConfiguration{} @ConfigurationProperties(prefix = "mybatis") 前缀 public class MybatisProperties
MybatisX 插件。 这个插件可以实现映射跳转
命名空间指向的是 接口。 返回值结果是 实体类 。
(2).非注解的配置模式
application.yaml
mybatis: config-location: classpath:mybatis/mybatis-config.xml #指定mybatis的核心文件位置 mapper-locations: classpath:mybatis/mapper/*.xml #指定mybatis的映射文件位置
实体类
package com.jsxs.bean; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @Author Jsxs * @Date 2023/7/17 11:01 * @PackageName:com.jsxs.bean * @ClassName: Admin * @Description: TODO * @Version 1.0 */ @Data @AllArgsConstructor @NoArgsConstructor public class Admin { Integer id; String name; String password; }
mapper层接口: mapper注解是为了让mybatis发现,respoity目的是为了让
package com.jsxs.mapper; import com.jsxs.bean.Admin; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; /** * @Author Jsxs * @Date 2023/7/17 11:01 * @PackageName:com.jsxs.mapper * @ClassName: AdminMapper * @Description: TODO * @Version 1.0 */ @Mapper @Repository public interface AdminMapper { // 1.查询所有的用户 public List<Admin> allAdmin(); }
mybatis.xml映射SQL文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.jsxs.mapper.AdminMapper"> <select id="allAdmin" resultType="com.jsxs.bean.Admin"> select *from admin; </select> </mapper>
接口
package com.jsxs.service; import com.jsxs.bean.Admin; import com.jsxs.mapper.AdminMapper; import org.springframework.stereotype.Service; import java.util.List; /** * @Author Jsxs * @Date 2023/7/17 13:39 * @PackageName:com.jsxs.service * @ClassName: AdminService * @Description: TODO * @Version 1.0 */ public interface AdminService { public List<Admin> allAdmin(); }
接口实现类
package com.jsxs.service.impl; import com.jsxs.bean.Admin; import com.jsxs.mapper.AdminMapper; import com.jsxs.service.AdminService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @Author Jsxs * @Date 2023/7/17 13:44 * @PackageName:com.jsxs.service.impl * @ClassName: AdminServiceImpl * @Description: TODO * @Version 1.0 */ @Service public class AdminServiceImpl implements AdminService { @Resource AdminMapper adminMapper; @Override public List<Admin> allAdmin() { return adminMapper.allAdmin(); } }
测试实验
package com.jsxs.service.impl; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import static org.junit.jupiter.api.Assertions.*; /** * @Author Jsxs * @Date 2023/7/17 13:47 * @PackageName:com.jsxs.service.impl * @ClassName: AdminServiceImplTest * @Description: TODO * @Version 1.0 */ @SpringBootTest class AdminServiceImplTest { @Resource AdminServiceImpl adminService; @Test void test(){ System.out.println(adminService.allAdmin()); } }