目录
Ⅱ、UserMapper.xml 查询所有用户的具体实现 SQL:🍓
前言🍭
❤️❤️❤️SSM专栏更新中,各位大佬觉得写得不错,支持一下,感谢了!❤️❤️❤️
上篇我们写了一个简单的根据id进行查询,知道了如何去进行查询,下面来仔细讲讲增删改查操作。
一、增删查改操作🍭
下面操作会使用到Spring Boot单元测试,可以先看:
Spring Boot单元测试_spring boot 单元测试_冷兮雪的博客-CSDN博客
1、查🍉
查询所有的用户:
Ⅰ、mapper接口:🍓
package com.example.ssmdemo1.mapper;
import com.example.ssmdemo1.entity.Userinfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper//需要添加 @Mapper 注解
public interface UserMapper {
/**
* 根据用户id查询用户信息
* @param id
* @return
*/
Userinfo getUserById(@Param("id") Integer id);
//查询所有的用户
List<Userinfo> getAll();
}
Ⅱ、UserMapper.xml 查询所有用户的具体实现 SQL:🍓
使用$进行传递参数可能会SQL注入,所以大部分情况下是使用#的
<?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.ssmdemo1.mapper.UserMapper">
<select id="getUserById" resultType="com.example.ssmdemo1.entity.Userinfo">
select * from userinfo where id=${id}
</select>
<select id="getAll" resultType="com.example.ssmdemo1.entity.Userinfo">
select * from userinfo
</select>
</mapper>
Ⅲ、进行单元测试🍓
在UserMapperTest中就有了getAll的测试代码: