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;
import java.util.Date;
public class User {
private String name;
private String sex;
private Integer age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
和
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;
}
}
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";
}
}
4.1 配置application.properties文件
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
这里的url配置一定要注意设置区时,否则解析可能出错
4.2 编写测试文件
@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() );
}
}
这里的测试,是直接调用jdbcTemplate的query方法,完成查询动作,然后再控制台输出查看结果.
4.3运行测试结果如下
5.1 添加druid配置类
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;
}
}
5.2 添加druid配置信息在application.properties文件中
###### 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
5.3 运行项目,测试
在地栏输入:http://localhost:8081/druid/
然后输入设置的用户名和密码:admin:123456
登录后的界面