Druid连接池使用log4j监控sql
一、pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
<!-- 继承spring-boot父类,主要是用于管理版本号,每次集成是只需要加starter -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<!-- spring-boot继承springmvc -->
<dependencies>
<!-- 集成springmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 集成redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 集成数据库默认c3p0 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 引入mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 集成druid连接池 -->
<!-- <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId>
<version>1.1.10</version> </dependency> -->
<!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId>
<version>1.2.17</version> 0/dependency> -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
二、application.properties
属性可在:https://spring.io/projects/spring-boot#learn 里找。
spring.redis.host=192.168.0.198
spring.redis.password=123456
spring.datasource.url=jdbc:mysql://localhost/unit02
spring.datasource.username=root
spring.datasource.password=ps123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.stat-view-servlet.login-username=xf
spring.datasource.druid.stat-view-servlet.login-password=xm
spring.datasource.druid.filter.stat.slow-sql-millis=2000
三、程序入口
package cn.ps;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class Example {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
四、测试Sql
package cn.ps.contorller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JdbcController {
@Autowired
private JdbcTemplate jtl;
@GetMapping("/jdbc")
public List<Map<String, Object>> jdbc() {
List<Map<String, Object>> query = jtl.query("select * from user_t", new ColumnMapRowMapper());
return query;
}
@GetMapping("/jdbc1")
public List<Map<String, Object>> jdbc1() {
List<Map<String, Object>> query = jtl.query("select * from emp", new ColumnMapRowMapper());
return query;
}
}
五、登录查看
-
输入:IP地址:端口号/druid/login.html
-
填写账号密码,没设置的直接进
-
访问:
http://192.168.0.198:8080/jdbc
http://192.168.0.198:8080/jdbc1 -
结果:
六、redis操作
package cn.ps.contorller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisContorller {
@Autowired
private StringRedisTemplate srt;
@GetMapping("/redis")
public String redis() {
srt.boundValueOps("re").set("redis");
return srt.boundValueOps("re").get();
}
}