温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot集合Mybatis的过程

发布时间:2021-08-25 23:33:10 来源:亿速云 阅读:126 作者:chen 栏目:编程语言

这篇文章主要讲解了“SpringBoot集合Mybatis的过程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot集合Mybatis的过程”吧!

玩了两三天的SpringBoot,集成其他框架,就是配置、配置、再配置。

这次配置一下Mybatis:

第一步、pom.xml中引入Mybatis依赖:

(注意:我的SpringBoot版本是2.0.3)

<!-- mybatis依赖begin -->    <dependency>      <groupId>org.mybatis.spring.boot</groupId>      <artifactId>mybatis-spring-boot-starter</artifactId>      <version>1.3.0</version>    </dependency>    <!-- mybatis依赖end -->    <!-- mysql驱动begin -->    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>    </dependency>    <!-- mysql驱动end -->

第二步、application.yml中配置数据库信息,Mapper信息和mybatis-config信息:

spring: datasource:  username: sblueice  password: sblueice  url: jdbc:mysql://localhost:3306/sblueice?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&allowMultiQueries=true&autoReconnect=true&useSSL=false  driver-class-name: com.mysql.jdbc.Drivermybatis: #Mapper所在位置 mapper-locations: classpath:mybatis/mapping/*Mapper.xml config-location: classpath:mybatis/mybatis-config.xml

说明:下面是Mybatis对应的位置信息,对应yml中配置的路径和名称

第三步、配置mybatis-config(没有的创建下就OK):

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"   "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>  <settings>     <setting name="cacheEnabled" value="true" /><!-- 全局映射器启用缓存 -->      <!-- 打印查询语句 -->    <setting name="logImpl" value="STDOUT_LOGGING" />  </settings>  <typeAliases>    <typeAlias alias="pd" type="com.sblueice.util.PageData" />  </typeAliases></configuration>

第四步、SpringBoot启动类中添加注解@MapperScan("com.sblueice.mapper")

说明:这样会增加耦合,但是不加又扫描不到,目前没找到合适的方法解耦,暂时用这个注解

@MapperScan("com.sblueice.mapper")@SpringBootApplicationpublic class DemoApplication {   public static void main(String[] args) {    SpringApplication.run(DemoApplication.class, args);  } }

完成以上步骤就可以开开心心的访问MySql数据库了。。。

感谢各位的阅读,以上就是“SpringBoot集合Mybatis的过程”的内容了,经过本文的学习后,相信大家对SpringBoot集合Mybatis的过程这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI