From 8f4456d6b0124151b8b181285a78cd70541d542d Mon Sep 17 00:00:00 2001 From: viking Date: Wed, 8 May 2019 11:27:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=88=86=E6=94=AF2.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++++++--- .../DruidConfiguration.java | 5 +++-- .../{datasource => mapper}/DruidStatFilter.java | 3 ++- .../DruidStatViewServlet.java | 9 +++++---- .../controller/MyRestController.java | 17 +++++++++++++++++ .../MySpringBoot/mapper/FruitsMapper.java | 8 ++++++++ .../resources/application-mybatis.properties | 12 +++++++++--- src/main/resources/banner.txt | 3 +-- src/main/resources/mapper/FruitsMapper.xml | 6 +++++- 9 files changed, 56 insertions(+), 16 deletions(-) rename src/main/java/com/viking/MySpringBoot/config/{datasource => mapper}/DruidConfiguration.java (84%) rename src/main/java/com/viking/MySpringBoot/config/{datasource => mapper}/DruidStatFilter.java (87%) rename src/main/java/com/viking/MySpringBoot/config/{datasource => mapper}/DruidStatViewServlet.java (76%) diff --git a/README.md b/README.md index 95c31a9..8326a9d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # MySpringBoot That is a project for learning the springBoot
-当前版本1.0
-* springBoot中使用Hikari连接池和spring data JPA配置的多数据源连接 -* 基于springMVC的web项目和springBoot中跨域的配置 +当前版本2.0
+* springBoot中使用Druid连接池和Mybatis配置的数据源连接 +* 使用springBoot自带的日志工具Logging配置输出sql语句 +* 使用Druid Monitor监控sql +* 打开http://localhost:8080/druid/login.html登录监控平台 +* 登录账号在DruidStatViewServlet.java中配置 #### [Spring Boot Reference Guide](https://docs.spring.io/spring-boot/docs/current/reference/html/index.html) ##### [31. Working with SQL Databases](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html) * [31.1. Configure a DataSource](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-configure-datasource) diff --git a/src/main/java/com/viking/MySpringBoot/config/datasource/DruidConfiguration.java b/src/main/java/com/viking/MySpringBoot/config/mapper/DruidConfiguration.java similarity index 84% rename from src/main/java/com/viking/MySpringBoot/config/datasource/DruidConfiguration.java rename to src/main/java/com/viking/MySpringBoot/config/mapper/DruidConfiguration.java index ec10676..09048f4 100644 --- a/src/main/java/com/viking/MySpringBoot/config/datasource/DruidConfiguration.java +++ b/src/main/java/com/viking/MySpringBoot/config/mapper/DruidConfiguration.java @@ -1,4 +1,4 @@ -package com.viking.MySpringBoot.config.datasource; +package com.viking.MySpringBoot.config.mapper; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -9,7 +9,8 @@ import javax.sql.DataSource; /** - * Created by yanshuai on 2019/5/7 + * Created by Viking on 2019/5/7 + * 注入DruidDataSource */ @Configuration @Primary diff --git a/src/main/java/com/viking/MySpringBoot/config/datasource/DruidStatFilter.java b/src/main/java/com/viking/MySpringBoot/config/mapper/DruidStatFilter.java similarity index 87% rename from src/main/java/com/viking/MySpringBoot/config/datasource/DruidStatFilter.java rename to src/main/java/com/viking/MySpringBoot/config/mapper/DruidStatFilter.java index 8017d7d..ad2e138 100644 --- a/src/main/java/com/viking/MySpringBoot/config/datasource/DruidStatFilter.java +++ b/src/main/java/com/viking/MySpringBoot/config/mapper/DruidStatFilter.java @@ -1,4 +1,4 @@ -package com.viking.MySpringBoot.config.datasource; +package com.viking.MySpringBoot.config.mapper; import com.alibaba.druid.support.http.WebStatFilter; @@ -7,6 +7,7 @@ /** * Created by Viking on 2019/5/7 + * 配置DruidStatFilter */ @WebFilter( filterName="druidWebStatFilter", diff --git a/src/main/java/com/viking/MySpringBoot/config/datasource/DruidStatViewServlet.java b/src/main/java/com/viking/MySpringBoot/config/mapper/DruidStatViewServlet.java similarity index 76% rename from src/main/java/com/viking/MySpringBoot/config/datasource/DruidStatViewServlet.java rename to src/main/java/com/viking/MySpringBoot/config/mapper/DruidStatViewServlet.java index f8f4c02..321bca3 100644 --- a/src/main/java/com/viking/MySpringBoot/config/datasource/DruidStatViewServlet.java +++ b/src/main/java/com/viking/MySpringBoot/config/mapper/DruidStatViewServlet.java @@ -1,4 +1,4 @@ -package com.viking.MySpringBoot.config.datasource; +package com.viking.MySpringBoot.config.mapper; import com.alibaba.druid.support.http.StatViewServlet; @@ -8,13 +8,14 @@ /** * Created by Viking on 2019/5/7 + * 配置DruidStatViewServlet */ @WebServlet( urlPatterns= {"/druid/*"}, initParams= { - @WebInitParam(name="allow",value="127.0.0.1"), - @WebInitParam(name="loginUsername",value="root"), - @WebInitParam(name="loginPassword",value="1234"), + @WebInitParam(name="allow",value="127.0.0.1"),//允许访问HTML页面的IP + @WebInitParam(name="loginUsername",value="root"),//登录用户名 + @WebInitParam(name="loginPassword",value="1234"),//登录密码 @WebInitParam(name="resetEnable",value="true")// 允许HTML页面上的“Reset All”功能 } ) diff --git a/src/main/java/com/viking/MySpringBoot/controller/MyRestController.java b/src/main/java/com/viking/MySpringBoot/controller/MyRestController.java index 3990c4d..c2bf7fb 100644 --- a/src/main/java/com/viking/MySpringBoot/controller/MyRestController.java +++ b/src/main/java/com/viking/MySpringBoot/controller/MyRestController.java @@ -1,9 +1,11 @@ package com.viking.MySpringBoot.controller; +import com.viking.MySpringBoot.mapper.FruitsMapper; import com.viking.MySpringBoot.pojo.MyPojo; import com.viking.MySpringBoot.pojo.NewPojo; import com.viking.MySpringBoot.entity.Weather; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @@ -22,6 +24,9 @@ public class MyRestController { // @Autowired // private WeatherRepository weatherRepository; + @Autowired + private FruitsMapper fruitsMapper; + @RequestMapping(value = "user",method = RequestMethod.GET) public MyPojo getPojo(){ MyPojo pojo = new MyPojo(); @@ -85,4 +90,16 @@ public Object getWeather(Long id){ // return weatherRepository.getWeather(id); return "OK"; } + + @RequestMapping("mybatis") + public Object testSql(){ + return fruitsMapper.getAllWeather(); + } + @RequestMapping("ins") + public Object testInsert(Weather param){ + param.setDate(new Date()); + fruitsMapper.insert(param); + return "OK"; + + } } diff --git a/src/main/java/com/viking/MySpringBoot/mapper/FruitsMapper.java b/src/main/java/com/viking/MySpringBoot/mapper/FruitsMapper.java index ec9aac6..48a331e 100644 --- a/src/main/java/com/viking/MySpringBoot/mapper/FruitsMapper.java +++ b/src/main/java/com/viking/MySpringBoot/mapper/FruitsMapper.java @@ -1,7 +1,15 @@ package com.viking.MySpringBoot.mapper; +import com.viking.MySpringBoot.entity.Weather; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + /** * Created by Viking on 2019/5/7 */ public interface FruitsMapper { + List getAllWeather(); + + void insert(@Param("param") Weather weather); } diff --git a/src/main/resources/application-mybatis.properties b/src/main/resources/application-mybatis.properties index 3dcd944..decbbc6 100644 --- a/src/main/resources/application-mybatis.properties +++ b/src/main/resources/application-mybatis.properties @@ -19,8 +19,14 @@ spring.datasource.druid.max-wait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接(单位:毫秒) spring.datasource.druid.time-between-eviction-runs-millis=60000 # 配置一个连接在池中最小生存的时间(单位:毫秒) -spring.datasource.druid.min-evictable-idle-time-millis=30000 -spring.datasource.druid.validation-query=SELECT * FROM TABLE +spring.datasource.druid.min-evictable-idle-time-millis=300000 +# 用来检测连接是否有效的sql,要求是一个查询语句。 +# 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用 +spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 -spring.datasource.druid.use-global-data-source-stat=true \ No newline at end of file +spring.datasource.druid.use-global-data-source-stat=true +# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 +spring.datasource.druid.filters=stat,wall,slf4j +# 配置项目下所有的mapper包下的日志级别为debug +logging.level.com.viking.MySpringBoot.*.mapper=debug \ No newline at end of file diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt index d8024ee..82ebba7 100644 --- a/src/main/resources/banner.txt +++ b/src/main/resources/banner.txt @@ -16,8 +16,7 @@ \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - 佛祖保佑 永无BUG + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _____________________________________________ _ | | diff --git a/src/main/resources/mapper/FruitsMapper.xml b/src/main/resources/mapper/FruitsMapper.xml index e9c88e3..2431582 100644 --- a/src/main/resources/mapper/FruitsMapper.xml +++ b/src/main/resources/mapper/FruitsMapper.xml @@ -3,7 +3,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - select * from weather + + INSERT INTO weather (weather,temperature,tip,date,user_name) + VALUES (#{param.weather},#{param.temperature},#{param.tip},#{param.date},#{param.userName}) + \ No newline at end of file From 3a33848c744e7374125c2d980e8cb472ce31b0f6 Mon Sep 17 00:00:00 2001 From: viking Date: Thu, 9 May 2019 16:51:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B02.0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=97=A5=E5=BF=97=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/resources/application-mybatis.properties | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8326a9d..ae5c564 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # MySpringBoot That is a project for learning the springBoot
当前版本2.0
+* 修改springBoot启动时的Banner图样 * springBoot中使用Druid连接池和Mybatis配置的数据源连接 * 使用springBoot自带的日志工具Logging配置输出sql语句 * 使用Druid Monitor监控sql diff --git a/src/main/resources/application-mybatis.properties b/src/main/resources/application-mybatis.properties index decbbc6..4c45fbc 100644 --- a/src/main/resources/application-mybatis.properties +++ b/src/main/resources/application-mybatis.properties @@ -28,5 +28,7 @@ spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.use-global-data-source-stat=true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.druid.filters=stat,wall,slf4j +# 配置根目录日志级别(和下面的配置同时生效) +logging.level.root=error # 配置项目下所有的mapper包下的日志级别为debug -logging.level.com.viking.MySpringBoot.*.mapper=debug \ No newline at end of file +logging.level.com.viking.MySpringBoot.*.mapper=debug