本文讲解mybatis的基本使用,mybatis-generater的使用,druid连接和sql监控。

数据库请使用utf8mb4,可以尽量避免各种异常。

基础准备

pom引用

第一步肯定是要引用jar文件了。

1、引入mybaits

  1. <!--mybatis-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>1.1.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>druid</artifactId>
  14. <version>1.0.26</version>
  15. </dependency>
  16. <!--lombok-->
  17. <dependency>
  18. <groupId>org.projectlombok</groupId>
  19. <artifactId>lombok</artifactId>
  20. <version>1.18.4</version>
  21. </dependency>

2、引入mybatis-generater

  1. <dependency>
  2. <groupId>org.mybatis.generator</groupId>
  3. <artifactId>mybatis-generator-core</artifactId>
  4. <version>1.3.7</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <!--mybatis.generator maven插件-->
  8. <plugin>
  9. <groupId>org.mybatis.generator</groupId>
  10. <artifactId>mybatis-generator-maven-plugin</artifactId>
  11. <version>1.3.7</version>
  12. <configuration>
  13. <configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
  14. <overwrite>true</overwrite>
  15. </configuration>
  16. <dependencies>
  17. <dependency>
  18. <groupId>mysql</groupId>
  19. <artifactId>mysql-connector-java</artifactId>
  20. <version>8.0.12</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.itfsw</groupId>
  24. <artifactId>mybatis-generator-plugin</artifactId>
  25. <version>1.3.7</version>
  26. </dependency>
  27. </dependencies>
  28. </plugin>

application.yml配置数据源

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. driver-class-name: com.mysql.jdbc.Driver
  5. url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf8&useSSL=false
  6. username:
  7. password:
  8. #下面为连接池补充设置
  9. initialSize: 5
  10. # 配置获取连接等待超时的时间
  11. maxWait: 60000
  12. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  13. timeBetweenEvictionRunsMillis: 60000
  14. # 配置一个连接在池中最小生存的时间,单位是毫秒
  15. minEvictableIdleTimeMillis: 300000
  16. validationQuery: SELECT 1 FROM DUAL
  17. testWhileIdle: true
  18. testOnBorrow: false
  19. testOnReturn: false
  20. # 打开PSCache,并且指定每个连接上PSCache的大小
  21. poolPreparedStatements: true
  22. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  23. filters: stat,wall
  24. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  25. connectionProperties: config.stat.mergeSql=true;config.stat.slowSqlMillis=5000
  26. # 合并多个DruidDataSource的监控数据
  27. useGlobalDataSourceStat: true
  28. mybatis:
  29. type-aliases-package: com.alvin.entity
  30. mapper-locations: classpath:mapper/*Mapper.xml
  31. # 设置debug模式下打印mysql
  32. logging:
  33. level:
  34. com:
  35. alvin:
  36. mapper: debug

mapper扫描

加入注解:@MapperScan(“com.alvin.mapper”)

  1. @SpringBootApplication
  2. @MapperScan("com.alvin.mapper")
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

配置druid连接池

配置druid的连接池和页面监控

image.png

DruidDataSourceConfiguration 连接池

  1. import javax.sql.DataSource;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Primary;
  6. import com.alibaba.druid.pool.DruidDataSource;
  7. @Configuration
  8. public class DruidDataSourceConfiguration {
  9. @Bean
  10. @Primary
  11. @ConfigurationProperties(prefix = "spring.datasource")
  12. public DataSource druidDataSource() {
  13. DruidDataSource druidDataSource = new DruidDataSource();
  14. return druidDataSource;
  15. }
  16. }

DruidStatFilter 过滤规则

  1. import javax.servlet.annotation.WebFilter;
  2. import javax.servlet.annotation.WebInitParam;
  3. import com.alibaba.druid.support.http.WebStatFilter;
  4. /**
  5. * 配置druid过滤规则
  6. * @author Administrator
  7. *
  8. */
  9. @WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
  10. initParams={
  11. @WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/config/*")// 忽略资源
  12. })
  13. public class DruidStatFilter extends WebStatFilter {
  14. }

DruidStatViewServlet 监控页面

  1. import javax.servlet.annotation.WebInitParam;
  2. import javax.servlet.annotation.WebServlet;
  3. import com.alibaba.druid.support.http.StatViewServlet;
  4. /**
  5. * 配置druid页面配置
  6. * @author Administrator
  7. *
  8. */
  9. @SuppressWarnings("serial")
  10. @WebServlet(urlPatterns = "/druid/*",
  11. initParams={
  12. @WebInitParam(name="allow",value="192.168.1.20,127.0.0.1"),// IP白名单 (没有配置或者为空,则允许所有访问)
  13. @WebInitParam(name="deny",value="192.168.16.111"),// IP黑名单 (存在共同时,deny优先于allow)
  14. @WebInitParam(name="loginUsername",value="admin"),// 用户名
  15. @WebInitParam(name="loginPassword",value="admin"),// 密码
  16. @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
  17. })
  18. public class DruidStatViewServlet extends StatViewServlet {
  19. }

查看druid监控

启动application,输入http://localhost:8080/druid/sql.html,输入用户名和密码,bingo!。

image.png

idea插件

推荐:free mybatis plugin,可以自由在mapper和xml中跳转。

image.png

效果:

mapper文件

image.png

xml文件

image.png