【Spring Boot】整合MyBatis

配置

server:
  port: 9915
# 数据库 相关设置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql.ycx:3306/ycxdemo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&autoReConnect=true
    username: root
    password: 123456
    # hikari连接池的参数 相关设置
    hikari:
      # 生效超时
      validationTimeout: 3000
      # 定义获取连接的超时时间。最小250ms,默认30s
      connectionTimeout: 60000
      # 定义连接空闲时间。最小10s,默认10m
      idleTimeout: 60000
      # 定义最小的空闲连接数。推荐不设置。或与最大连接数一致;保持固定的连接数目
      minimumIdle: 10
      # 定义最大的连接数。默认10
      maximumPoolSize: 10
      # 定义连接的最大生命周期。推荐设置该属性。最小30s,默认30m
      maxLifeTime: 60000
      # 从连接池获取到连接后,进行检查的查询语句。推荐设置该属性。默认值为none
      connectionTestQuery: select 1
# MyBatis配置
mybatis:
  # 搜索指定包别名
  type-aliases-package: ycx.mybatis.entity
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapper-locations: classpath*:mapper/*Mapper.xml

起始

package ycx.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import ycx.mybatis.entity.Blog;

@MapperScan("ycx.mybatis.mapper")
@SpringBootApplication
public class MybatisDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisDemoApplication.class, args);
    }
}

配置扫描映射 @MapperScan("ycx.mybatis.mapper")

实体

package ycx.mybatis.entity;

import lombok.Data;
import org.apache.ibatis.type.Alias;

@Alias("Blog")
@Data
public class Blog {
    private String id;
    private String title;
    private String content;
    public String toString() {
        return "[id=" + id + ",title=" + title + ",content=" + content + "]";
    }
}

映射

package ycx.mybatis.mapper;

import org.springframework.stereotype.Repository;
import ycx.mybatis.entity.Blog;

@Repository
public interface BlogMapper {
//    @Select("select * from Blog where id = #{id}")
    Blog selectBlog(String id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ycx.mybatis.mapper.BlogMapper">
    <select id="selectBlog" resultType="ycx.mybatis.entity.Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

控制器

package ycx.mybatis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ycx.mybatis.entity.Blog;
import ycx.mybatis.service.BlogService;

@RestController
public class BlogController {
    @Autowired
    BlogService blogService;

    @RequestMapping("/getBlog")
    public Blog getBlog(String id) {
        return blogService.getBlog(id);
    }
}

服务

package ycx.mybatis.service;

import ycx.mybatis.entity.Blog;

public interface BlogService {
    Blog getBlog(String id);
}

package ycx.mybatis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ycx.mybatis.entity.Blog;
import ycx.mybatis.mapper.BlogMapper;
import ycx.mybatis.service.BlogService;
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogMapper blogMapper;
    @Override
    public Blog getBlog(String id) {
        return blogMapper.selectBlog(id);
    }
}

原文地址:https://www.cnblogs.com/yangchongxing/p/12242436.html

时间: 2024-08-30 16:29:31

【Spring Boot】整合MyBatis的相关文章

企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

Spring中整合MyBatis就不多说了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤.搜了一下Spring Boot整合MyBatis的文章,方法都比较老,比较繁琐.查了一下文档,实际已经支持较为简单的整合与使用.下面就来详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 整合MyBatis 新建Spring Boot项目,或以Chapter1为基础来操作 pom.xml中引入依赖 这里用到spring-bo

Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题

现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warning,至于怎末设置可以自行百度,这里不再赘述,但是接下来spring boot能够运行起来,但是通过浏览器访问的时候,就会报错,后来也是经过多方查询,发现了问题的原因,特此记录一下: spring  boot整合mybatis时,要将mapper装配到spring容器中,要在mapper接口中加上@M

第七天.spring boot 整合mybatis

一. spring boot 整合mybatis 1.整合思路: 1.1 添加依赖 mybatis 1.2 在配置文件中配置数据源信息 1.3 编写pojo mapper接口 mapeer映射文件 1.4手动配置mybatis的包扫描,在主启动类添加@MapperScan 1.5 启动springboot服务器 2.开始工程部署: 2.1:添加依赖 mybatis <!--整合springboot与mybatis的整合--> <dependencies> <dependenc

Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

一.多数据源的应用场景 目前,业界流行的数据操作框架是 Mybatis,那 Druid 是什么呢? Druid 是 Java 的数据库连接池组件.Druid 能够提供强大的监控和扩展功能.比如可以监控 SQL ,在监控业务可以查询慢查询 SQL 列表等.Druid 核心主要包括三部分: 1. DruidDriver 代理 Driver,能够提供基于 Filter-Chain 模式的插件体系. 2. DruidDataSource 高效可管理的数据库连接池 3. SQLParser 当业务数据量达

spring boot整合mybatis+mybatis-plus

Spring boot对于我来说是一个刚接触的新东西,学习过程中,发现这东西还是很容易上手的,Spring boot没配置时会默认使用Spring data jpa,这东西可以说一个极简洁的工具,可是我还是比较喜欢用mybatis,工具是没有最好的,只有这合适自己的. 说到mybatis,最近有一个很好用的工具--------mybatis-Plus(官网),现在更新的版本是2.1.2,这里使用的也是这个版本.我比较喜欢的功能是代码生成器,条件构造器,这样就可以更容易的去开发了. mybatis

spring boot 整合 mybatis

spring boot jpa的方式确实非常简单, 但是复杂系统避免不了自己写sql, 那么如果把sql写在方法的上面, 可能有些人会觉得怪异, 或者不舒服. 那么能不能将mybatis整合进spring boot , 将sql 分离出来呢. 一. pom.xml <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-s

spring boot整合mybatis

spring boot本来可以使用jpa进行数据库操作,但是考虑到jpa的资料比较少,学习成本比较大,不是所有的人都可以十分了解,因此考虑采用mybatis来进行数据库操作. 1.新建maven项目,在pom中添加相关依赖. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLoca

spring boot 整合mybatis(好用!!!!)

springboot整合mybatis 1.pom依赖 <!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> 2.配置application.properties spri

通过Spring Boot整合Mybatis分析自动配置详解

前言 SpringBoot凭借"约定大于配置"的理念,已经成为最流行的web开发框架,所以有必须对其进行深入的了解:本文通过整合Mybatis类来分析SpringBoot提供的自动配置(AutoConfigure)功能,在此之前首先看一个整合Mybatis的实例. SpringBoot整合Mybatis 提供SpringBoot整合Mybatis的实例,通过Mybatis实现简单的增删改查功能: 1.表数据 CREATE TABLE `role` (  `note` varchar(2

[web] spring boot 整合MyBatis

1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM