mybatis-plus .xml注意事项总结

发布时间:2024-02-21 15:30

1.xml配置可能未编译到class中

<build>
	 <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>
        resources>
build>

2.配置xml路径才能生效

如果pom文件中引入的是 mybatis-plus-boot-starter

 <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.1.0version>
dependency>

需要在properties文件中配置:

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

如果pom文件中引入的是mybatis-plus 包

<dependency>
     <groupId>com.baomidougroupId>
     <artifactId>mybatis-plus-boot-starterartifactId>
     <version>3.1.0version>
 dependency>

需要在properties文件中配置:

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

3.MyBaitsPlus的xml中的resultType总结

a.返回单个对象(一般为按Id查找)

<select id="getPerson" resultType="com.b505.entity.Person">
     select id, first_name firstName, last_name lastName, age, email, address  from person where id = #{id}
select>

b.返回List,需要返回的不是List而是List中所包含的对象所以resultType不能设置为List,而是设置为单个对象

c.查询单个对象,使用Map封装

mapper接口:
public interface PersonMapper
{
    Map<String, Object> getPersonMap(Integer id);
}
xml
<select id="getPersonMap" resultType="map">
    select id, first_name firstName, last_name lastName, age, email, address from person where id = #{id}
select>

d.查询多个对象,使用map封装

这种情况一般是使用主键(id)作为map的key,使用实体类作为value

public interface PersonMapper
{
    @MapKey("id")
    Map<Integer, Person> getPersonsMap(String address);
}
<select id="getPersonsMap" resultType="map">
    select id, first_name firstName, last_name lastName, age, email, address from person where address = #{address}
select>

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号