mybatis-plus使用xml自定義sql語句方式
學(xué)習(xí)準(zhǔn)備
使用mybaits-plus配置項(xiàng)目好基本的三個(gè)層次 dao service controller
可以參考我的這篇文章
前言
mybatis-plus的使用確實(shí)很方便,但我們在日常的使用中難免遇到復(fù)雜的查詢
這時(shí)候應(yīng)該使用xml自定義sql
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、在Mapper層自定義方法

這里定義了一個(gè)根據(jù)ID查詢方法
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper extends BaseMapper<User> {
User selectUserByID(@Param("id") int id);
}
二、創(chuàng)建mapper.xml文件
我這里是在resources下創(chuàng)建了mapper文件夾
并在里面創(chuàng)建User.xml


<?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.example.demo.mapper.UserMapper">
<!-- 注釋一定要使用這種形式,不然會(huì)報(bào)錯(cuò)-->
<select id="selectUserByID" resultType="com.example.demo.entity.User">
select * from db01.dept where deptno = #{id}
</select>
</mapper>
三、配置YAML


spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
server:
port: 8082
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.entity
總結(jié)


以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法
這篇文章主要介紹了idea注解參數(shù)換行時(shí)間日期格式設(shè)置方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決
這篇文章主要介紹了spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring 環(huán)境下實(shí)現(xiàn)策略模式的示例
這篇文章主要介紹了Spring 環(huán)境下實(shí)現(xiàn)策略模式的示例,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下2020-10-10
以Java?Web項(xiàng)目為例淺談前后端分離開發(fā)模式
這篇文章主要介紹了以Java?Web項(xiàng)目為例淺談前后端分離開發(fā)模式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作,結(jié)合實(shí)例形式分析了靜態(tài)工廠方法創(chuàng)建Bean的相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11

