4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.zfh
springboot-mysql-01
0.0.1-SNAPSHOT
springboot-mysql-01
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-jdbc
com.alibaba
druid
1.1.4
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-maven-plugin
package com.fhzheng.demo.entity;
public class User1 {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.1控制器编写
package com.fhzheng.demo.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fhzheng.demo.entity.User;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/test")
public String test(Model model) {
//查询用户
List users = new ArrayList();
User user1 = new User();
user1.setName("郑丰华");
user1.setSex("男");
user1.setAge(20);
user1.setBirthday(new Date());
User user2 = new User();
user2.setName("郑富华");
user2.setSex("女");
user2.setAge(30);
user2.setBirthday(new Date());
User user3 = new User();
user3.setName("郑生华");
user3.setSex("保密");
user3.setAge(40);
user3.setBirthday(new Date());
users.add(user1);
users.add(user2);
users.add(user3);
model.addAttribute("users",users);
return "user";
}
}
server.port = 8081
### mysql连接信息
###### &allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
### 用户名
spring.datasource.username=rootcuit
### 密码
spring.datasource.password=rootcuit
### 驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
注意,这里的服务器端口号换成了8081
package com.fhzheng.demo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit4.SpringRunner;
import com.fhzheng.demo.entity.User1;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMysql01ApplicationTests {
@Resource
private JdbcTemplate jdbcTemplate;
@Test
public void mySqlTest() {
String sql="select id,username,password from user";
List userList = (List) jdbcTemplate.query(sql,new RowMapper() {
@Override
public User1 mapRow(ResultSet rs,int rowNum) throws SQLException{
User1 user = new User1();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
return user;
}
});
System.out.println("查询成功:");
for (User1 user : userList) {
System.out.println("【id】:"+user.getId()+";【name】:"+user.getUsername() );
}
}
@Test
public void contextLoads() {
}
}
###### druid配置信息
### 数据源类别
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
### 初始化参数:大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
### 超时时间
spring.datasource.maxWait=60000
### 检测并关闭空闲连接时间
spring.datasource.timeBetweenEvictionRunsMillis=60000
## 连接在池中的最小生存时间
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validateQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
### 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
### 配置监控统计拦截filters,去掉后监控界面无法显示SQL无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
### 通过connectionProperties属性来打开mergeSql功能,慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
package com.fhzheng.demo.filter;
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 com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
@Configuration
public class DruidConfiguration {
// TODO 未完成druid配置和测试
// 配置类完成ServletRegistrationBean和FilterRegistrationBean实现
// Servlet和Filter类的注册
// 在druidStatViewServlet类中,设定访问数据库的白名单,黑名单,登录用户名和密码等信息
// 在druidStateFilter类中,设定了过滤的规则和需要忽略的格式
@Bean
public ServletRegistrationBean druidStatViewServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//添加初始化参数 initParams
//白名单:
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
//IP黑名单(存在共同时,deny优先于allow)
//如果满足deny,就提示:Sorry, you are not permitted to view this page.
servletRegistrationBean.addInitParameter("deny","192.168.1.1");
//登录查看信息的帐号和密码
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
//是否能够重置数据
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidStateFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则
filterRegistrationBean.addUrlPatterns("/*");
//添加需要忽略的格式信息
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
启动应用程序,然后在地址栏中输入
http://localhost:8081/druid
如下图
得到用户登录界面
然后输入用户名和密码,这个是在配置类中设置的admin:123456
完成登录
然后就可以查看相应的信息了
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。
什么是Druid
Druid是一个高效的数据查询系统,主要解决的是对于大量的基于时序的数据进行聚合查询。数据可以实时摄入,进入到Druid后立即可查,同时数据是几乎是不可变。通常是基于时序的事实事件,事实发生后进入Druid,外部系统就可以对该事实进行查询。
Druid是一个JDBC组件,它包括三部分: