欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合mybatis的方法詳解

 更新時(shí)間:2022年03月13日 17:58:09   作者:小徐也要努力鴨  
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合mybatis的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1 依賴配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>
<dependencies>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.18.22</version>
     </dependency>
	 <dependency>
	     <groupId>org.mybatis.spring.boot</groupId>
	     <artifactId>mybatis-spring-boot-starter</artifactId>
	     <version>1.3.2</version>
	 </dependency>
	 <!--     spring連接驅(qū)動(dòng)時(shí),如com.mysql.cj.jdbc.Driver使用   -->
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

找到mybatis-spring-boot-starter配置的依賴,即autotoconfigure包,SpringBoot的自動(dòng)配置,會(huì)找到META-INF下的spring.factories,找到EnableAutoConfiguration對(duì)應(yīng)的類:

在這里插入圖片描述

在這里插入圖片描述

可知自動(dòng)配置類為MybatisAutoConfiguration:

在這里插入圖片描述

在這里插入圖片描述

查看配置綁定類MybatisProperties,可知yml中前綴配置為mybatis:

在這里插入圖片描述

可知,mybatis前綴的yml配置,可以配置屬性,比如:configLocationmapperLocations、typeAliasesPackage等等。

mybatis下,又具有@NestedConfigurationProperty成員變量,故而前綴是mybatis.configuration,其中具有如下屬性:

在這里插入圖片描述

其中有非常熟悉的屬性:mapUnderscoreToCamelCase,也就是數(shù)據(jù)庫字段下劃線轉(zhuǎn)駝峰的配置。

然后,數(shù)據(jù)源有如下配置:

在這里插入圖片描述

數(shù)據(jù)源的配置綁定是DataSourceProperties

在這里插入圖片描述

可知,數(shù)據(jù)源在yml中以spring.datasource為前綴,可配置連接數(shù)據(jù)源的driverClassName、url、username、password等參數(shù)。

在這里插入圖片描述

2 使用

2.1 SpringBoot配置整合mybatis:

建表:

在這里插入圖片描述

實(shí)體類(mybatis本質(zhì)上將數(shù)據(jù)庫表的數(shù)據(jù)和實(shí)體類對(duì)應(yīng),就是依靠的getter和setter,所以@Data是必須有的):

package com.xiaoxu.boot.dto;
import lombok.Data;
import java.util.Date;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.dto.PeopleDTO
 */
@Data
public class PeopleDTO {
    // 人的id編號(hào)
    long id;
    // 人的名字
    String myName;
    // 年齡
    int myAge;
    // 出生日期
    Date birthday;
}

新建Mapper接口:

在這里插入圖片描述

注意mapper接口必須要有@Mapper注解:

package com.xiaoxu.boot.mapper;
import com.xiaoxu.boot.dto.PeopleDTO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PeopleMapper {
    List<PeopleDTO> queryPeopleByAge(int age);
}

在resources目錄下準(zhǔn)備mybatis的配置文件,以及Mapper文件:

在這里插入圖片描述

mybatis-confi.xml:

<?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>
</configuration>

在這里插入圖片描述

<?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.xiaoxu.boot.mapper.PeopleMapper">
    <select id="queryPeopleByAge" resultType="com.xiaoxu.boot.dto.PeopleDTO">
        select * from my_people where my_age = #{age}
    </select>
</mapper>

在application.yml中配置如下:

spring:
  datasource:
    username: root
    password: ******
    url: jdbc:mysql://localhost:3306/xiaoxu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

#mybatis的相關(guān)配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
#  #mybatis配置文件
#  config-location: classpath:mybatis-config.xml
#  config-location和configuration不能同時(shí)存在
  #開啟駝峰命名
  configuration:
    map-underscore-to-camel-case: true

插入測試數(shù)據(jù):

在這里插入圖片描述

service層實(shí)現(xiàn):

package com.xiaoxu.service;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.boot.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.service.PeopleService
 */
@Service
public class PeopleService {
    @Autowired
    PeopleMapper peopleMapper;
    public List<PeopleDTO> getPeoples(int Age){
        return peopleMapper.queryPeopleByAge(Age);
    }
}

controller層實(shí)現(xiàn):

package com.xiaoxu.boot.controller;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.controller.PeopleController
 */
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/people")
    public List<PeopleDTO> queryPeople(@RequestParam(value = "ag") int age){
        return peopleService.getPeoples(age);
    }
}

執(zhí)行結(jié)果無誤:

在這里插入圖片描述

2.2 SpringBoot注解整合mybatis:

修改Mapper接口文件:

package com.xiaoxu.boot.mapper;
import com.xiaoxu.boot.dto.PeopleDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface PeopleMapper {
    List<PeopleDTO> queryPeopleByAge(int age);
    @Select("select * from my_people")
    List<PeopleDTO> queryAllPeople();
}

修改服務(wù)層:

package com.xiaoxu.service;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.boot.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.service.PeopleService
 */
@Service
public class PeopleService {
    @Autowired
    PeopleMapper peopleMapper;
    public List<PeopleDTO> getPeoples(int Age){
        return peopleMapper.queryPeopleByAge(Age);
    }
    public List<PeopleDTO> getAllPeople(){
        return peopleMapper.queryAllPeople();
    }
}

增加controller:

package com.xiaoxu.boot.controller;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.controller.PeopleController
 */
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/people")
    public List<PeopleDTO> queryPeople(@RequestParam(value = "ag") int age){
        return peopleService.getPeoples(age);
    }
    @GetMapping("/allPeople")
    public List<PeopleDTO> queryAllPeople(){
        return peopleService.getAllPeople();
    }
}

結(jié)果返回?zé)o誤:

在這里插入圖片描述

2.3 在配置類上增加@MapperScan注解,掃描某個(gè)包下的全部Mapper文件:

如果每個(gè)Mapper接口文件上增加@Mapper比較麻煩,那么可以在配置類,如主程序類上,增加@MapperScan注解,以及掃描路徑,效果和@Mapper一致:

在這里插入圖片描述

主程序類增加@MapperScan注解:

在這里插入圖片描述

重新執(zhí)行效果一致:

在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!  

相關(guān)文章

  • SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題

    SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題

    這篇文章主要介紹了SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 一文帶你深入了解Java泛型

    一文帶你深入了解Java泛型

    Java?泛型(generics)是?Jdk?5?中引入的一個(gè)新特性,?泛型提供了編譯時(shí)類型安全檢測機(jī)制,?該機(jī)制允許程序員在編譯時(shí)檢測到非法的類型。本文將通過示例詳解Java泛型的定義與使用,需要的可以參考一下
    2022-08-08
  • 詳解Java String類常用方法有哪些

    詳解Java String類常用方法有哪些

    今天給大家?guī)淼氖顷P(guān)于Java String的相關(guān)知識(shí),文章圍繞著String類常用方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • java  實(shí)現(xiàn)輸出隨機(jī)圖片實(shí)例代碼

    java 實(shí)現(xiàn)輸出隨機(jī)圖片實(shí)例代碼

    這篇文章主要介紹了java 實(shí)現(xiàn)輸出隨機(jī)圖片實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • MyBatis 動(dòng)態(tài)SQL之where標(biāo)簽的使用

    MyBatis 動(dòng)態(tài)SQL之where標(biāo)簽的使用

    本文主要介紹了MyBatis 動(dòng)態(tài)SQL之where標(biāo)簽,where 標(biāo)簽主要用來簡化 SQL 語句中的條件判斷,可以自動(dòng)處理 AND/OR 條件,下面就來具體介紹一下
    2024-01-01
  • 上傳自己的jar包到maven中央倉庫的快速操作方法

    上傳自己的jar包到maven中央倉庫的快速操作方法

    網(wǎng)絡(luò)上可以搜索到很多jar包到中央倉庫,但是都不是多適合自己的項(xiàng)目,于是自己動(dòng)手寫個(gè),本文檔通過sonatype上傳jar包至maven中央倉庫,Sonatype通過JIRA來管理OSSRH倉庫,具體實(shí)例代碼跟隨小編一起看看吧
    2021-08-08
  • Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的方法實(shí)例

    Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • SpringMVC中ModelAndView用法小結(jié)

    SpringMVC中ModelAndView用法小結(jié)

    本文主要介紹了SpringMVC中ModelAndView用法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • 淺談Java編程中的synthetic關(guān)鍵字

    淺談Java編程中的synthetic關(guān)鍵字

    這篇文章主要介紹了淺談Java編程中的synthetic關(guān)鍵字的相關(guān)內(nèi)容,包括其簡單的介紹和實(shí)例,需要的朋友可以了解下。
    2017-09-09
  • Java中操作超大數(shù)的方法

    Java中操作超大數(shù)的方法

    本篇文章是小編在網(wǎng)上整理的關(guān)于java操作超大數(shù)的方法以及解決思路,有興趣的朋友參考學(xué)習(xí)下。
    2018-06-06

最新評(píng)論