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

MyBatis-Plus 自定義sql語句的實(shí)現(xiàn)

 更新時(shí)間:2020年12月17日 14:39:18   作者:IT賤男  
這篇文章主要介紹了MyBatis-Plus 自定義sql語句的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、引言

Good Good Study,Day Day Up,童鞋點(diǎn)個(gè)關(guān)注,不迷路,么么噠~~~

MP自帶的條件構(gòu)造器雖然很強(qiáng)大,有時(shí)候也避免不了寫稍微復(fù)雜一點(diǎn)業(yè)務(wù)的sql,那么那么今天說說MP怎么自定義sql語句吧。

二、配置

自定義的sql當(dāng)然是寫在XML文件中的啦,那么首先來定義xml文件的位置,在yml配置文件如下

mybatis-plus:
 # 如果是放在src/main/java目錄下 classpath:/com/*/*/mapper/*Mapper.xml
 # 如果是放在resource目錄 classpath:/mapper/**.xml
 mapper-locations: classpath:/mapper/**.xml

三、具體實(shí)現(xiàn)

使用注解實(shí)現(xiàn):

在我們Mapper接口中定義自定義方法即可。

/**
 * @Auther: IT賤男
 * @Date: 2019/6/10 14:40
 * @Description: User對(duì)象持久層
 */
public interface UserMapper extends BaseMapper<User> {
 
  /**
   *
   * 如果自定義的方法還希望能夠使用MP提供的Wrapper條件構(gòu)造器,則需要如下寫法
   *
   * @param userWrapper
   * @return
   */
  @Select("SELECT * FROM user ${ew.customSqlSegment}")
  List<User> selectByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper);
 
  /**
   * 和Mybatis使用方法一致
   * @param name
   * @return
   */
  @Select("SELECT * FROM user where name = #{name}")
  List<User> selectByName(@Param("name") String name);
 
}

使用xml文件實(shí)現(xiàn):

使用xml一定要指定xml文件所在位置

/**
 * @Auther: IT賤男
 * @Date: 2019/6/10 14:40
 * @Description: User對(duì)象持久層
 */
public interface UserMapper extends BaseMapper<User> {
 
  /**
   *
   * 如果自定義的方法還希望能夠使用MP提供的Wrapper條件構(gòu)造器,則需要如下寫法
   *
   * @param userWrapper
   * @return
   */
  List<User> selectByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper);
 
  /**
   * 和Mybatis使用方法一致
   * @param name
   * @return
   */
  List<User> selectByName(@Param("name") String name);
 
}
<?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="selectByName" resultType="com.example.demo.model.User">
    SELECT * FROM user where name = #{name}
  </select>
 
  <select id="selectByMyWrapper" resultType="com.example.demo.model.User">
    SELECT * FROM user ${ew.customSqlSegment}
  </select>
 
</mapper>

測(cè)試測(cè)試:

  /**
   * 自定義sql查詢語句
   */
  @Test
  public void selectByMySelect() {
    List<User> users = userMapper.selectByName("王天風(fēng)");
    users.forEach(System.out::println);
  }
 
  /**
   * 自定義sql使用Wrapper
   */
  @Test
  public void selectByMyWrapper() {
    QueryWrapper<User> wrapper = new QueryWrapper();
    wrapper.like("name", "雨").lt("age", 40);
    List<User> users = userMapper.selectByMyWrapper(wrapper);
    users.forEach(System.out::println);
  }

四、回答評(píng)論區(qū)提問

提問一:能否提供自定義Update語句與Wrapper的整合使用呢?

/**
 * <p>
 * 用戶 Mapper 接口
 * </p>
 *
 * @author IT賤男
 * @since 2019-06-14
 */
public interface UserMapper extends BaseMapper<User> {
 
 
  /**
   * 自定Wrapper修改
   *
   * @param userWrapper 條件構(gòu)造器
   * @param user    修改的對(duì)象參數(shù)
   * @return
   */
 
  int updateByMyWrapper(@Param(Constants.WRAPPER) Wrapper<User> userWrapper, @Param("user") User user);
 
}
<?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">
 
  <update id="updateByMyWrapper">
    UPDATE user SET email = #{user.email} ${ew.customSqlSegment}
  </update>
 
</mapper>
  @Test
  public void updateByMyWrapper() {
    // 條件構(gòu)造器
    QueryWrapper<User> wrapper = new QueryWrapper();
    wrapper.eq("name", "admin");
 
    // 修改后的對(duì)象
    User user = new User();
    user.setEmail("Test@email.com");
 
    userMapper.updateByMyWrapper(wrapper, user);
 
  }

到此這篇關(guān)于MyBatis-Plus 自定義sql語句的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis-Plus 自定義sql語句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論