在SpringBoot中整合Mybatis:

1.在pom.xml中添加mybatis依赖

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

2.连接数据库

在application.properties中添加数据源,同时添加对mybatis的配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.fyl.entity
#驼峰命名规范:
mybatis.configuration.map-underscore-to-camel-case=true

3.使用Mybatis

SpringBoot扫描Mapper接口包的方法

1.使用@Mapper接口类上,此方法需要在每个Mapper接口上都添加

2.在启动类使用@MapperScan可以指定要扫描mapper类的包的路径

@SpringBootApplication
@MapperScan("com.example.myspringboot.mapper")
public class MyspringbootApplication {public static void main(String[] args) {SpringApplication.run(MyspringbootApplication.class, args);}
}

使用@MapperScan扫描多个包

   @MapperScan("com.example.myspringboot.mapper","com.fyl.test")

注意:整合mysql是出现这个错误

错误原因:MySQL这个jar包依赖类型是runtime:即只有运行时生效,所以这里报错,不影响代码运行

解决办法:将runtime修改为Compile即可

4.mapper.xml文件位置

两种方式:

一种是放在resources文件夹下:

配置: mybatis.mapper-locations=classpath:mappers/*.xml

另一种是在java文件加下和接口在一块:

配置: mybatis.mapper-locations=classpath*:com/fyl/navigation/mappers/*.xml

注意:要在pom.xml文件中配置

<resources><!--资源路径插件--><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes></resource>
</resources>

5.MybatisConfig配置类:

@Configuration
@MapperScan(basePackages = {"com.greentranboot.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryGreentranDB")
public class MybatisConfig   {@Autowired@Qualifier("qingchaDB")private DataSource dataSource;@Beanpublic SqlSessionFactory sqlSessionFactoryGreentranDB() throws Exception {SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(dataSource);PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();Resource[] resources = pathMatchingResourcePatternResolver.getResources("classpath:com/greentranboot/dao/mappers/*.xml");factoryBean.setMapperLocations(resources);//添加插件factoryBean.setPlugins(new Interceptor[]{pageHelper()});return factoryBean.getObject();}@Beanpublic SqlSessionTemplate sqlSessionTemplate1() throws Exception {SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryGreentranDB());return template;}@Beanpublic PageHelper pageHelper(){//分页插件PageHelper pageHelper=new PageHelper();Properties properties=new Properties();properties.setProperty("reasonable","true");properties.setProperty("supportMethodsArguments","true");properties.setProperty("returnPageInfo","check");properties.setProperty("params","count=countSql");pageHelper.setProperties(properties);return pageHelper;}

注:mybatis分页插件使用:

(1)SpringBoot中配置

#mybatis 分页插件
pagehelper.helper-dialect=mysql
pagehelper.param=count=countSql
pagehelper.reasonable=fase
pagehelper.support-methods-arguments=true

(2) 配置类配置

@Bean
public PageHelper pageHelper(){//分页插件PageHelper pageHelper=new PageHelper();Properties properties=new Properties();properties.setProperty("reasonable","true");properties.setProperty("supportMethodsArguments","true");properties.setProperty("returnPageInfo","check");properties.setProperty("params","count=countSql");pageHelper.setProperties(properties);return pageHelper;
}

(3)使用插件

PageHelper.startPage(pageNum,pageSize);
List<WebsiteSearchVO> websiteSearchVOList = websiteMapper.websiteSearch(wname,cid);
PageInfo<WebsiteSearchVO> pageInfo=new PageInfo<>(websiteSearchVOList);
if(websiteSearchVOList!=null){return ServerResponse.createBySuccess(pageInfo);
}else{return ServerResponse.createByErrorMessage("没有数据");
}

SpringBoot(七) 整合Mybatis相关推荐

  1. springboot项目整合mybatis

    SpringBoot项目整合mybatis 本章内容 使用 idea创建 SpringBoot项目 SpringBoot项目中配制 mybatis 框架 1 创建 SpringBoot项目 1.1 在 ...

  2. springboot中整合mybatis及简单使用

    springboot中整合mybatis及简单使用 1.引入依赖 2.在applicaiton.yaml中配置数据源以及mybatis 3.创建sql测试表 4.编写mapper接口和mapper.x ...

  3. 玩转springboot:整合mybatis实例

    这篇文章讲解一下springboot整合mybatis,其实,springboot整合mybatis和springmvc整合mybatis并没有什么太大的区别,大体上还是差不多哦,只是比springm ...

  4. Springboot轻松整合Mybatis

    在springboot没有出现之前,spring整合mybatis是相当麻烦,各种bean配置,现在我们来感受一下Springboot整合Mybatis是有多么方便 先来看看效果 添加用户: 查询用户 ...

  5. java xml快捷注释_详解SpringBoot 快速整合Mybatis(去XML化+注解进阶)

    序言:使用MyBatis3提供的注解可以逐步取代XML,例如使用@Select注解直接编写SQL完成数据查询,使用@SelectProvider高级注解还可以编写动态SQL,以应对复杂的业务需求. 一 ...

  6. springboot之整合mybatis

    第一次搞,真的是花了老大的经历了,各种字段敲错了. 记录一下过程: 首先添加依赖: <dependency><groupId>org.mybatis.spring.boot&l ...

  7. springboot/springcloud整合mybatis(mysql)

    1.导入相关jar包 <!-- mysql客户端 --> <dependency><groupId>mysql</groupId><artifac ...

  8. SpringBoot(七)整合themeleaf+bootstrap

    前言 Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎.Thymeleaf的主要目标是将优雅的自然模板带到您的开发工作流程中-HTML能够在浏览器中正确显示,并且可以作为静态原型 ...

  9. SpringBoot第六篇:springboot整合mybatis

    本文主要讲解如何在springboot下整合mybatis,并访问数据库.由于mybatis这个框架太过于流行,所以我就不讲解了. 引入依赖 在pom文件引入mybatis-spring-boot-s ...

最新文章

  1. win7服务器远程灰色的,小编为你细说win7系统远程协助复选框是灰色的详细技巧...
  2. 计算机里创建本地磁盘分区,大神教你如何将本地硬盘进行分区!
  3. lduan SCVMM 2012 库服务器(五)
  4. python曲线图局部放大_python放大图片和画方格实现算法
  5. CImage类的使用
  6. 神经网络的BP算法推导详解
  7. 使用IPv6下载google drive 大文件
  8. 一个SQL tvp+.net的例子
  9. 拆解任天堂教科书般的界面动效设计
  10. Android源码配置第三方应用电池白名单流程分析笔记
  11. 岭回归、LASSO回归(包括公式推导)
  12. [GYCTF2020]Node Game
  13. 红米android版本,红米note2安卓版本号是多少?红米note2版本介绍
  14. Confluent之Kafka Connector初体验
  15. 微信怎样查绑定的服务器地址,你的微信绑定了哪些网站和应用?这个方法可以一键查看......
  16. 2022小木虫与研招网调剂监视脚本
  17. 20170627-BTC、ETH、LTC暴躁高盛大摩看空,仍有业内人士很乐观
  18. JAVA高级篇之Java Reflection详解
  19. 电商网站架构探索|SOA分布式架构详解
  20. ST(StoryTest)简单了解

热门文章

  1. 使用nodejs + wecharty打造你的个人微信机器人
  2. 圣诞之歌:クリスマス タイム和My Baby Grand~ぬくもりが欲しくて~ ZARD
  3. java包裹邮费计算_猿实战16——承运商之搭建你的运费基石
  4. 将centos7打造成桌面系统
  5. JVM vs DVM
  6. leet75:颜色分类
  7. 关于linux下UART串口编程的困惑
  8. RFID出入库管理是如何实施的
  9. 【EMGU CV】油管一个视频合集学习笔记
  10. 一楼到十楼的每层电梯门口都放着一颗钻石,钻石大小不一。你乘坐电梯从一楼到十楼,每层楼电梯门都会打开一次,只能拿一次钻石,问怎样才能拿到最大的一颗?