MyBatis——进阶操作

文章详细介绍了MyBatis中resultMap的用法,包括当属性名不匹配时如何映射数据库字段到Java对象,以及如何处理like查询和多表联合查询。示例展示了如何定义resultMap,避免SQL注入问题,并通过继承创建新的VO对象来处理多表查询的结果。
摘要由CSDN通过智能技术生成

resultMap

xml中可以通过returnType来指定返回的对象,只需要一个对象名就可以返回所有的属性

但是,如果sql中的属性名和对象的名称不一致,那么就需要resultMap来指定返回的数据了

当数据库中是username,而对象是name时:
在这里插入图片描述
在xml中指定resultMap

<resultMap id="名称" type="实体类">
	<id column="sql主键名" property="程序主键名"></id>
	<result column="sql名称" property="程序名称">
</resultMap>
  • id:该resultMap的名称
  • type:程序中的实体类
  • :表格的主键
  • :表格的普通列
  • column:sql中的名称
  • property:程序中的名称
<resultMap id="baseMap" type="com.example.demo.entity.UserInfo">
    <id column="id" property="id"></id>
    <result column="username" property="name"></result>
    <result column="password" property="password"></result>
    <result column="photo" property="photo"></result>
    <result column="createtime" property="createTime"></result>
    <result column="updatetime" property="updateTime"></result>
    <result column="state" property="state"></result>
</resultMap>

然后在sql语句中执行即可

<select id="getAll" resultMap="baseMap">
    select * from userinfo
</select>

最后name会返回对应的信息
在这里插入图片描述
或者,可以使用sql中的取别名 as,将username变成name,程序就会返回对应的值了

<select id="getAll" resultType="com.example.demo.entity.UserInfo">
   select id, username as name, password, photo, createtime, updatetime, state from userinfo
</select>

在这里插入图片描述

like查询

如果想要使用模糊查询,那么就需要使用sql中的like,但是直接使用下面这个语句查询会报错

<select id="getListByName" resultType="com.example.demo.entity.UserInfo">
    select * from userinfo where username like '%#{username}%'
</select>

在这里插入图片描述
并且,也不能使用$来直接替换字符,这样会有sql注入的安全性问题

可以使用sql的concat方法,将这三个字符拼接起来

<select id="getListByName" resultType="com.example.demo.entity.UserInfo">
   select * from userinfo where username like concat('%', #{username}, '%')
</select>

就可以返回正确的结果了
在这里插入图片描述

多表联合查询

假设当前场景:有一个用户信息表和一个文章信息表,现在要通过用户的id来查找其发过的文章
用户信息:

@Data
public class UserInfo {
    private int id;
    private String username;
//    private String name;
    private String password;
    private String photo;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private int state;
}

文章信息:

@Data
public class ArticleInfo {
    private int id;
    private String title;
    private String createtime;
    private String updatetime;
    private String content;
    private int uid;
    private int rcount;
    private int state;
}

显然,我们需要使用多表查询,而多表查询的返回值不光有UserInfo的内容,还有ArticleInfo的内容,这时就需要一个新的对象来接收这些内容:
编写ArticleInfoVO类,使用继承,可以只写扩展的属性

public class ArticleInfoVO extends ArticleInfo {
    private String username;
}

ArticleMapper:

@Mapper
public interface ArticleMapper {
    ArticleInfoVO getById(@Param("id") Integer id);
}

ArticleMapper.xml:

<mapper namespace="com.example.demo.mapper.ArticleMapper">
    <select id="getById" resultType="com.example.demo.entity.viewobject.ArticleInfoVO">
        select a.*,u.username from articleinfo a
        left join userinfo u on u.id=a.id
        where a.id=#{id}
    </select>
</mapper>

ArticleMapperTest

@SpringBootTest
class ArticleMapperTest {
    @Autowired
    private ArticleMapper articleMapper;

    @Test
    void getById() {
        int id = 1;
        ArticleInfoVO articleInfoVO = articleMapper.getById(id);
        System.out.println(articleInfoVO);
    }
}

可以看到,最终不仅打印出来了id=1的文章属性,还打印出了username
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值