赞
踩
-- 创建 spring_boot DROP DATABASE IF EXISTS spring_boot; CREATE DATABASE spring_boot; USE spring_boot; -- 创建家居表 CREATE TABLE furn( `id` INT(11) PRIMARY KEY AUTO_INCREMENT, ## id `name` VARCHAR(64) NOT NULL, ## 家居名 `maker` VARCHAR(64) NOT NULL, ## 厂商 `price` DECIMAL(11,2) NOT NULL, ## 价格 `sales` INT(11) NOT NULL, ## 销量 `stock` INT(11) NOT NULL, ## 库存 `img_path` VARCHAR(256) NOT NULL ## 照片路径 ); -- 初始化家居数据 INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 北 欧 风 格 小 桌 子 ' , ' 熊 猫 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/1.jpg'); INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 简 约 风 格 小 椅 子 ' , ' 熊 猫 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/2.jpg'); INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 典 雅 风 格 小 台 灯 ' , ' 蚂 蚁 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/3.jpg'); INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 温 馨 风 格 盆 景 架 ' , ' 蚂 蚁 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/4.jpg');
<!--引入data-jdbc数据源-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!--引入mysql的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--这里没有使用版本仲裁-->
<version>5.1.49</version>
</dependency>
spring:
datasource: #配置数据源
url: jdbc:mysql://localhost:3306/spring_boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
package com.sun.springboot.bean; import lombok.Data; import java.math.BigDecimal; /** * @author 孙显圣 * @version 1.0 */ @Data public class Furn { private Integer id; private String name; private String maker; private BigDecimal price; private Integer sales; private Integer stock; private String imgPath; }
<!--引入starter-test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
package com.sun.springboot; import com.sun.springboot.bean.Furn; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import javax.annotation.Resource; import java.util.List; /** * @author 孙显圣 * @version 1.0 */ @SpringBootTest public class ApplicationTests { //使用JdbcTemplate @Resource private JdbcTemplate jdbcTemplate; @Test public void contextLoads() { //使用这个对象来封装查询结果 BeanPropertyRowMapper<Furn> furnBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Furn.class); //进行查询 List<Furn> query = jdbcTemplate.query("select * from `furn`", furnBeanPropertyRowMapper); for (Furn furn : query) { System.out.println(furn); } } }
<!--引入druid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
package com.sun.springboot.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * @author 孙显圣 * @version 1.0 */ @Configuration public class DruidDataSourceConfig { //注入一个德鲁伊数据源 @ConfigurationProperties("spring.datasource") //读取yaml配置文件的参数,获取数据源配置 @Bean public DataSource dataSource() { return new DruidDataSource(); } }
//配置德鲁伊监控sql功能
@Bean
public ServletRegistrationBean statViewServlet() {
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> registrationBean =
new ServletRegistrationBean<>(statViewServlet, "/druid/*");
//配置登录监控页面用户名和密码
registrationBean.addInitParameter("loginUsername", "root");
registrationBean.addInitParameter("loginPassword", "root");
return registrationBean;
}
package com.sun.springboot.controller; import com.sun.springboot.bean.Furn; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * @author 孙显圣 * @version 1.0 */ @Controller public class DruidSqlController { @Autowired JdbcTemplate jdbcTemplate; @ResponseBody @GetMapping("/sql") public List<Furn> crudDB(){ BeanPropertyRowMapper<Furn> rowMapper = new BeanPropertyRowMapper<>(Furn.class); List<Furn> furns = jdbcTemplate.query("select * from furn", rowMapper); for (Furn furn : furns) { System.out.println(furn); } return furns; } }
//配置webStatFilter
@Bean
public FilterRegistrationBean webStatFilter() {
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean<WebStatFilter> filterRegistrationBean =
new FilterRegistrationBean<>(webStatFilter);
//默认对所有 URL 请求监控
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
//排除 URL
filterRegistrationBean.addInitParameter
("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springboot-usersys</artifactId> <version>1.0-SNAPSHOT</version> <name>Archetype - springboot-usersys</name> <url>http://maven.apache.org</url> <!--导入springboot父工程--> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.3</version> </parent> <dependencies> <!--web场景启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <!--thymeleaf场景启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--引入data-jdbc数据源--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <!--引入mysql的驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!--这里没有使用版本仲裁--> <version>5.1.49</version> </dependency> <!--引入starter-test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--引入druid依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.17</version> </dependency> </dependencies> </project>
spring:
datasource: #配置数据源
url: jdbc:mysql://localhost:3306/spring_boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
package com.sun.springboot.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.Arrays; /** * @author 孙显圣 * @version 1.0 */ @Configuration public class DruidDataSourceConfig { //注入一个德鲁伊数据源 @ConfigurationProperties("spring.datasource") //读取yaml配置文件的参数,获取数据源配置 @Bean public DataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setFilters("stat, wall"); //开启sql监控 return druidDataSource; } //配置德鲁伊监控sql功能 @Bean public ServletRegistrationBean statViewServlet() { StatViewServlet statViewServlet = new StatViewServlet(); ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*"); //配置登录监控页面用户名和密码 registrationBean.addInitParameter("loginUsername", "root"); registrationBean.addInitParameter("loginPassword", "root"); return registrationBean; } //配置webStatFilter @Bean public FilterRegistrationBean webStatFilter() { WebStatFilter webStatFilter = new WebStatFilter(); FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter); //默认对所有 URL 请求监控 filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); //排除 URL filterRegistrationBean.addInitParameter ("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
<!--引入德鲁伊的starter-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
spring: datasource: #配置数据源 url: jdbc:mysql://localhost:3306/spring_boot?useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver #配置德鲁伊监控功能 druid: stat-view-servlet: enabled: true login-username: root login-password: root reset-enable: false #配置web监控 web-stat-filter: enabled: true url-pattern: /* exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #配置sql监控 filter: stat: log-slow-sql: true slow-sql-millis: 1000 #1000毫秒就算慢查询 enabled: true wall: enabled: true config: #黑白名单 drop-table-allow: false #不允许删除表 select-all-column-allow: false #不允许查询所有字段
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。