- Typically, SQL statements are stored in XML files. For each Mapper interface, there is an XML file corresponding to the Mapper. In simpler cases, annotations can be used instead of XML files.
- Configuration work: previously in the Mybatis file, through
resources
Locate the XML file, but now there is no XML file, you need to passclass
Property to locate the interface file- Before:
<mappers> <mapper resource="com/du/mybatis/dao/UserMapper.xml"/> </mappers> Copy the code
- Now:
<mappers> <mapper class="com.du.mybatis.dao.UserMapper"/> </mappers> Copy the code
public interface UserMapper {
@Select("select * from user")
List<User> selectUsers(a);
@Select("select * from user limit #{startIndex}, #{offset}")
List<User> selectUsersByLimit(@Param("startIndex") int startIndex, @Param("offset") int offset);
@Insert("insert into user(name, address) values(#{name}, #{address})")
int insertUser(User user);
@Delete("delete from user where id = #{id}")
int deleteUserById(@Param("id") int id);
@Update("update user set name=#{name}, address=#{address} where id=#{id}")
int updateUser(User user);
}
Copy the code
The ${}
Parameters saved in the parameter selection list must be passed@Param
Pointed out. When there is only one parameter, it can be ignored