mybatisplus使用xml的示例詳解

一、配置xml路徑

mybatis-plus:
mapper-locations: classpath:mapper/*.xml

二、編寫Mapper裡面的方法

public interface UserMapper extends BaseMapper {
List findAll();

List<User> selectByXml(@Param("name") String name);
}

三、編寫sql

<select id="selectByXml" resultType="com.example.mybatisplusdemo.sample.model.User">
    select *
    from user
    <where>
        <if test="name != null and name != ''">
            and name = #{name}
        </if>
    </where>
</select>

四、測試

@Test
void test7(){
List users = userMapper.selectByXml("Jone");
users.stream().forEach(System.out::println);
}

結果:

==>  Preparing: select * from user WHERE name = ?
==> Parameters: Jone(String)
<==    Columns: ID, NAME, AGE, EMAIL
<==        Row: 1, Jone, 18, [email protected]
<==      Total: 1

五、更改Mapper裡面方法入參

public interface UserMapper extends BaseMapper {
List findAll();

// List selectByXml(@Param("name") String name);

List<User> selectByXml(@Param("ew") Wrapper<User> queryWrapper);
}
<select id="selectByXml" resultType="com.example.mybatisplusdemo.sample.model.User">
    select *
    from user
     ${ew.customSqlSegment}
</select>

測試:

@Test
void test7(){
    List<User> users = userMapper.selectByXml(new QueryWrapper<User>().eq("name","Jone"));
    users.stream().forEach(System.out::println);
}

執行結果:

==>  Preparing: select * from user WHERE (name = ?)
==> Parameters: Jone(String)
<==    Columns: ID, NAME, AGE, EMAIL
<==        Row: 1, Jone, 18, [email protected]
<==      Total: 1

到此這篇關於mybatisplus使用xml的文章就介紹到這瞭,更多相關mybatisplus使用xml內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: