赞
踩
关于MySQL数据的安装本文不做介绍。
新建工程springboot-mysql,并引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
在application.yml文件中添加如下的配置:
spring:
datasource:
#MySQL连接信息
url: jdbc:mysql://172.16.6.31:3306/test
# 账号
username: root
# 密码
password: 123456
# 驱动
driver-class-name: com.mysql.jdbc.Driver
新建一张用户表t_user
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`name` varchar(10) DEFAULT '' COMMENT '用户姓名',
`password` varchar(32) DEFAULT '' COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `t_user` VALUES (1, '小H', '123456');
INSERT INTO `t_user` VALUES (2, '萧炎', '123456');
新建一个实体类User.java
public class User {
// 主键
private String id;
// 用户名
private String name;
// 密码
private String password;
// 省略getter、setter、toString
}
SpringbootMysqlApplicationTests.java
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootMysqlApplicationTests { @Resource private JdbcTemplate jdbcTemplate; @Test public void contextLoads() { String sql = "select id, name, password from t_user"; List<User> users = jdbcTemplate.query(sql, new RowMapper<User>() { @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setId(resultSet.getString("id")); user.setName(resultSet.getString("name")); user.setPassword(resultSet.getString("password")); return user; } }); System.out.println("查询成功:" users); } }
DRUID是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB连接池,据说是目前最好的连接池。
pom.xml中引入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
在application.yml添加以下配置
spring: datasource: #MySQL连接信息 url: jdbc:mysql://172.16.6.31:3306/test # 账号 username: root # 密码 password: 123456 # 驱动 driver-class-name: com.mysql.jdbc.Driver # 数据源类别 type: com.alibaba.druid.pool.DruidDataSource # 初始化大小、最小、最大 initialSize: 5 minIdle: 5 maxActive: 20 # 配置获取连接等待超时的时间,单位是毫秒 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvicableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置监控统计拦截的filters,去掉后监控界面的SQL无法统计,'wall'用于防火墙 filters: stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能,慢SQL记录 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多个DruidDataSource的监控数据 useGlobalDataourceStat: true
开启监控的方式:
@Configuration public class DruidConfiguration { private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class); private static final String DB_PREFIX = "spring.datasource"; @Bean public ServletRegistrationBean druidServlet() { logger.info("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); // IP白名单 servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // IP黑名单(共同存在时,deny优先于allow) servletRegistrationBean.addInitParameter("deny", "192.168.1.100"); //控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "9527"); //是否能够重置数据 禁用HTML页面上的“Reset All”功能 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
启动项目访问:http://localhost:8080/druid/index.html,输入配置的账号/密码:admin/9527
GitHub:https://github.com/chenjiecg/SpringBoot.git
本文由博客一文多发平台 OpenWrite 发布!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。