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

這里定義了一個根據(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">
<!-- 注釋一定要使用這種形式,不然會報錯-->
<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é)


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

