springboot配置数据库连接池druid、整合mybatis、整合pagehelper springboot配置数据库连接池druid
druid所需pom依赖
<dependency>
<groupId>com.alibaba</groupId>
druid连接池配置详解<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
springboot默认数据源是at.jdbc.pool.DataSource
spring:
datasource:
#1.JDBC
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: sql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC username: mybatis_ssm
password: 123
druid:
#2.连接池配置
#初始化连接池的连接数量⼤⼩,最⼩,最⼤
initial-size: 5
min-idle: 5
max-active: 20
#配置获取连接等待超时的时间
max-wait: 60000
#配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: true
test-on-return: false
# 是否缓存preparedStatement,也就是PSCache 官⽅建议MySQL下建议关闭个⼈建议如果想⽤SQL防⽕墙建议打开
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界⾯sql⽆法统计,'wall'⽤于防⽕墙
filter:
stat:
merge-sql: true
slow-sql-millis: 5000
#3.基础监控配置
web-stat-filter:
enabled: true
url-pattern: /*
#设置不统计哪些URL
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: true
session-stat-max-count: 100
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
#设置监控页⾯的登录名和密码
login-username: admin
login-password: admin
allow: 127.0.0.1
#deny: 192.168.1.100
server:
port: 80
servlet:
context-path: /
接下来写⼏个测试⽅法来查看URL监控
package com.ller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class Hellocontroller {
@RequestMapping("/say1")
public String say1(){
return"zs说";
}
@RequestMapping("/say2")
public String say2(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return"ls说";
}
@RequestMapping("/say3")
public String say3(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return"ww说";
}
}
springboot整合mybatis
springboot整合mybatis逆向⽣成插件
相关pom依赖
<!--更改springboot中的mysql版本,逆向⽣成不兼容⾼版本-->
<mysql.version>5.1.44</mysql.version>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!--引⼊mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<resources>
<!--解决mybatis-generator-maven-plugin运⾏时没有将l⽂件放⼊target⽂件夹的问题--> <resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--解决mybatis-generator-maven-plugin运⾏时没有将jdbc.properites⽂件放⼊target⽂件夹的问题--> <resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>*.yml</include>
</includes>
</resource>
</resources>
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<!--使⽤Mybatis-generator插件不能使⽤太⾼版本的mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
逆向⽣成配置⽂件l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- 引⼊配置⽂件 -->
<properties resource="jdbc.properties"/>
<!--指定数据库jdbc驱动jar包的位置-->
<classPathEntry location="D:\initPath\mvn_repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>
<!-- ⼀个数据库⼀个context -->
<context id="infoGuardian">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
<property name="suppressDate" value="true"/> <!-- 是否⽣成注释代时间戳 -->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使⽤bigDecimal, false可⾃动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 01指定javaBean⽣成的位置 -->
<!-- targetPackage:指定⽣成的model⽣成所在的包名 -->
<!-- targetProject:指定在该项⽬下所在的路径 -->
<javaModelGenerator targetPackage="com.ity"
targetProject="src/main/java">
<!-- 是否允许⼦包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对model添加构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否针对string类型的字段在set的时候进⾏trim调⽤ -->
<property name="trimStrings" value="false"/>
<!-- 建⽴的Model对象是否不可改变即⽣成的Model对象不会有 setter⽅法,只有构造⽅法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 02指定sql映射⽂件⽣成的位置 -->
<sqlMapGenerator targetPackage="com.psy.springboot02.mapper"
targetProject="src/main/java">
<!-- 是否允许⼦包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 03⽣成XxxMapper接⼝ -->
<!-- type="ANNOTATEDMAPPER",⽣成Java Model 和基于注解的Mapper对象 -->
<!-- type="MIXEDMAPPER",⽣成基于注解的Java Model 和相应的Mapper对象 -->
<!-- type="XMLMAPPER",⽣成SQLMap XML⽂件和独⽴的Mapper接⼝ -->
<javaClientGenerator targetPackage="com.psy.springboot02.mapper"
targetProject="src/main/java" type="XMLMAPPER">
<!-- 是否在当前路径下新加⼀层schema,false路径p.del,p.del.[schemaName] --> <property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 -->
<!-- tableName为对应的数据库表 -->
<!-- domainObjectName是要⽣成的实体类 -->
<!-- enable*ByExample是否⽣成 example类 -->
<table schema="" tableName="t_mvc_book" domainObjectName="Book"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
<property name="useActualColumnNames" value="true" />
</table>
</context>
</generatorConfiguration>
jdbc.properties
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=mybatis_ssm
jdbc.password=123
逆向⽣成集成到maven中的命令
mybatis-generator:generate -e
使⽤@Repository注解,在启动类中添加@MapperScan(“xxxx”)注解,⽤于扫描Mapper类的包。@MapperScan("com.psy.springboot02.mapper")
测试:
springboot整合pagehelper
导⼊相关pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
配置l⽂件
#pagehelper分页插件配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
#显⽰⽇志
logging:
level:
com.psy.springboot02.mapper: debug
PagerAspect
package com.psy.springboot02.aspect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.psy.springboot02.uitls.PageBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Aspect
public class PagerAspect {
@Around("execution(* *..*Service.*Pager(..))")
public Object invoke(ProceedingJoinPoint args) throws Throwable{
Object[] params = Args();
PageBean pageBean = null;
for (Object param : params) {
if(param instanceof PageBean){
pageBean = (PageBean) param;
break;
}
}
if (pageBean !=null && pageBean.isPagination())
PageHelper.Page(),Rows());
Object proceed = args.proceed(params);
if (pageBean !=null && pageBean.isPagination()){
PageInfo pageInfo = new PageInfo((List)proceed);
pageBean.Total()+"");
}
return proceed;
}
}
相关代码:
List<Map> bookPager(Map map, PageBean pageBean);
<select id="bookPager" resultType="java.util.Map" parameterType="java.util.Map" > select * from t_mvc_book
<where>
<if test="bname !=null " >
and bname like concat ('%',#{bname},'%')
</if>
</where>
</select>
测试代码:
@Test
public void bookPager(){
Map map=new HashMap();
map.put("bname","圣墟");
PageBean pageBean=new PageBean();
pageBean.setPage(2);
List<Map> list = this.bookService.bookPager(map, pageBean);
for (Map m:list) {
System.out.println(m);
}
System.out.println(pageBean);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论