MyBatis Mapper接口的配置需要完成以下几个步骤:
public interface UserMapper {
User getUserById(int id);
}
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
<!-- mybatis-config.xml -->
<configuration>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id) {
return userMapper.getUserById(id);
}
}
这样就完成了MyBatis Mapper接口的配置。注意,Mapper接口的命名规范和XML文件的命名规范需要按照约定来做,以便MyBatis能够正确地进行映射和配置。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: MyBatis中怎么进行延迟加载