springboot-2-整合myBatis及druid连接池
整合mybatis
1、引入mybatis的maven包
<!-- Spring Boot Mybatis 依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis-spring-boot}</version></dependency><!-- MySQL 连接驱动依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql-connector}</version></dependency><!-- 数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.5</version></dependency>
2、application.properties配置
server.port=8089 server.servlet.context-path=/first spring.profiles.active=dev#mybatis mybatis.typeAliasesPackage=com.example.springboot1.dao mybatis.mapperLocations=classpath:mapper/*.xml
spring.datasource.url=jdbc:mysql://10.6.80.147:3306/j_test?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=skSB@123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5
3、mybatis配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><properties><property name="dialect" value="mysql" /></properties><settings><!-- 这个配置使全局的映射器启用或禁用缓存。系统默认值是true,设置只是为了展示出来 --><setting name="cacheEnabled" value="true" /><!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 系统默认值是true,设置只是为了展示出来 --><setting name="lazyLoadingEnabled" value="true" /><!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)。 系统默认值是true,设置只是为了展示出来 --><setting name="multipleResultSetsEnabled" value="true" /><!--使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。 系统默认值是true,设置只是为了展示出来 --><setting name="useColumnLabel" value="true" /><!--允许 JDBC 支持生成的键。需要适合的驱动。如果设置为 true 则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如Derby)。 系统默认值是false,设置只是为了展示出来 --><setting name="useGeneratedKeys" value="false" /><!--配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 系统默认值是SIMPLE,设置只是为了展示出来 --><setting name="defaultExecutorType" value="SIMPLE" /><!--设置超时时间,它决定驱动等待一个数据库响应的时间。 系统默认值是null,设置只是为了展示出来 --><setting name="defaultStatementTimeout" value="25000" /></settings><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql" /><property name="offsetAsPageNum" value="true" /><property name="rowBoundsWithCount" value="true" /><property name="pageSizeZero" value="true" /><property name="reasonable" value="true" /></plugin></plugins> </configuration>
4、配置mapper扫描器
springboot只需,在启动类上加注解
@SpringBootApplication @MapperScan("com.example.springboot1.dao") @EnableCaching public class Springboot1Application {public static void main(String[] args) {SpringApplication.run(Springboot1Application.class, args);}}
到这里配置就全部完成了,可以开始使用了
5、dao层
这里dao层我们就不用了传统的xml映射的方式了。用一下mybatis提供的@Mapper注解,这使得我们可以不用去写烦人的xml了。
@Mapper @Component public interface StudentMapper {@Select("select * from student where name = #{name}")public Student find(String name); }
转载于:https://www.cnblogs.com/yinjing/p/11166556.html
springboot-2-整合myBatis及druid连接池相关推荐
- springboot整合mybatis 使用HikariCP连接池
springboot整合mybatis 使用HikariCP连接池 前言 Springboot让Java开发更加美好,本节主要讲的是使用Hikari数据库连接池,如果需要使用druid连接池的请看我另 ...
- Mybatis使用Druid连接池
1.导入maven依赖 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis< ...
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...
- java使用druid maven_SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置...
一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...
- druid连接池_SpringBoot整合JDBCTemplate及Druid连接池
SpringBoot整合JDBCTemplate 本篇文章将会介绍用SpringBoot整合JDBCTemplate来实现简单的增删改查的功能.及通过SpringBoot整合Druid数据库连接池实时 ...
- springboot 2.0.5配置Druid连接池和web监控后台
配置类: import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewSe ...
- mybatis mysql merge_使用Mybatis和druid连接池报错 merge sql error, dbType mysql, sql
在项目中用mybatis查询的时候遇到一个这样的问题,抛错详情如下: merge sql error, dbType mysql, sql : select r.id from role r inne ...
- SpringBoot整合Druid连接池
SpringBoot整合Druid连接池 前言 第一种整合方式 自定义的方式 引入Druid依赖 配置文件 配置类 实现监控功能 开启SQL防火墙 实现web应用 访问Druid页面需登录 第二种整合 ...
- mybatis 配置 mysql连接池_spring 5.x 系列第5篇 —— 整合 mybatis + druid 连接池 (xml配置方式)...
项目目录结构 1. 导入依赖 创建 maven 工程,除了 Spring 的基本依赖外,还需要导入 Mybatis 和 Druid 的相关依赖: org.springframeworkgroupId& ...
- spring-boot中使用druid连接池
最近因为项目的要求,需要在spring-boot中配置druid连接池,数据库是Oracle,并且是多数据源的连接池,特地写下我的配置经历. 用的框架是spring-boot,数据库是orac ...
最新文章
- C代码生成图片:BMP、PNG和JPEG
- java+pom.xml+是什么_java – 父pom.xml的包装无效,必须是“pom”,而是“ear”
- php补充 扩展,php补充安装扩展支持
- SAP UI5 应用开发教程之六十四 - 基于 OData V4 的 SAP UI5 表格控件如何实现 filter(过滤) 和 sort(排序)功能
- matlab must agree,EEMD遇到Inner matrix dimensions must agree.
- es6 嵌套数组循环_ES6 常用数组循环
- numpy文件读写的三对函数
- componentDidUpdate vs componentWillReceiveProps
- 面试题--------7、Files的常用方法
- git指定版本openwrt源码_关于Github Action自动编译Lean_Openwrt的配置修改问题
- 新浪微博PC端登录分析
- 《算法导论3rd第十二章》二叉查找树
- Elasticseach api keys are not enabled
- c语言编译器提示隐式声明,在C中使用“隐式声明功能”警告有什么含义?
- 计算机专业需要学习打字吗,电脑学习打字的最快方法是什么
- 日晒、水洗、蜜处理?简单带你认识咖啡加工处理法
- Android学习之 Fragment
- oracle 横竖互换,oracle横竖表互转
- c语言课程结束小项目:2048小游戏
- 生产订单工艺路线修改
热门文章
- 自主研发国产高端企业云服务器,浪潮商用机器如何聚沙成塔?
- 前端杂谈: CSS 权重 (Specificity)
- Learn C++学习笔记:第M章—最常用的智能指针:std::unique_ptr std::make_unique
- Python-批处理修改音频采样率(批量重采样)
- 章文嵩-构建云计算平台的实践
- java 立体几何体中心点,高中数学立体几何重心考点的解题技巧_招生指南
- pandas绘图plot函数
- uniapp应用内苹果支付怎么写?
- 高等代数 具有度量的线性空间(第10章)5 正交空间与辛空间
- 关于VScode中如何修改默认的中文注释格式(包括去除斜体和修改颜色)