亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot整合mybatis的超詳細(xì)過程(配置模式+注解模式)

 更新時間:2022年04月27日 10:11:47   作者:笑霸final  
這篇文章主要介紹了springboot整合mybatis的詳細(xì)過程(配置模式+注解模式),這里我使用的是配置模式+注解模式所以需要配置全局文件,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

一.簡單介紹

1.配置相關(guān)的依賴
2.配置模式
在這里插入圖片描述
3寫.mapper、controller、service
4.配置yaml文件 配置mybatis全局配置文件
(這里我使用的是配置模式+注解模式所以需要配置全局文件)

二具體配置

2.1.配置相關(guān)的依賴.

當(dāng)然也可以在創(chuàng)建springboot的時候勾選對應(yīng)的功能

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <!--mybatis整合springboot起步依賴-->
        <!-- 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.3</version>
        </dependency>

2.2 寫.mapper、controller、service

在寫這個之前話要寫基本的pojo

在這里插入圖片描述

pojo相關(guān) 這里使用了 lombok

package com.xbfinal.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ssmdb {
    private Integer id;
    private String name;
    private String type;
    private String description;
}

2.2.1mapper文件

話不多說注意代碼的注釋

package com.xbfinal.mapper;

import com.xbfinal.pojo.ssmdb;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface ssmdbMapper {
    //更具id查對應(yīng)的書用注解模式
    @Select("select * from tbl_book where id=#{id}")
    public ssmdb getById(int id);
    //查詢所有的書 ,這里我們用配置模式
    //配置模式我個人喜歡用來寫復(fù)制的sql語句(狗頭)
    public List<ssmdb> getAll();
}

2.2.2service文件

一般用來實現(xiàn)mapper的 直接看代碼

package com.xbfinal.service;
import com.xbfinal.mapper.ssmdbMapper;
import com.xbfinal.pojo.ssmdb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ser {
    @Autowired
    ssmdbMapper ssmdbMapper;
    public ssmdb getById(int id){
        return ssmdbMapper.getById(id);
    }
    public List<ssmdb> getAll(){
        return ssmdbMapper.getAll();
    }
}

2.2.2controller文件

package com.xbfinal.controller;
import com.xbfinal.pojo.ssmdb;
import com.xbfinal.service.ser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class controller01 {
    @Autowired
    ser ser;
    @RequestMapping("/id")
    public String to01(){
        
     final ssmdb byId = ser.getById(1);
        return byId.toString();
    }
    @RequestMapping("/all")
    public String to02(){
        // final ssmdb byId = ser.getById(1);
        final List<ssmdb> all = ser.getAll();
        return all.toString();
    }
}

2.3配置相關(guān)文件

1.寫mysql的文件。寫在application.yaml文件中

spring:
  datasource:
    password: 0615
    username: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false

2.由于用配置模式+注解模式所以需要配置mybatis全局文件
在這里插入圖片描述

在static文件下創(chuàng)建mybatis文件夾然后創(chuàng)建配置文件如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--基本的東西都不用配置,因為springboot配好了-->
</configuration>

接著在mapper文件下寫mybatis對應(yīng)的mapper配置

在這里插入圖片描述

<?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="com.xbfinal.mapper.ssmdbMapper">
    
    <!--public List<ssmdb> getAll();-->
    <select id="getAll" resultType="com.xbfinal.pojo.ssmdb">
        select * from tbl_book
    </select>
</mapper>

最后在yaml文件中配好mybatis

在這里插入圖片描述

mybatis:
  config-location: classpath:static/mybatis/mybatis-config.xml
  mapper-locations: classpath:static/mybatis/mapper/*.xml

三、結(jié)果截圖

在這里插入圖片描述

在這里插入圖片描述

數(shù)據(jù)庫

在這里插入圖片描述

四、可能遇到的報錯

SpringBoot連接數(shù)據(jù)庫報錯:Access denied for user ‘root‘@‘localhost‘ (using password: YES)

解決方案
檢查自己的mysql配置是否正確如果正確嘗試一下把密碼加上""如圖

在這里插入圖片描述

到此這篇關(guān)于springboot整合mybatis(配置模式+注解模式)的文章就介紹到這了,更多相關(guān)springboot整合mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入理解Java設(shè)計模式之職責(zé)鏈模式

    深入理解Java設(shè)計模式之職責(zé)鏈模式

    這篇文章主要介紹了JAVA設(shè)計模式之職責(zé)鏈模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2021-11-11
  • 深入分析JAVA流程控制語句

    深入分析JAVA流程控制語句

    這篇文章主要介紹了JAVA流程控制語句的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問題

    解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問題

    這篇文章主要介紹了解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java設(shè)計模式之適配器模式(Adapter)

    java設(shè)計模式之適配器模式(Adapter)

    這篇文章主要介紹了java設(shè)計模式之適配器模式Adapter的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 關(guān)于elasticsearch的match_phrase_prefix查詢詳解

    關(guān)于elasticsearch的match_phrase_prefix查詢詳解

    這篇文章主要介紹了關(guān)于elasticsearch的match_phrase_prefix查詢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 迅速掌握J(rèn)ava容器中常用的ArrayList類與Vector類用法

    迅速掌握J(rèn)ava容器中常用的ArrayList類與Vector類用法

    這篇文章主要介紹了Java容器中常用的ArrayList類與Vector類用法,文中只對其最基本的功能給出了示例代碼,需要的朋友可以參考下
    2015-11-11
  • Mybatis配置之typeAlias標(biāo)簽的用法

    Mybatis配置之typeAlias標(biāo)簽的用法

    這篇文章主要介紹了Mybatis配置之typeAlias標(biāo)簽的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 消息中間件ActiveMQ的簡單入門介紹與使用

    消息中間件ActiveMQ的簡單入門介紹與使用

    消息隊列是指利用高效可靠的消息傳遞機制進(jìn)行與平臺無關(guān)的數(shù)據(jù)交流,并基于數(shù)據(jù)通信來進(jìn)行分布式系統(tǒng)的集成,這篇文章主要給大家介紹了關(guān)于ActiveMQ的簡單入門介與使用的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Java關(guān)于數(shù)組的介紹與使用

    Java關(guān)于數(shù)組的介紹與使用

    數(shù)組(Array)是有序的元素序列。 若將有限個類型相同的變量的集合命名,那么這個名稱為數(shù)組名。組成數(shù)組的各個變量稱為數(shù)組的分量,也稱為數(shù)組的元素,有時也稱為下標(biāo)變量
    2021-09-09
  • Java初學(xué)者必會的Map集合及其原理

    Java初學(xué)者必會的Map集合及其原理

    這篇文章主要給大家介紹Map集合及其原理,該集合中的信息是key-value形式,Map集合與Collection集合又有什么不同呢,要想搞清楚以上問題,下面跟著小編一起來看看吧
    2023-06-06

最新評論