Spring boot整合mysql和druid
集成mysql数据库
引入依赖
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-jdbc
添加数据库配置
Spring boot项目的resources目录下的applicataion.properties
# datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
设计数据库表和实体
create table user(
id bigint primary key auto_increment,
name varchar(10) not null ,
age int default 0
);
insert into user (name, age) VALUES ('jack', 12);
public class User {
private int id;//id
private String name;//姓名
private int age;//年龄
//get set省略
@Override
public String toString() {
return "User{"+
"id="+ id +
", name='"+ name + '\'' +
", age="+ age +
'}';
}
}
集成测试
在test文件夹下的SpringBootApplication.java中编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootMysqlApplicationTests {
@Resource
private JdbcTemplate jdbcTemplate;
@Test
public void contextLoads() {
String sql = "select id, name, age from user";
List userList = jdbcTemplate.query(sql, new RowMapper() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
return user;
}
});
System.out.println("----------查询结果如下:----------");
//下面使用JDK8的新特性,如果没有安装JDK8的可以修改为普通的for循环
userList.stream().forEach(user -> {System.out.println(user.toString());});
}
}
集成Druid
引入依赖
com.alibaba
druid-spring-boot-starter
1.1.10
添加配置
# druid,更多属性可以看官网
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=60000
spring.datasource.druid.filter.config.enabled=true
# 配置StatFilter
spring.datasource.druid.filter.stat.db-type=mysql
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
#
# 配置WallFilter
spring.datasource.druid.filter.wall.enabled=true
spring.datasource.druid.filter.wall.db-type=mysql
spring.datasource.druid.filter.wall.config.delete-allow=false
spring.datasource.druid.filter.wall.config.drop-table-allow=false
开启Druid功能
@Configuration
public class DruidConfiguration {
@Bean
public ServletRegistrationBean statViewServle() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//IP白名单
//servletRegistrationBean.addInitParameter("allow", "127.0.0.1, 127.0.0.1");
//IP黑名单
//servletRegistrationBean.addInitParameter("deny", "127.0.0.1, 127.0.0.1");
servletRegistrationBean.addInitParameter("loginUsername", "druid");
servletRegistrationBean.addInitParameter("loginPassword", "12345678");
//是否允许重置数据
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则
filterRegistrationBean.addUrlPatterns("/*");
//忽略过滤的格式
filterRegistrationBean.addInitParameter("exclusions", "*.js, *.jpg, *.png, *.css, /druid/*");
return filterRegistrationBean;
}
}
浏览效果