SpringBoot集成Mybatis框架

导读:本篇文章讲解 SpringBoot集成Mybatis框架,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SpringBoot 集成Mybatis框架


前言

案例github地址(如果有用点个star呗) https://github.com/chenxiban/BlogCaseSet.git


SpringBoot 集成Mybatis框架

之前我们学习的Spring集成Mybatis,主要是加载Mybatis配置、接口和映射文件,然后加载到到Spring的
xml配置文件中,配置较为繁琐,而SpringBoot集成Mybatis只需要以下三步

  1. 添加依赖;
  2. 配置数据源;
  3. 扫描接口包。

具体集成步骤如下:

参考的数据库:

CREATE DATABASE /*!32312 IF NOT EXISTS*/`springbootmybatisdb` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `springbootmybatisdb`;

/*Table structure for table `city` */

DROP TABLE IF EXISTS `city`;

CREATE TABLE `city` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号',
  `provinceId` int(10) unsigned DEFAULT NULL COMMENT '省份编号',
  `cityName` varchar(20) DEFAULT NULL COMMENT '城市名称',
  `description` text COMMENT '城市描述',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

/*Data for the table `city` */

insert  into `city`(`id`,`provinceId`,`cityName`,`description`) values (1,1,'北京','这是北京市的描述信息,北京这家伙是中国首都,百年帝都,政治经济文化中心,也将是世界的中心.'),(2,2,'郑州','这是郑州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀,河南政治经济文化中心,也是中国的中心城市.'),(3,3,'ZhengZhou','这是郑州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀,河南政治经济文化中心,也是中国的中心城市.');
  1. 添加依赖;除了常规依赖外,需要加入Mybatis和MySQL依赖。
<!-- Spring Boot Mybatis 依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring-boot-version}</version>
		</dependency>

		<!-- MySQL 连接驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector-version}</version>
		</dependency>
  1. 在application.properties配置文件中,配置数据源、Mybatis的配置及映射文件。(附:logback显示sql语句,logback-spring.xml文件细节如下:)
## Mybatis 配置
# 实体所在包,起别名
mybatis: 
  typeAliasesPackage: com.cyj.springboot.entity	
  # 映射文件所在路径
  mapperLocations: classpath:mapper/*.xml

#日志输出debug级别以上细节
#logging.level.org.springframework.web=info
#日志输出mybatis接口执行SQL的细节
logging: 
  level: 
    com: 
      cyj: 
        springboot: 
          mapper: debug

#应用项目名称
#server.context-path=/mydemo
#修改tomcat的URIEncoding为UTF-8
server: 
  port: 80
  tomcat: 
    uri-encoding: UTF-8
     
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    #数据库意思是:机构权限文件
    url: mysql://localhost:3306/springbootmybatisdb?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: root
    dbcp2:                                          # 进行数据库连接池的配置
      min-idle: 5                                   # 数据库连接池的最小维持连接数
      initial-size: 5                               # 初始化提供的连接数
      max-total: 5                                  # 最大的连接数
      max-wait-millis: 200                          # 等待连接获取的最大超时时间
  #集中解决各种编码问题
  banner:
    charset: UTF-8
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  messages:
    encoding: UTF-8
  #     spring mvc 视图解析器
  mvc:
    view:
      prefix: /
      suffix: .html
  # 时间格式化
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    # 时区设置
    time-zone: GMT+8
  1. 在主模块上注解扫描接口包,使用@MapperScan(“包名”)。
package com.cyj.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // Spring Boot 应用的标识
@MapperScan("com.cyj.springboot.mapper") // mapper 接口类扫描包配置,不是mybatis映射文件
public class Application {
	public static void main(String[] args) {
		// 程序启动入口
		// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
		SpringApplication.run(Application.class, args);
	}
}

注:Controller层、Dao层、Service层自动注入和原来Spring框架用法的使用完全一样

项目结构图:

在这里插入图片描述


最后

  • 更多参考精彩博文请看这里:《陈永佳的博客》

  • 喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/97616.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!