mybatisplus使用xml的示例详解
作者:shigongp
这篇文章主要介绍了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, test1@baomidou.com
<== 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, test1@baomidou.com
<== Total: 1
到此这篇关于mybatisplus使用xml的文章就介绍到这了,更多相关mybatisplus使用xml内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!