综合概述
MyBatis 是一款优秀的持久层框架
- 它支持定制化 SQL、存储过程以及高级映射。
- MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
- MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。MyBatis是一款半ORM框架,相对于Hibernate这样的完全ORM框架,MyBatis显得更加灵活,因为可以直接控制SQL语句,所以使用MyBatis可以非常方便的实现各种复杂的查询需求。当然了,有利必有弊,也正因为太过自由,所以需要自己编写SQL语句,而如何编写更为简洁高效的SQL语句,也是一门学问。
环境/版本一览:
使用的环境:
- 开发工具:Intellij IDEA 2017.1.2
- springboot: 2.1.9.RELEASE
- jdk:1.8.0_161
- maven:3.5.4
- mysql 服务器 5.7
- druid 数据库连接池 1.1.20
添加相关依赖
添加 Maven 相关依赖,这里需要添加上WEB和MyBatis,数据库msql,pagehelper 分页插件的,druid 数据连接池依赖。
WEB依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
MyBatis依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
<!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency>
MySQL依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
druid 数据连接池依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
添加MyBatis配置---- 方式一或 方式 2 选择一种 即可
方式一:
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatis dao扫描配置
*
* @author lhh
* @Date 2019/10/26 10:25
*/
@Configuration
public class MyBatisConfig {
/**
* mybatis dao扫描配置
*
* @return
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
//扫描该路径下的dao层
mapperScannerConfigurer.setBasePackage("com.lhh.springboot.oauth.dao, com.lhh.springboot.modules.*.dao");
return mapperScannerConfigurer;
}
}
方式2:项目启动类 加 @MapperScan
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @author lhh
*/
@SpringBootApplication(scanBasePackages = {"com.lhh.springboot"}) //扫描此包的注解
@MapperScan("com.lhh.springboot.oauth.dao, com.lhh.springboot.modules.*.dao")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
在 application-dev.properties 配置:
#================== mybatis =====================# mybatis.mapper-locations=classpath:mappers/**/*.xml # 指定下划线驼峰法的映射配置 mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.jdbc-type-for-null=null
#================ mybatis pagehelper ==============# pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql #================== database 数据源配置 ===================# spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url= jdbc:mysql://127.0.0.1:3306/mango?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 初始化大小,最小,最大 spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 # 配置获取连接等待超时的时间 spring.datasource.druid.max-wait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.druid.time-between-eviction-runs-millis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false # 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.druid.filters=stat,wall spring.main.allow-bean-definition-overriding=true
测试是否成功
控制台没有报错,属于成功。
测试案例使用:
遇到的问题
1.Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
翻译就是:无法配置DataSource:未指定'url'属性,也无法配置嵌入数据源。
解决办法配置数据源
2.驱动报红
重新加载maven ,将其包注释,然后再恢复。
3.
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
说明:无法配置数据源:未指定'url'属性,并且无法配置任何嵌入式数据源。
原因:无法确定合适的驱动程序类别